-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfield.go
56 lines (45 loc) · 1.09 KB
/
field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package zconfig
import (
"fmt"
"reflect"
)
const (
TagInjectAs = "inject-as"
TagInject = "inject"
TagKey = "key"
TagDefault = "default"
TagDescription = "description"
)
type Field struct {
Value reflect.Value
Path string
Anonymous bool
Tags reflect.StructTag
Parent *Field
Children []*Field
Key string
Provider string
Configurable bool
ConfigurationKey string
}
func (f *Field) Inject(s *Field) (err error) {
if !s.Value.Type().AssignableTo(f.Value.Type()) {
return fmt.Errorf("cannot inject %s into %s for field %s", s.Value.Type(), f.Value.Type(), f.Path)
}
if !f.Value.CanSet() {
return fmt.Errorf("cannot address %s for injection", f.Value.Type())
}
f.Value.Set(s.Value)
return nil
}
func (f *Field) IsLeaf() bool {
if _, ok := f.Tags.Lookup(TagInject); ok {
return true
}
// The field is a leaf if it is a type different than a struct or a
// pointer to a struct.
if f.Value.Kind() != reflect.Ptr {
return f.Value.Kind() != reflect.Struct
}
return f.Value.Type().Elem().Kind() != reflect.Struct
}