-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
89 lines (71 loc) · 1.69 KB
/
parser.go
File metadata and controls
89 lines (71 loc) · 1.69 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package di
import (
"reflect"
"github.com/pkg/errors"
)
type Struct struct {
typePkg string
typeName string
fullType string
fields []Field
}
type Field struct {
name string
typePkg string
typeName string
fullType string
}
func (s Struct) TypePkg() string {
return s.typePkg
}
func (s Struct) TypeName() string {
return s.typeName
}
func (s Struct) FullType() string {
return s.fullType
}
func (c *container) parseStruct(tp reflect.Type) (*Struct, error) {
if tp == nil {
return nil, errors.New("tp must be not nil")
}
if (tp.Kind() != reflect.Pointer || tp.Elem().Kind() != reflect.Struct) &&
(tp.Kind() != reflect.Pointer || tp.Elem().Kind() != reflect.Interface) &&
tp.Kind() != reflect.Struct &&
tp.Kind() != reflect.Interface {
return nil, errors.New("tp must be a struct | interface, or a pointer of struct | interface")
}
if tp.Kind() == reflect.Pointer {
tp = tp.Elem()
}
s := &Struct{
typePkg: tp.PkgPath(),
typeName: tp.Name(),
fields: []Field{},
}
s.fullType = s.typePkg + ":" + s.typeName
if tp.Kind() == reflect.Struct {
if err := c.parseFields(s, tp); err != nil {
return nil, err
}
}
return s, nil
}
func (c *container) parseFields(s *Struct, tp reflect.Type) error {
for i := 0; i < tp.NumField(); i++ {
field := tp.Field(i)
if tp.Kind() != reflect.Interface && (tp.Kind() != reflect.Pointer || tp.Elem().Kind() != reflect.Struct) {
continue
}
if _, ok := field.Tag.Lookup("inject"); !ok {
continue
}
f := Field{
name: field.Name,
typePkg: field.Type.PkgPath(),
typeName: field.Type.Name(),
fullType: field.Type.PkgPath() + ":" + field.Type.Name(),
}
s.fields = append(s.fields, f)
}
return nil
}