1
2
3
4
5 package xml
6
7 import (
8 "fmt"
9 "reflect"
10 "strings"
11 "sync"
12 )
13
14
15 type typeInfo struct {
16 xmlname *fieldInfo
17 fields []fieldInfo
18 }
19
20
21 type fieldInfo struct {
22 idx []int
23 name string
24 xmlns string
25 flags fieldFlags
26 parents []string
27 }
28
29 type fieldFlags int
30
31 const (
32 fElement fieldFlags = 1 << iota
33 fAttr
34 fCDATA
35 fCharData
36 fInnerXML
37 fComment
38 fAny
39
40 fOmitEmpty
41
42 fMode = fElement | fAttr | fCDATA | fCharData | fInnerXML | fComment | fAny
43
44 xmlName = "XMLName"
45 )
46
47 var tinfoMap sync.Map
48
49 var nameType = reflect.TypeFor[Name]()
50
51
52
53 func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
54 if ti, ok := tinfoMap.Load(typ); ok {
55 return ti.(*typeInfo), nil
56 }
57
58 tinfo := &typeInfo{}
59 if typ.Kind() == reflect.Struct && typ != nameType {
60 n := typ.NumField()
61 for i := 0; i < n; i++ {
62 f := typ.Field(i)
63 if (!f.IsExported() && !f.Anonymous) || f.Tag.Get("xml") == "-" {
64 continue
65 }
66
67
68 if f.Anonymous {
69 t := f.Type
70 if t.Kind() == reflect.Pointer {
71 t = t.Elem()
72 }
73 if t.Kind() == reflect.Struct {
74 inner, err := getTypeInfo(t)
75 if err != nil {
76 return nil, err
77 }
78 if tinfo.xmlname == nil {
79 tinfo.xmlname = inner.xmlname
80 }
81 for _, finfo := range inner.fields {
82 finfo.idx = append([]int{i}, finfo.idx...)
83 if err := addFieldInfo(typ, tinfo, &finfo); err != nil {
84 return nil, err
85 }
86 }
87 continue
88 }
89 }
90
91 finfo, err := structFieldInfo(typ, &f)
92 if err != nil {
93 return nil, err
94 }
95
96 if f.Name == xmlName {
97 tinfo.xmlname = finfo
98 continue
99 }
100
101
102 if err := addFieldInfo(typ, tinfo, finfo); err != nil {
103 return nil, err
104 }
105 }
106 }
107
108 ti, _ := tinfoMap.LoadOrStore(typ, tinfo)
109 return ti.(*typeInfo), nil
110 }
111
112
113 func structFieldInfo(typ reflect.Type, f *reflect.StructField) (*fieldInfo, error) {
114 finfo := &fieldInfo{idx: f.Index}
115
116
117 tag := f.Tag.Get("xml")
118 if ns, t, ok := strings.Cut(tag, " "); ok {
119 finfo.xmlns, tag = ns, t
120 }
121
122
123 tokens := strings.Split(tag, ",")
124 if len(tokens) == 1 {
125 finfo.flags = fElement
126 } else {
127 tag = tokens[0]
128 for _, flag := range tokens[1:] {
129 switch flag {
130 case "attr":
131 finfo.flags |= fAttr
132 case "cdata":
133 finfo.flags |= fCDATA
134 case "chardata":
135 finfo.flags |= fCharData
136 case "innerxml":
137 finfo.flags |= fInnerXML
138 case "comment":
139 finfo.flags |= fComment
140 case "any":
141 finfo.flags |= fAny
142 case "omitempty":
143 finfo.flags |= fOmitEmpty
144 }
145 }
146
147
148 valid := true
149 switch mode := finfo.flags & fMode; mode {
150 case 0:
151 finfo.flags |= fElement
152 case fAttr, fCDATA, fCharData, fInnerXML, fComment, fAny, fAny | fAttr:
153 if f.Name == xmlName || tag != "" && mode != fAttr {
154 valid = false
155 }
156 default:
157
158 valid = false
159 }
160 if finfo.flags&fMode == fAny {
161 finfo.flags |= fElement
162 }
163 if finfo.flags&fOmitEmpty != 0 && finfo.flags&(fElement|fAttr) == 0 {
164 valid = false
165 }
166 if !valid {
167 return nil, fmt.Errorf("xml: invalid tag in field %s of type %s: %q",
168 f.Name, typ, f.Tag.Get("xml"))
169 }
170 }
171
172
173 if finfo.xmlns != "" && tag == "" {
174 return nil, fmt.Errorf("xml: namespace without name in field %s of type %s: %q",
175 f.Name, typ, f.Tag.Get("xml"))
176 }
177
178 if f.Name == xmlName {
179
180
181
182 finfo.name = tag
183 return finfo, nil
184 }
185
186 if tag == "" {
187
188
189
190 if xmlname := lookupXMLName(f.Type); xmlname != nil {
191 finfo.xmlns, finfo.name = xmlname.xmlns, xmlname.name
192 } else {
193 finfo.name = f.Name
194 }
195 return finfo, nil
196 }
197
198
199 parents := strings.Split(tag, ">")
200 if parents[0] == "" {
201 parents[0] = f.Name
202 }
203 if parents[len(parents)-1] == "" {
204 return nil, fmt.Errorf("xml: trailing '>' in field %s of type %s", f.Name, typ)
205 }
206 finfo.name = parents[len(parents)-1]
207 if len(parents) > 1 {
208 if (finfo.flags & fElement) == 0 {
209 return nil, fmt.Errorf("xml: %s chain not valid with %s flag", tag, strings.Join(tokens[1:], ","))
210 }
211 finfo.parents = parents[:len(parents)-1]
212 }
213
214
215
216
217 if finfo.flags&fElement != 0 {
218 ftyp := f.Type
219 xmlname := lookupXMLName(ftyp)
220 if xmlname != nil && xmlname.name != finfo.name {
221 return nil, fmt.Errorf("xml: name %q in tag of %s.%s conflicts with name %q in %s.XMLName",
222 finfo.name, typ, f.Name, xmlname.name, ftyp)
223 }
224 }
225 return finfo, nil
226 }
227
228
229
230
231 func lookupXMLName(typ reflect.Type) (xmlname *fieldInfo) {
232 for typ.Kind() == reflect.Pointer {
233 typ = typ.Elem()
234 }
235 if typ.Kind() != reflect.Struct {
236 return nil
237 }
238 for i, n := 0, typ.NumField(); i < n; i++ {
239 f := typ.Field(i)
240 if f.Name != xmlName {
241 continue
242 }
243 finfo, err := structFieldInfo(typ, &f)
244 if err == nil && finfo.name != "" {
245 return finfo
246 }
247
248
249 break
250 }
251 return nil
252 }
253
254
255
256
257
258
259
260
261 func addFieldInfo(typ reflect.Type, tinfo *typeInfo, newf *fieldInfo) error {
262 var conflicts []int
263 Loop:
264
265 for i := range tinfo.fields {
266 oldf := &tinfo.fields[i]
267 if oldf.flags&fMode != newf.flags&fMode {
268 continue
269 }
270 if oldf.xmlns != "" && newf.xmlns != "" && oldf.xmlns != newf.xmlns {
271 continue
272 }
273 minl := min(len(newf.parents), len(oldf.parents))
274 for p := 0; p < minl; p++ {
275 if oldf.parents[p] != newf.parents[p] {
276 continue Loop
277 }
278 }
279 if len(oldf.parents) > len(newf.parents) {
280 if oldf.parents[len(newf.parents)] == newf.name {
281 conflicts = append(conflicts, i)
282 }
283 } else if len(oldf.parents) < len(newf.parents) {
284 if newf.parents[len(oldf.parents)] == oldf.name {
285 conflicts = append(conflicts, i)
286 }
287 } else {
288 if newf.name == oldf.name && newf.xmlns == oldf.xmlns {
289 conflicts = append(conflicts, i)
290 }
291 }
292 }
293
294 if conflicts == nil {
295 tinfo.fields = append(tinfo.fields, *newf)
296 return nil
297 }
298
299
300
301 for _, i := range conflicts {
302 if len(tinfo.fields[i].idx) < len(newf.idx) {
303 return nil
304 }
305 }
306
307
308 for _, i := range conflicts {
309 oldf := &tinfo.fields[i]
310 if len(oldf.idx) == len(newf.idx) {
311 f1 := typ.FieldByIndex(oldf.idx)
312 f2 := typ.FieldByIndex(newf.idx)
313 return &TagPathError{typ, f1.Name, f1.Tag.Get("xml"), f2.Name, f2.Tag.Get("xml")}
314 }
315 }
316
317
318
319 for c := len(conflicts) - 1; c >= 0; c-- {
320 i := conflicts[c]
321 copy(tinfo.fields[i:], tinfo.fields[i+1:])
322 tinfo.fields = tinfo.fields[:len(tinfo.fields)-1]
323 }
324 tinfo.fields = append(tinfo.fields, *newf)
325 return nil
326 }
327
328
329
330 type TagPathError struct {
331 Struct reflect.Type
332 Field1, Tag1 string
333 Field2, Tag2 string
334 }
335
336 func (e *TagPathError) Error() string {
337 return fmt.Sprintf("%s field %q with tag %q conflicts with field %q with tag %q", e.Struct, e.Field1, e.Tag1, e.Field2, e.Tag2)
338 }
339
340 const (
341 initNilPointers = true
342 dontInitNilPointers = false
343 )
344
345
346
347
348
349
350 func (finfo *fieldInfo) value(v reflect.Value, shouldInitNilPointers bool) reflect.Value {
351 for i, x := range finfo.idx {
352 if i > 0 {
353 t := v.Type()
354 if t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.Struct {
355 if v.IsNil() {
356 if !shouldInitNilPointers {
357 return reflect.Value{}
358 }
359 v.Set(reflect.New(v.Type().Elem()))
360 }
361 v = v.Elem()
362 }
363 }
364 v = v.Field(x)
365 }
366 return v
367 }
368
View as plain text