di.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Package di provides dependency injection for the Iris Hero and Iris MVC new features.
  2. // It's used internally by "hero" and "mvc" packages directly.
  3. package di
  4. import "reflect"
  5. type (
  6. // Hijacker is a type which is used to catch fields or function's input argument
  7. // to bind a custom object based on their type.
  8. Hijacker func(reflect.Type) (*BindObject, bool)
  9. // TypeChecker checks if a specific field's or function input argument's
  10. // is valid to be binded.
  11. TypeChecker func(reflect.Type) bool
  12. )
  13. var (
  14. // DefaultHijacker is the hijacker used on the package-level Struct & Func functions.
  15. DefaultHijacker Hijacker
  16. // DefaultTypeChecker is the typechecker used on the package-level Struct & Func functions.
  17. DefaultTypeChecker TypeChecker
  18. )
  19. // Struct is being used to return a new injector based on
  20. // a struct value instance, if it contains fields that the types of those
  21. // are matching with one or more of the `Values` then they are binded
  22. // with the injector's `Inject` and `InjectElem` methods.
  23. func Struct(s interface{}, values ...reflect.Value) *StructInjector {
  24. if s == nil {
  25. return &StructInjector{Has: false}
  26. }
  27. return MakeStructInjector(
  28. ValueOf(s),
  29. DefaultHijacker,
  30. DefaultTypeChecker,
  31. Values(values).CloneWithFieldsOf(s)...,
  32. )
  33. }
  34. // Func is being used to return a new injector based on
  35. // a function, if it contains input arguments that the types of those
  36. // are matching with one or more of the `Values` then they are binded
  37. // to the function's input argument when called
  38. // with the injector's `Inject` method.
  39. func Func(fn interface{}, values ...reflect.Value) *FuncInjector {
  40. if fn == nil {
  41. return &FuncInjector{Has: false}
  42. }
  43. return MakeFuncInjector(
  44. ValueOf(fn),
  45. DefaultHijacker,
  46. DefaultTypeChecker,
  47. values...,
  48. )
  49. }
  50. // D is the Dependency Injection container,
  51. // it contains the Values that can be changed before the injectors.
  52. // `Struct` and the `Func` methods returns an injector for specific
  53. // struct instance-value or function.
  54. type D struct {
  55. Values
  56. hijacker Hijacker
  57. goodFunc TypeChecker
  58. }
  59. // New creates and returns a new Dependency Injection container.
  60. // See `Values` field and `Func` and `Struct` methods for more.
  61. func New() *D {
  62. return &D{}
  63. }
  64. // Hijack sets a hijacker function, read the `Hijacker` type for more explanation.
  65. func (d *D) Hijack(fn Hijacker) *D {
  66. d.hijacker = fn
  67. return d
  68. }
  69. // GoodFunc sets a type checker for a valid function that can be binded,
  70. // read the `TypeChecker` type for more explanation.
  71. func (d *D) GoodFunc(fn TypeChecker) *D {
  72. d.goodFunc = fn
  73. return d
  74. }
  75. // Clone returns a new Dependency Injection container, it adopts the
  76. // parent's (current "D") hijacker, good func type checker and all dependencies values.
  77. func (d *D) Clone() *D {
  78. return &D{
  79. Values: d.Values.Clone(),
  80. hijacker: d.hijacker,
  81. goodFunc: d.goodFunc,
  82. }
  83. }
  84. // Struct is being used to return a new injector based on
  85. // a struct value instance, if it contains fields that the types of those
  86. // are matching with one or more of the `Values` then they are binded
  87. // with the injector's `Inject` and `InjectElem` methods.
  88. func (d *D) Struct(s interface{}) *StructInjector {
  89. if s == nil {
  90. return &StructInjector{Has: false}
  91. }
  92. return MakeStructInjector(
  93. ValueOf(s),
  94. d.hijacker,
  95. d.goodFunc,
  96. d.Values.CloneWithFieldsOf(s)...,
  97. )
  98. }
  99. // Func is being used to return a new injector based on
  100. // a function, if it contains input arguments that the types of those
  101. // are matching with one or more of the `Values` then they are binded
  102. // to the function's input argument when called
  103. // with the injector's `Inject` method.
  104. func (d *D) Func(fn interface{}) *FuncInjector {
  105. if fn == nil {
  106. return &FuncInjector{Has: false}
  107. }
  108. return MakeFuncInjector(
  109. ValueOf(fn),
  110. d.hijacker,
  111. d.goodFunc,
  112. d.Values...,
  113. )
  114. }