-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.go
More file actions
209 lines (164 loc) · 3.82 KB
/
Copy pathconvert.go
File metadata and controls
209 lines (164 loc) · 3.82 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package structify
import (
"fmt"
"reflect"
)
// Map converts a struct to a map[string]any. Convenience wrapper.
func Map(s any) map[string]any {
return New(s).Map()
}
// Values returns the field values of a struct as []any. Convenience wrapper.
func Values(s any) []any {
return New(s).Values()
}
// Names returns the field names of a struct. Convenience wrapper.
func Names(s any) []string {
return New(s).Names()
}
// Fields returns the fields of a struct. Convenience wrapper.
func Fields(s any) []*Field {
return New(s).Fields()
}
// IsZero returns true if all exported fields of the struct are zero-valued.
func IsZero(s any) bool {
return New(s).IsZero()
}
// HasZero returns true if any exported field of the struct is zero-valued.
func HasZero(s any) bool {
return New(s).HasZero()
}
// IsStruct returns true if the given value is a struct or pointer to struct.
func IsStruct(s any) bool {
v := reflect.ValueOf(s)
for v.Kind() == reflect.Ptr {
if v.IsNil() {
return false
}
v = v.Elem()
}
return v.Kind() == reflect.Struct
}
// Name returns the struct type name. Convenience wrapper.
func Name(s any) string {
return New(s).Name()
}
// structToMap converts a struct reflect.Value to a map[string]any.
func structToMap(val reflect.Value, tagName string) map[string]any {
t := val.Type()
out := make(map[string]any)
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
fv := val.Field(i)
// Skip unexported non-embedded fields
if !sf.IsExported() && !sf.Anonymous {
continue
}
opts := parseTag(sf.Tag.Get(tagName))
// Skip fields tagged with "-"
if opts.skip {
continue
}
// Determine map key
key := sf.Name
if opts.name != "" {
key = opts.name
}
// Handle omitempty
if opts.omitempty && fv.IsZero() {
continue
}
// Handle string option — format as string
if opts.asString {
out[key] = fmt.Sprintf("%v", fv.Interface())
continue
}
// Resolve pointers
v := fv
for v.Kind() == reflect.Ptr {
if v.IsNil() {
break
}
v = v.Elem()
}
// Handle nested structs
if v.Kind() == reflect.Struct && !opts.omitnested {
out[key] = structToMap(v, tagName)
continue
}
out[key] = fv.Interface()
}
return out
}
// structValues extracts field values from a struct.
func structValues(val reflect.Value, tagName string) []any {
t := val.Type()
values := make([]any, 0, t.NumField())
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
fv := val.Field(i)
if !sf.IsExported() && !sf.Anonymous {
continue
}
opts := parseTag(sf.Tag.Get(tagName))
if opts.skip {
continue
}
values = append(values, fv.Interface())
}
return values
}
// structNames extracts field names from a struct.
func structNames(val reflect.Value, tagName string) []string {
t := val.Type()
names := make([]string, 0, t.NumField())
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
if !sf.IsExported() && !sf.Anonymous {
continue
}
opts := parseTag(sf.Tag.Get(tagName))
if opts.skip {
continue
}
names = append(names, sf.Name)
}
return names
}
// isZero returns true if all exported fields are zero-valued.
func isZero(val reflect.Value, tagName string) bool {
t := val.Type()
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
fv := val.Field(i)
if !sf.IsExported() && !sf.Anonymous {
continue
}
opts := parseTag(sf.Tag.Get(tagName))
if opts.skip {
continue
}
if !fv.IsZero() {
return false
}
}
return true
}
// hasZero returns true if any exported field is zero-valued.
func hasZero(val reflect.Value, tagName string) bool {
t := val.Type()
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
fv := val.Field(i)
if !sf.IsExported() && !sf.Anonymous {
continue
}
opts := parseTag(sf.Tag.Get(tagName))
if opts.skip {
continue
}
if fv.IsZero() {
return true
}
}
return false
}