-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph_node_dependency.go
68 lines (47 loc) · 1.16 KB
/
graph_node_dependency.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
57
58
59
60
61
62
63
64
65
66
67
68
package inj
import (
"reflect"
"strings"
)
type graphNodeDependency struct {
DatasourcePaths []string
Path structPath
Type reflect.Type
}
func findDependencies(t reflect.Type, deps *[]graphNodeDependency, path *structPath) error {
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
// Ignore unpexported fields, regardless of tags
if f.PkgPath != "" {
continue
}
// Get all tags
tag := f.Tag
// Generate a struct path branch
branch := path.Branch(f.Name)
// Ignore tags that don't have injection deps
if !strings.Contains(string(tag), "inj:") {
if f.Type.Kind() == reflect.Struct {
// Recurse
findDependencies(f.Type, deps, &branch)
}
continue
}
// Assemble everything we know about the dependency
dep := parseStructTag(tag)
// Add the path in the struct
dep.Path = branch
// We also know the type
dep.Type = f.Type
// Add the dependency
*deps = append(*deps, dep)
}
return nil
}
func parseStructTag(t reflect.StructTag) (d graphNodeDependency) {
parts := strings.Split(t.Get("inj"), ",")
if len(parts) > 0 && len(parts[0]) > 0 {
d.DatasourcePaths = parts
}
return
}