values.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package di
  2. import "reflect"
  3. // Values is a shortcut of []reflect.Value,
  4. // it makes easier to remove and add dependencies.
  5. type Values []reflect.Value
  6. // NewValues returns new empty (dependencies) values.
  7. func NewValues() Values {
  8. return Values{}
  9. }
  10. // Clone returns a copy of the current values.
  11. func (bv Values) Clone() Values {
  12. if n := len(bv); n > 0 {
  13. values := make(Values, n, n)
  14. copy(values, bv)
  15. return values
  16. }
  17. return NewValues()
  18. }
  19. // CloneWithFieldsOf will return a copy of the current values
  20. // plus the "s" struct's fields that are filled(non-zero) by the caller.
  21. func (bv Values) CloneWithFieldsOf(s interface{}) Values {
  22. values := bv.Clone()
  23. // add the manual filled fields to the dependencies.
  24. filledFieldValues := LookupNonZeroFieldsValues(ValueOf(s), true)
  25. values = append(values, filledFieldValues...)
  26. return values
  27. }
  28. // Len returns the length of the current "bv" values slice.
  29. func (bv Values) Len() int {
  30. return len(bv)
  31. }
  32. // Add adds values as dependencies, if the struct's fields
  33. // or the function's input arguments needs them, they will be defined as
  34. // bindings (at build-time) and they will be used (at serve-time).
  35. func (bv *Values) Add(values ...interface{}) {
  36. bv.AddValues(ValuesOf(values)...)
  37. }
  38. // AddValues same as `Add` but accepts reflect.Value dependencies instead of interface{}
  39. // and appends them to the list if they pass some checks.
  40. func (bv *Values) AddValues(values ...reflect.Value) {
  41. for _, v := range values {
  42. if !goodVal(v) {
  43. continue
  44. }
  45. *bv = append(*bv, v)
  46. }
  47. }
  48. // Remove unbinds a binding value based on the type,
  49. // it returns true if at least one field is not binded anymore.
  50. //
  51. // The "n" indicates the number of elements to remove, if <=0 then it's 1,
  52. // this is useful because you may have bind more than one value to two or more fields
  53. // with the same type.
  54. func (bv *Values) Remove(value interface{}, n int) bool {
  55. return bv.remove(reflect.TypeOf(value), n)
  56. }
  57. func (bv *Values) remove(typ reflect.Type, n int) (ok bool) {
  58. input := *bv
  59. for i, in := range input {
  60. if equalTypes(in.Type(), typ) {
  61. ok = true
  62. input = input[:i+copy(input[i:], input[i+1:])]
  63. if n > 1 {
  64. continue
  65. }
  66. break
  67. }
  68. }
  69. *bv = input
  70. return
  71. }
  72. // Has returns true if a binder responsible to
  73. // bind and return a type of "typ" is already registered to this controller.
  74. func (bv Values) Has(value interface{}) bool {
  75. return bv.valueTypeExists(reflect.TypeOf(value))
  76. }
  77. func (bv Values) valueTypeExists(typ reflect.Type) bool {
  78. for _, in := range bv {
  79. if equalTypes(in.Type(), typ) {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. // AddOnce binds a value to the controller's field with the same type,
  86. // if it's not binded already.
  87. //
  88. // Returns false if binded already or the value is not the proper one for binding,
  89. // otherwise true.
  90. func (bv *Values) AddOnce(value interface{}) bool {
  91. return bv.addIfNotExists(reflect.ValueOf(value))
  92. }
  93. func (bv *Values) addIfNotExists(v reflect.Value) bool {
  94. var (
  95. typ = v.Type() // no element, raw things here.
  96. )
  97. if !goodVal(v) {
  98. return false
  99. }
  100. if bv.valueTypeExists(typ) {
  101. return false
  102. }
  103. bv.Add(v)
  104. return true
  105. }