/
fields.go
158 lines (147 loc) · 3.75 KB
/
fields.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package configschema // import "github.com/open-telemetry/opentelemetry-collector-contrib/cmd/configschema"
import (
"log"
"reflect"
"strings"
"time"
"github.com/fatih/structtag"
)
// Field holds attributes and subfields of a config struct.
type Field struct {
Name string `yaml:",omitempty"`
Type string `yaml:",omitempty"`
Kind string `yaml:",omitempty"`
Default any `yaml:",omitempty"`
Doc string `yaml:",omitempty"`
Fields []*Field `yaml:",omitempty"`
}
// ReadFields accepts both a config struct's Value, as well as a DirResolver,
// and returns a Field pointer for the top level struct as well as all of its
// recursive subfields.
func ReadFields(v reflect.Value, dr DirResolver) (*Field, error) {
cfgType := v.Type()
field := &Field{
Type: cfgType.String(),
}
err := refl(field, v, dr)
return field, err
}
func refl(field *Field, v reflect.Value, dr DirResolver) error {
if v.Kind() == reflect.Ptr {
err := refl(field, v.Elem(), dr)
if err != nil {
return err
}
}
if v.Kind() != reflect.Struct {
return nil
}
comments, err := commentsForStruct(v, dr)
if err != nil {
return err
}
// _struct comments are those that are on the struct type itself. Here we check
// if field.Doc is empty, thus preventing a squashed type with struct comments
// from overwriting the containing struct's comments.
if sc, ok := comments["_struct"]; ok && field.Doc == "" {
field.Doc = sc
}
for i := 0; i < v.NumField(); i++ {
structField := v.Type().Field(i)
if !structField.IsExported() {
continue
}
tagName, options, err := mapstructure(structField.Tag)
if err != nil {
log.Printf("error parsing mapstructure tag for type: %s: %s: %v", field.Type, structField.Tag, err)
// not fatal, can keep going
}
if tagName == "-" {
continue
}
fv := v.Field(i)
next := field
if !containsSquash(options) {
name := tagName
if name == "" {
name = strings.ToLower(structField.Name)
}
kindStr := fv.Kind().String()
typeStr := fv.Type().String()
if typeStr == kindStr {
typeStr = "" // omit if redundant
}
next = &Field{
Name: name,
Type: typeStr,
Kind: kindStr,
Doc: comments[structField.Name],
}
field.Fields = append(field.Fields, next)
}
err = handleKind(fv, next, dr)
if err != nil {
return err
}
}
return nil
}
func handleKind(v reflect.Value, f *Field, dr DirResolver) (err error) {
switch v.Kind() {
case reflect.Struct:
err = refl(f, v, dr)
case reflect.Ptr:
if v.IsNil() {
err = refl(f, reflect.New(v.Type().Elem()), dr)
} else {
err = refl(f, v.Elem(), dr)
}
case reflect.Slice:
e := v.Type().Elem()
if e.Kind() == reflect.Struct {
err = refl(f, reflect.New(e), dr)
} else if e.Kind() == reflect.Ptr {
err = refl(f, reflect.New(e.Elem()), dr)
}
case reflect.String:
f.Default = v.String()
case reflect.Bool:
f.Default = v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Int() != 0 {
if v.Type() == reflect.TypeOf(time.Duration(0)) {
f.Default = time.Duration(v.Int()).String()
} else {
f.Default = v.Int()
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
f.Default = v.Uint()
}
return
}
func mapstructure(st reflect.StructTag) (string, []string, error) {
tag := string(st)
if tag == "" {
return "", nil, nil
}
tags, err := structtag.Parse(tag)
if err != nil {
return "", nil, err
}
ms, err := tags.Get("mapstructure")
if err != nil {
return "", nil, err
}
return ms.Name, ms.Options, nil
}
func containsSquash(options []string) bool {
for _, option := range options {
if option == "squash" {
return true
}
}
return false
}