This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 183
/
structs.go
78 lines (65 loc) · 1.88 KB
/
structs.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
69
70
71
72
73
74
75
76
77
78
package selfdescribe
import (
"reflect"
"strings"
)
// Embedded structs to always ignore since they already provided in other
// places in the output.
var excludedFields = map[string]bool{
"MonitorConfig": true,
"ObserverConfig": true,
"OtherConfig": true,
}
// This will have to change if we start pulling in monitors from other repos
func packageDirOfType(t reflect.Type) string {
return strings.TrimPrefix(t.PkgPath(), "github.com/signalfx/signalfx-agent/")
}
func getStructMetadata(typ reflect.Type) structMetadata {
packageDir := packageDirOfType(typ)
structName := typ.Name()
if packageDir == "" || structName == "" {
return structMetadata{}
}
fieldMD := []fieldMetadata{}
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
if excludedFields[f.Name] {
continue
}
if f.Anonymous {
nestedSM := getStructMetadata(f.Type)
fieldMD = append(fieldMD, nestedSM.Fields...)
continue
// Embedded struct name and doc is irrelevant.
}
yamlName := getYAMLName(f)
if (yamlName == "" || yamlName == "-" || strings.HasPrefix(yamlName, "_")) && !isInlinedYAML(f) {
continue
}
fm := fieldMetadata{
YAMLName: yamlName,
Doc: structFieldDocs(packageDir, structName)[f.Name],
Default: getDefault(f),
Required: getRequired(f),
Type: indirectKind(f.Type).String(),
}
if indirectKind(f.Type) == reflect.Struct {
smd := getStructMetadata(indirectType(f.Type))
fm.ElementStruct = &smd
} else if f.Type.Kind() == reflect.Map || f.Type.Kind() == reflect.Slice {
ikind := indirectKind(f.Type.Elem())
fm.ElementKind = ikind.String()
if ikind == reflect.Struct {
smd := getStructMetadata(indirectType(f.Type.Elem()))
fm.ElementStruct = &smd
}
}
fieldMD = append(fieldMD, fm)
}
return structMetadata{
Name: structName,
Doc: structDoc(packageDir, structName),
Package: packageDir,
Fields: fieldMD,
}
}