-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag.go
More file actions
237 lines (187 loc) · 5.03 KB
/
tag.go
File metadata and controls
237 lines (187 loc) · 5.03 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2025 Codnect
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tag
import (
"fmt"
"reflect"
"strings"
)
const DefaultOptionName = "value"
// Tagger represents a struct tag.
type Tagger interface {
// Tag returns the name of the tag
Tag() string
}
// Parse parses the struct tag string and populates the fields.
func Parse[T Tagger](raw string, target T) error {
if raw == "" {
return nil
}
rTargetVal := reflect.ValueOf(target)
if rTargetVal.Kind() != reflect.Ptr || rTargetVal.Elem().Kind() != reflect.Struct {
return fmt.Errorf("tag: target must be pointer to struct, got %T", target)
}
for raw != "" {
skip := skipSpaces(raw)
raw = raw[skip:]
if raw == "" {
break
}
i := 0
for i < len(raw) && raw[i] > ' ' && raw[i] != ':' && raw[i] != '"' && raw[i] != 0x7f {
i++
}
if i == 0 {
return fmt.Errorf("tag: empty tag name in '%s'", raw)
}
if i+1 >= len(raw) {
return fmt.Errorf("tag: missing tag value for tag '%s'", raw[:i])
}
if raw[i] != ':' {
return fmt.Errorf("tag: expected ':' after tag name %q in %q", raw[:i], raw)
}
name := raw[:i]
raw = raw[i+1:]
value, valueLen, err := scanString(raw, '"')
if err != nil {
return fmt.Errorf("tag: invalid tag value: %s", err)
}
if target.Tag() == name {
skip = skipSpaces(value)
opts := value[skip:]
if len(opts) == 0 {
return fmt.Errorf("tag: empty tag value for tag '%s'", name)
}
if err = parseOptions(value, rTargetVal); err != nil {
return fmt.Errorf("tag: failed to parse '%s' options %q: %w", name, value, err)
}
return nil
}
raw = raw[valueLen:]
}
return nil
}
// parseOptions parses the options and sets the struct fields accordingly.
func parseOptions(opts string, structPtr reflect.Value) error {
val, valLen, err := scanPosOptValue(opts)
if err != nil {
return err
}
if valLen <= 0 {
return fmt.Errorf("missing positional option value")
}
rFieldVal, exists := findStructField(DefaultOptionName, structPtr)
if exists {
if err = setValue(rFieldVal, val); err != nil {
return err
}
}
opts = opts[valLen:]
skip := skipSpaces(opts)
opts = opts[skip:]
if len(opts) == 0 {
return nil
}
if opts[0] != ',' {
return fmt.Errorf("missing ',' after positional option value")
} else {
opts = opts[1:]
}
for len(opts) != 0 {
val, valLen = scanIdentifier(opts)
if valLen == 0 {
return fmt.Errorf("missing option key")
}
optName := val
opts = opts[valLen:]
skip = skipSpaces(opts)
opts = opts[skip:]
if len(opts) >= 1 && opts[0] == '=' {
opts = opts[1:]
skip = skipSpaces(opts)
opts = opts[skip:]
if len(opts) == 0 {
return fmt.Errorf("missing value for option %q", optName)
}
val, valLen, err = scanOptValue(optName, opts, structPtr)
if err != nil {
return err
}
if err = setOption(optName, val, structPtr); err != nil {
return err
}
opts = opts[valLen:]
} else if len(opts) == 0 {
if err = setFlagOption(optName, structPtr); err != nil {
return err
}
break
}
skip = skipSpaces(opts)
opts = opts[skip:]
if len(opts) == 0 {
break
}
if opts[0] != ',' {
return fmt.Errorf("missing comma after option %q", optName)
} else {
opts = opts[1:]
}
}
return nil
}
// findStructField finds the struct field corresponding to the option name.
func findStructField(optName string, structPtr reflect.Value) (reflect.Value, bool) {
optName = strings.ToLower(optName)
structType := structPtr.Type().Elem()
for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
if !field.IsExported() {
continue
}
optionTag, ok := field.Tag.Lookup("option")
if !ok {
continue
}
optionTag = strings.ToLower(optionTag)
if optionTag != optName {
continue
}
fieldVal := structPtr.Elem().Field(i)
return fieldVal, true
}
return reflect.Value{}, false
}
// setFlagOption sets a boolean flag option in the struct.
func setFlagOption(name string, structPtr reflect.Value) error {
rFieldVal, exists := findStructField(name, structPtr)
if !exists {
return nil
}
if rFieldVal.Kind() != reflect.Bool {
return fmt.Errorf("invalid type for flag option %q: want bool", name)
}
return setOption(name, "true", structPtr)
}
// setOption sets an option with a value in the struct.
func setOption(name string, value string, structPtr reflect.Value) error {
rFieldVal, exists := findStructField(name, structPtr)
if !exists {
return nil
}
if err := setValue(rFieldVal, value); err != nil {
return fmt.Errorf("failed to set option %q: %w", name, err)
}
return nil
}