-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_struct_fields.go
100 lines (88 loc) · 2.2 KB
/
get_struct_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
package structs
import (
"reflect"
"strings"
)
func GetStructFields(structure any, parent *Field) ([]Field, error) {
val := reflect.ValueOf(structure)
if val.Kind() != reflect.Ptr {
return nil, ErrInputPointer
}
if val.Elem().Kind() != reflect.Struct {
return nil, ErrInputPointerStruct
}
fields := make([]Field, 0)
val = val.Elem()
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
fieldValue := val.Field(i)
tags := parseTags(string(field.Tag))
f := NewField(field.Name, field.Type.Kind(), fieldValue, tags, parent)
if field.Type.Kind() == reflect.Struct {
nestedFields, err := GetStructFields(val.Field(i).Addr().Interface(), &f)
if err != nil {
return nil, err
}
for j := range nestedFields {
nestedFields[j].Parent = &f
nestedFields[j].FQN = nestedFields[j].buildFQN()
}
f.Fields = nestedFields
fields = append(fields, f)
} else {
fields = append(fields, f)
}
}
return fields, nil
}
// parseTags extracts inputs and their inputs from a given line of text
// arg:"cwd" short:"c" help:"Current working directory"
// The function returns a map: {"arg": "cwd", "short": "c", "help": "Current working directory"}
func parseTags(line string) map[string]string {
line = strings.TrimSpace(line)
result := make(map[string]string)
inTag := true
lastTagName := ""
inValue := false
lastTagValue := ""
for i, char := range line {
switch {
case char == ':':
if inValue {
lastTagValue += string(char)
continue
}
inTag = false
continue
case char == '"':
// allow escaping quotes
if i > 0 && line[i-1] == '\\' {
lastTagValue = lastTagValue[:len(lastTagValue)-1] + string(char)
continue
}
// entering or exiting a tag value
inValue = !inValue
if inValue {
continue
} else {
// exiting a tag value
lastTagName = strings.TrimSpace(lastTagName)
lastTagValue = strings.TrimSpace(lastTagValue)
result[lastTagName] = lastTagValue
lastTagName = ""
lastTagValue = ""
inTag = true
}
default:
// Collect characters for the tag name or value
if inTag {
lastTagName += string(char)
}
if inValue {
lastTagValue += string(char)
}
}
}
return result
}