Debian Codegraph @

pkg/internal/third_party/go-json-experiment/json/fields_test.go

raw
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package json
6
7 import (
8 "encoding"
9 "errors"
10 "reflect"
11 "testing"
12
13 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsontest"
14 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext"
15 )
16
17 type unexported struct{}
18
19 func TestMakeStructFields(t *testing.T) {
20 type Embed struct {
21 Foo string
22 }
23 type Recursive struct {
24 A string
25 *Recursive `json:",inline"`
26 B string
27 }
28 type MapStringAny map[string]any
29 tests := []struct {
30 name jsontest.CaseName
31 in any
32 want structFields
33 wantErr error
34 }{{
35 name: jsontest.Name("Names"),
36 in: struct {
37 F1 string
38 F2 string `json:"-"`
39 F3 string `json:"json_name"`
40 f3 string
41 F5 string `json:"json_name_nocase,case:ignore"`
42 }{},
43 want: structFields{
44 flattened: []structField{
45 {id: 0, index: []int{0}, typ: stringType, fieldOptions: fieldOptions{name: "F1", quotedName: `"F1"`}},
46 {id: 1, index: []int{2}, typ: stringType, fieldOptions: fieldOptions{name: "json_name", quotedName: `"json_name"`, hasName: true}},
47 {id: 2, index: []int{4}, typ: stringType, fieldOptions: fieldOptions{name: "json_name_nocase", quotedName: `"json_name_nocase"`, hasName: true, casing: caseIgnore}},
48 },
49 },
50 }, {
51 name: jsontest.Name("BreadthFirstSearch"),
52 in: struct {
53 L1A string
54 L1B struct {
55 L2A string
56 L2B struct {
57 L3A string
58 } `json:",inline"`
59 L2C string
60 } `json:",inline"`
61 L1C string
62 L1D struct {
63 L2D string
64 L2E struct {
65 L3B string
66 } `json:",inline"`
67 L2F string
68 } `json:",inline"`
69 L1E string
70 }{},
71 want: structFields{
72 flattened: []structField{
73 {id: 0, index: []int{0}, typ: stringType, fieldOptions: fieldOptions{name: "L1A", quotedName: `"L1A"`}},
74 {id: 3, index: []int{1, 0}, typ: stringType, fieldOptions: fieldOptions{name: "L2A", quotedName: `"L2A"`}},
75 {id: 7, index: []int{1, 1, 0}, typ: stringType, fieldOptions: fieldOptions{name: "L3A", quotedName: `"L3A"`}},
76 {id: 4, index: []int{1, 2}, typ: stringType, fieldOptions: fieldOptions{name: "L2C", quotedName: `"L2C"`}},
77 {id: 1, index: []int{2}, typ: stringType, fieldOptions: fieldOptions{name: "L1C", quotedName: `"L1C"`}},
78 {id: 5, index: []int{3, 0}, typ: stringType, fieldOptions: fieldOptions{name: "L2D", quotedName: `"L2D"`}},
79 {id: 8, index: []int{3, 1, 0}, typ: stringType, fieldOptions: fieldOptions{name: "L3B", quotedName: `"L3B"`}},
80 {id: 6, index: []int{3, 2}, typ: stringType, fieldOptions: fieldOptions{name: "L2F", quotedName: `"L2F"`}},
81 {id: 2, index: []int{4}, typ: stringType, fieldOptions: fieldOptions{name: "L1E", quotedName: `"L1E"`}},
82 },
83 },
84 }, {
85 name: jsontest.Name("NameResolution"),
86 in: struct {
87 X1 struct {
88 X struct {
89 A string // loses in precedence to A
90 B string // cancels out with X2.X.B
91 D string // loses in precedence to D
92 } `json:",inline"`
93 } `json:",inline"`
94 X2 struct {
95 X struct {
96 B string // cancels out with X1.X.B
97 C string
98 D string // loses in precedence to D
99 } `json:",inline"`
100 } `json:",inline"`
101 A string // takes precedence over X1.X.A
102 D string // takes precedence over X1.X.D and X2.X.D
103 }{},
104 want: structFields{
105 flattened: []structField{
106 {id: 2, index: []int{1, 0, 1}, typ: stringType, fieldOptions: fieldOptions{name: "C", quotedName: `"C"`}},
107 {id: 0, index: []int{2}, typ: stringType, fieldOptions: fieldOptions{name: "A", quotedName: `"A"`}},
108 {id: 1, index: []int{3}, typ: stringType, fieldOptions: fieldOptions{name: "D", quotedName: `"D"`}},
109 },
110 },
111 }, {
112 name: jsontest.Name("NameResolution/ExplicitNameUniquePrecedence"),
113 in: struct {
114 X1 struct {
115 A string // loses in precedence to X2.A
116 } `json:",inline"`
117 X2 struct {
118 A string `json:"A"`
119 } `json:",inline"`
120 X3 struct {
121 A string // loses in precedence to X2.A
122 } `json:",inline"`
123 }{},
124 want: structFields{
125 flattened: []structField{
126 {id: 0, index: []int{1, 0}, typ: stringType, fieldOptions: fieldOptions{hasName: true, name: "A", quotedName: `"A"`}},
127 },
128 },
129 }, {
130 name: jsontest.Name("NameResolution/ExplicitNameCancelsOut"),
131 in: struct {
132 X1 struct {
133 A string // loses in precedence to X2.A or X3.A
134 } `json:",inline"`
135 X2 struct {
136 A string `json:"A"` // cancels out with X3.A
137 } `json:",inline"`
138 X3 struct {
139 A string `json:"A"` // cancels out with X2.A
140 } `json:",inline"`
141 }{},
142 want: structFields{flattened: []structField{}},
143 }, {
144 name: jsontest.Name("Embed/Implicit"),
145 in: struct {
146 Embed
147 }{},
148 want: structFields{
149 flattened: []structField{
150 {id: 0, index: []int{0, 0}, typ: stringType, fieldOptions: fieldOptions{name: "Foo", quotedName: `"Foo"`}},
151 },
152 },
153 }, {
154 name: jsontest.Name("Embed/Explicit"),
155 in: struct {
156 Embed `json:",inline"`
157 }{},
158 want: structFields{
159 flattened: []structField{
160 {id: 0, index: []int{0, 0}, typ: stringType, fieldOptions: fieldOptions{name: "Foo", quotedName: `"Foo"`}},
161 },
162 },
163 }, {
164 name: jsontest.Name("Recursive"),
165 in: struct {
166 A string
167 Recursive `json:",inline"`
168 C string
169 }{},
170 want: structFields{
171 flattened: []structField{
172 {id: 0, index: []int{0}, typ: stringType, fieldOptions: fieldOptions{name: "A", quotedName: `"A"`}},
173 {id: 2, index: []int{1, 2}, typ: stringType, fieldOptions: fieldOptions{name: "B", quotedName: `"B"`}},
174 {id: 1, index: []int{2}, typ: stringType, fieldOptions: fieldOptions{name: "C", quotedName: `"C"`}},
175 },
176 },
177 }, {
178 name: jsontest.Name("InlinedFallback/Cancelation"),
179 in: struct {
180 X1 struct {
181 X jsontext.Value `json:",inline"`
182 } `json:",inline"`
183 X2 struct {
184 X map[string]any `json:",unknown"`
185 } `json:",inline"`
186 }{},
187 want: structFields{},
188 }, {
189 name: jsontest.Name("InlinedFallback/Precedence"),
190 in: struct {
191 X1 struct {
192 X jsontext.Value `json:",inline"`
193 } `json:",inline"`
194 X2 struct {
195 X map[string]any `json:",unknown"`
196 } `json:",inline"`
197 X map[string]jsontext.Value `json:",unknown"`
198 }{},
199 want: structFields{
200 inlinedFallback: &structField{id: 0, index: []int{2}, typ: T[map[string]jsontext.Value](), fieldOptions: fieldOptions{name: "X", quotedName: `"X"`, unknown: true}},
201 },
202 }, {
203 name: jsontest.Name("InlinedFallback/InvalidImplicit"),
204 in: struct {
205 MapStringAny
206 }{},
207 want: structFields{
208 flattened: []structField{
209 {id: 0, index: []int{0}, typ: reflect.TypeOf(MapStringAny(nil)), fieldOptions: fieldOptions{name: "MapStringAny", quotedName: `"MapStringAny"`}},
210 },
211 },
212 wantErr: errors.New("embedded Go struct field MapStringAny of non-struct type must be explicitly given a JSON name"),
213 }, {
214 name: jsontest.Name("InvalidUTF8"),
215 in: struct {
216 Name string `json:"'\\xde\\xad\\xbe\\xef'"`
217 }{},
218 want: structFields{
219 flattened: []structField{
220 {id: 0, index: []int{0}, typ: stringType, fieldOptions: fieldOptions{hasName: true, name: "\u07ad\ufffd\ufffd", quotedName: "\"\u07ad\ufffd\ufffd\"", nameNeedEscape: true}},
221 },
222 },
223 wantErr: errors.New(`Go struct field Name has JSON object name "ޭ\xbe\xef" with invalid UTF-8`),
224 }, {
225 name: jsontest.Name("DuplicateName"),
226 in: struct {
227 A string `json:"same"`
228 B string `json:"same"`
229 }{},
230 want: structFields{flattened: []structField{}},
231 wantErr: errors.New(`Go struct fields A and B conflict over JSON object name "same"`),
232 }, {
233 name: jsontest.Name("BothInlineAndUnknown"),
234 in: struct {
235 A struct{} `json:",inline,unknown"`
236 }{},
237 wantErr: errors.New("Go struct field A cannot have both `inline` and `unknown` specified"),
238 }, {
239 name: jsontest.Name("InlineWithOptions"),
240 in: struct {
241 A struct{} `json:",inline,omitempty"`
242 }{},
243 wantErr: errors.New("Go struct field A cannot have any options other than `inline` or `unknown` specified"),
244 }, {
245 name: jsontest.Name("UnknownWithOptions"),
246 in: struct {
247 A map[string]any `json:",inline,omitempty"`
248 }{},
249 want: structFields{inlinedFallback: &structField{
250 index: []int{0},
251 typ: reflect.TypeFor[map[string]any](),
252 fieldOptions: fieldOptions{
253 name: "A",
254 quotedName: `"A"`,
255 inline: true,
256 },
257 }},
258 wantErr: errors.New("Go struct field A cannot have any options other than `inline` or `unknown` specified"),
259 }, {
260 name: jsontest.Name("InlineTextMarshaler"),
261 in: struct {
262 A struct{ encoding.TextMarshaler } `json:",inline"`
263 }{},
264 want: structFields{flattened: []structField{{
265 index: []int{0, 0},
266 typ: reflect.TypeFor[encoding.TextMarshaler](),
267 fieldOptions: fieldOptions{
268 name: "TextMarshaler",
269 quotedName: `"TextMarshaler"`,
270 },
271 }}},
272 wantErr: errors.New(`inlined Go struct field A of type struct { encoding.TextMarshaler } must not implement marshal or unmarshal methods`),
273 }, {
274 name: jsontest.Name("InlineTextAppender"),
275 in: struct {
276 A struct{ encoding.TextAppender } `json:",inline"`
277 }{},
278 want: structFields{flattened: []structField{{
279 index: []int{0, 0},
280 typ: reflect.TypeFor[encoding.TextAppender](),
281 fieldOptions: fieldOptions{
282 name: "TextAppender",
283 quotedName: `"TextAppender"`,
284 },
285 }}},
286 wantErr: errors.New(`inlined Go struct field A of type struct { encoding.TextAppender } must not implement marshal or unmarshal methods`),
287 }, {
288 name: jsontest.Name("UnknownJSONMarshaler"),
289 in: struct {
290 A struct{ Marshaler } `json:",unknown"`
291 }{},
292 wantErr: errors.New(`inlined Go struct field A of type struct { json.Marshaler } must not implement marshal or unmarshal methods`),
293 }, {
294 name: jsontest.Name("InlineJSONMarshalerTo"),
295 in: struct {
296 A struct{ MarshalerTo } `json:",inline"`
297 }{},
298 want: structFields{flattened: []structField{{
299 index: []int{0, 0},
300 typ: reflect.TypeFor[MarshalerTo](),
301 fieldOptions: fieldOptions{
302 name: "MarshalerTo",
303 quotedName: `"MarshalerTo"`,
304 },
305 }}},
306 wantErr: errors.New(`inlined Go struct field A of type struct { json.MarshalerTo } must not implement marshal or unmarshal methods`),
307 }, {
308 name: jsontest.Name("UnknownTextUnmarshaler"),
309 in: struct {
310 A *struct{ encoding.TextUnmarshaler } `json:",unknown"`
311 }{},
312 wantErr: errors.New(`inlined Go struct field A of type struct { encoding.TextUnmarshaler } must not implement marshal or unmarshal methods`),
313 }, {
314 name: jsontest.Name("InlineJSONUnmarshaler"),
315 in: struct {
316 A *struct{ Unmarshaler } `json:",inline"`
317 }{},
318 want: structFields{flattened: []structField{{
319 index: []int{0, 0},
320 typ: reflect.TypeFor[Unmarshaler](),
321 fieldOptions: fieldOptions{
322 name: "Unmarshaler",
323 quotedName: `"Unmarshaler"`,
324 },
325 }}},
326 wantErr: errors.New(`inlined Go struct field A of type struct { json.Unmarshaler } must not implement marshal or unmarshal methods`),
327 }, {
328 name: jsontest.Name("UnknownJSONUnmarshalerFrom"),
329 in: struct {
330 A struct{ UnmarshalerFrom } `json:",unknown"`
331 }{},
332 wantErr: errors.New(`inlined Go struct field A of type struct { json.UnmarshalerFrom } must not implement marshal or unmarshal methods`),
333 }, {
334 name: jsontest.Name("UnknownStruct"),
335 in: struct {
336 A struct {
337 X, Y, Z string
338 } `json:",unknown"`
339 }{},
340 wantErr: errors.New("inlined Go struct field A of type struct { X string; Y string; Z string } with `unknown` tag must be a Go map of string key or a jsontext.Value"),
341 }, {
342 name: jsontest.Name("InlineUnsupported/MapIntKey"),
343 in: struct {
344 A map[int]any `json:",unknown"`
345 }{},
346 wantErr: errors.New(`inlined Go struct field A of type map[int]interface {} must be a Go struct, Go map of string key, or jsontext.Value`),
347 }, {
348 name: jsontest.Name("InlineUnsupported/MapTextMarshalerStringKey"),
349 in: struct {
350 A map[nocaseString]any `json:",inline"`
351 }{},
352 wantErr: errors.New(`inlined map field A of type map[json.nocaseString]interface {} must have a string key that does not implement marshal or unmarshal methods`),
353 }, {
354 name: jsontest.Name("InlineUnsupported/MapMarshalerStringKey"),
355 in: struct {
356 A map[stringMarshalEmpty]any `json:",inline"`
357 }{},
358 wantErr: errors.New(`inlined map field A of type map[json.stringMarshalEmpty]interface {} must have a string key that does not implement marshal or unmarshal methods`),
359 }, {
360 name: jsontest.Name("InlineUnsupported/DoublePointer"),
361 in: struct {
362 A **struct{} `json:",inline"`
363 }{},
364 wantErr: errors.New(`inlined Go struct field A of type *struct {} must be a Go struct, Go map of string key, or jsontext.Value`),
365 }, {
366 name: jsontest.Name("DuplicateInline"),
367 in: struct {
368 A map[string]any `json:",inline"`
369 B jsontext.Value `json:",inline"`
370 }{},
371 wantErr: errors.New(`inlined Go struct fields A and B cannot both be a Go map or jsontext.Value`),
372 }, {
373 name: jsontest.Name("DuplicateEmbedInline"),
374 in: struct {
375 A MapStringAny `json:",inline"`
376 B jsontext.Value `json:",inline"`
377 }{},
378 wantErr: errors.New(`inlined Go struct fields A and B cannot both be a Go map or jsontext.Value`),
379 }}
380
381 for _, tt := range tests {
382 t.Run(tt.name.Name, func(t *testing.T) {
383 got, err := makeStructFields(reflect.TypeOf(tt.in))
384
385 // Sanity check that pointers are consistent.
386 pointers := make(map[*structField]bool)
387 for i := range got.flattened {
388 pointers[&got.flattened[i]] = true
389 }
390 for _, f := range got.byActualName {
391 if !pointers[f] {
392 t.Errorf("%s: byActualName pointer not in flattened", tt.name.Where)
393 }
394 }
395 for _, fs := range got.byFoldedName {
396 for _, f := range fs {
397 if !pointers[f] {
398 t.Errorf("%s: byFoldedName pointer not in flattened", tt.name.Where)
399 }
400 }
401 }
402
403 // Zero out fields that are incomparable.
404 for i := range got.flattened {
405 got.flattened[i].fncs = nil
406 got.flattened[i].isEmpty = nil
407 }
408 if got.inlinedFallback != nil {
409 got.inlinedFallback.fncs = nil
410 got.inlinedFallback.isEmpty = nil
411 }
412
413 // Reproduce maps in want.
414 tt.want.byActualName = make(map[string]*structField)
415 for i := range tt.want.flattened {
416 f := &tt.want.flattened[i]
417 tt.want.byActualName[f.name] = f
418 }
419 tt.want.byFoldedName = make(map[string][]*structField)
420 for i, f := range tt.want.flattened {
421 foldedName := string(foldName([]byte(f.name)))
422 tt.want.byFoldedName[foldedName] = append(tt.want.byFoldedName[foldedName], &tt.want.flattened[i])
423 }
424
425 // Only compare underlying error to simplify test logic.
426 var gotErr error
427 if err != nil {
428 gotErr = err.Err
429 }
430
431 tt.want.reindex()
432 if !reflect.DeepEqual(got, tt.want) || !reflect.DeepEqual(gotErr, tt.wantErr) {
433 t.Errorf("%s: makeStructFields(%T):\n\tgot (%v, %v)\n\twant (%v, %v)", tt.name.Where, tt.in, got, gotErr, tt.want, tt.wantErr)
434 }
435 })
436 }
437 }
438
439 func TestParseTagOptions(t *testing.T) {
440 tests := []struct {
441 name jsontest.CaseName
442 in any // must be a struct with a single field
443 wantOpts fieldOptions
444 wantIgnored bool
445 wantErr error
446 }{{
447 name: jsontest.Name("GoName"),
448 in: struct {
449 FieldName int
450 }{},
451 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`},
452 }, {
453 name: jsontest.Name("GoNameWithOptions"),
454 in: struct {
455 FieldName int `json:",inline"`
456 }{},
457 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, inline: true},
458 }, {
459 name: jsontest.Name("Empty"),
460 in: struct {
461 V int `json:""`
462 }{},
463 wantOpts: fieldOptions{name: "V", quotedName: `"V"`},
464 }, {
465 name: jsontest.Name("Unexported"),
466 in: struct {
467 v int `json:"Hello"`
468 }{},
469 wantIgnored: true,
470 wantErr: errors.New("unexported Go struct field v cannot have non-ignored `json:\"Hello\"` tag"),
471 }, {
472 name: jsontest.Name("UnexportedEmpty"),
473 in: struct {
474 v int `json:""`
475 }{},
476 wantIgnored: true,
477 wantErr: errors.New("unexported Go struct field v cannot have non-ignored `json:\"\"` tag"),
478 }, {
479 name: jsontest.Name("EmbedUnexported"),
480 in: struct {
481 unexported
482 }{},
483 wantOpts: fieldOptions{name: "unexported", quotedName: `"unexported"`},
484 }, {
485 name: jsontest.Name("Ignored"),
486 in: struct {
487 V int `json:"-"`
488 }{},
489 wantIgnored: true,
490 }, {
491 name: jsontest.Name("IgnoredEmbedUnexported"),
492 in: struct {
493 unexported `json:"-"`
494 }{},
495 wantIgnored: true,
496 }, {
497 name: jsontest.Name("DashComma"),
498 in: struct {
499 V int `json:"-,"`
500 }{},
501 wantOpts: fieldOptions{hasName: true, name: "-", quotedName: `"-"`},
502 wantErr: errors.New("Go struct field V has malformed `json` tag: invalid trailing ',' character"),
503 }, {
504 name: jsontest.Name("DashCommaOmitEmpty"),
505 in: struct {
506 V int `json:"-,omitempty"`
507 }{},
508 wantOpts: fieldOptions{hasName: true, name: "-", quotedName: `"-"`, omitempty: true},
509 wantErr: errors.New("Go struct field V has JSON object name \"-\"; either use `json:\"-\"` to ignore the field or use `json:\"'-',omitempty\"` to specify \"-\" as the name"),
510 }, {
511 name: jsontest.Name("QuotedDashCommaOmitEmpty"),
512 in: struct {
513 V int `json:"'-',omitempty"`
514 }{},
515 wantOpts: fieldOptions{hasName: true, name: "-", quotedName: `"-"`, omitempty: true},
516 }, {
517 name: jsontest.Name("QuotedDashName"),
518 in: struct {
519 V int `json:"'-'"`
520 }{},
521 wantOpts: fieldOptions{hasName: true, name: "-", quotedName: `"-"`},
522 }, {
523 name: jsontest.Name("LatinPunctuationName"),
524 in: struct {
525 V int `json:"$%-/"`
526 }{},
527 wantOpts: fieldOptions{hasName: true, name: "$%-/", quotedName: `"$%-/"`},
528 }, {
529 name: jsontest.Name("QuotedLatinPunctuationName"),
530 in: struct {
531 V int `json:"'$%-/'"`
532 }{},
533 wantOpts: fieldOptions{hasName: true, name: "$%-/", quotedName: `"$%-/"`},
534 }, {
535 name: jsontest.Name("LatinDigitsName"),
536 in: struct {
537 V int `json:"0123456789"`
538 }{},
539 wantOpts: fieldOptions{hasName: true, name: "0123456789", quotedName: `"0123456789"`},
540 }, {
541 name: jsontest.Name("QuotedLatinDigitsName"),
542 in: struct {
543 V int `json:"'0123456789'"`
544 }{},
545 wantOpts: fieldOptions{hasName: true, name: "0123456789", quotedName: `"0123456789"`},
546 }, {
547 name: jsontest.Name("LatinUppercaseName"),
548 in: struct {
549 V int `json:"ABCDEFGHIJKLMOPQRSTUVWXYZ"`
550 }{},
551 wantOpts: fieldOptions{hasName: true, name: "ABCDEFGHIJKLMOPQRSTUVWXYZ", quotedName: `"ABCDEFGHIJKLMOPQRSTUVWXYZ"`},
552 }, {
553 name: jsontest.Name("LatinLowercaseName"),
554 in: struct {
555 V int `json:"abcdefghijklmnopqrstuvwxyz_"`
556 }{},
557 wantOpts: fieldOptions{hasName: true, name: "abcdefghijklmnopqrstuvwxyz_", quotedName: `"abcdefghijklmnopqrstuvwxyz_"`},
558 }, {
559 name: jsontest.Name("GreekName"),
560 in: struct {
561 V string `json:"Ελλάδα"`
562 }{},
563 wantOpts: fieldOptions{hasName: true, name: "Ελλάδα", quotedName: `"Ελλάδα"`},
564 }, {
565 name: jsontest.Name("QuotedGreekName"),
566 in: struct {
567 V string `json:"'Ελλάδα'"`
568 }{},
569 wantOpts: fieldOptions{hasName: true, name: "Ελλάδα", quotedName: `"Ελλάδα"`},
570 }, {
571 name: jsontest.Name("ChineseName"),
572 in: struct {
573 V string `json:"世界"`
574 }{},
575 wantOpts: fieldOptions{hasName: true, name: "世界", quotedName: `"世界"`},
576 }, {
577 name: jsontest.Name("QuotedChineseName"),
578 in: struct {
579 V string `json:"'世界'"`
580 }{},
581 wantOpts: fieldOptions{hasName: true, name: "世界", quotedName: `"世界"`},
582 }, {
583 name: jsontest.Name("PercentSlashName"),
584 in: struct {
585 V int `json:"text/html%"`
586 }{},
587 wantOpts: fieldOptions{hasName: true, name: "text/html%", quotedName: `"text/html%"`},
588 }, {
589 name: jsontest.Name("QuotedPercentSlashName"),
590 in: struct {
591 V int `json:"'text/html%'"`
592 }{},
593 wantOpts: fieldOptions{hasName: true, name: "text/html%", quotedName: `"text/html%"`},
594 }, {
595 name: jsontest.Name("PunctuationName"),
596 in: struct {
597 V string `json:"!#$%&()*+-./:;<=>?@[]^_{|}~ "`
598 }{},
599 wantOpts: fieldOptions{hasName: true, name: "!#$%&()*+-./:;<=>?@[]^_{|}~ ", quotedName: `"!#$%&()*+-./:;<=>?@[]^_{|}~ "`, nameNeedEscape: true},
600 }, {
601 name: jsontest.Name("QuotedPunctuationName"),
602 in: struct {
603 V string `json:"'!#$%&()*+-./:;<=>?@[]^_{|}~ '"`
604 }{},
605 wantOpts: fieldOptions{hasName: true, name: "!#$%&()*+-./:;<=>?@[]^_{|}~ ", quotedName: `"!#$%&()*+-./:;<=>?@[]^_{|}~ "`, nameNeedEscape: true},
606 }, {
607 name: jsontest.Name("EmptyName"),
608 in: struct {
609 V int `json:"''"`
610 }{},
611 wantOpts: fieldOptions{hasName: true, name: "", quotedName: `""`},
612 }, {
613 name: jsontest.Name("SpaceName"),
614 in: struct {
615 V int `json:"' '"`
616 }{},
617 wantOpts: fieldOptions{hasName: true, name: " ", quotedName: `" "`},
618 }, {
619 name: jsontest.Name("CommaQuotes"),
620 in: struct {
621 V int `json:"',\\'\"\\\"'"`
622 }{},
623 wantOpts: fieldOptions{hasName: true, name: `,'""`, quotedName: `",'\"\""`, nameNeedEscape: true},
624 }, {
625 name: jsontest.Name("SingleComma"),
626 in: struct {
627 V int `json:","`
628 }{},
629 wantOpts: fieldOptions{name: "V", quotedName: `"V"`},
630 wantErr: errors.New("Go struct field V has malformed `json` tag: invalid trailing ',' character"),
631 }, {
632 name: jsontest.Name("SuperfluousCommas"),
633 in: struct {
634 V int `json:",,,,\"\",,inline,unknown,,,,"`
635 }{},
636 wantOpts: fieldOptions{name: "V", quotedName: `"V"`, inline: true, unknown: true},
637 wantErr: errors.New("Go struct field V has malformed `json` tag: invalid character ',' at start of option (expecting Unicode letter or single quote)"),
638 }, {
639 name: jsontest.Name("CaseAloneOption"),
640 in: struct {
641 FieldName int `json:",case"`
642 }{},
643 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`},
644 wantErr: errors.New("Go struct field FieldName is missing value for `case` tag option; specify `case:ignore` or `case:strict` instead"),
645 }, {
646 name: jsontest.Name("CaseIgnoreOption"),
647 in: struct {
648 FieldName int `json:",case:ignore"`
649 }{},
650 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, casing: caseIgnore},
651 }, {
652 name: jsontest.Name("CaseStrictOption"),
653 in: struct {
654 FieldName int `json:",case:strict"`
655 }{},
656 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, casing: caseStrict},
657 }, {
658 name: jsontest.Name("CaseUnknownOption"),
659 in: struct {
660 FieldName int `json:",case:unknown"`
661 }{},
662 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`},
663 wantErr: errors.New("Go struct field FieldName has unknown `case:unknown` tag value"),
664 }, {
665 name: jsontest.Name("CaseQuotedOption"),
666 in: struct {
667 FieldName int `json:",case:'ignore'"`
668 }{},
669 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, casing: caseIgnore},
670 wantErr: errors.New("Go struct field FieldName has unnecessarily quoted appearance of `case:'ignore'` tag option; specify `case:ignore` instead"),
671 }, {
672 name: jsontest.Name("BothCaseOptions"),
673 in: struct {
674 FieldName int `json:",case:ignore,case:strict"`
675 }{},
676 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, casing: caseIgnore | caseStrict},
677 wantErr: errors.New("Go struct field FieldName cannot have both `case:ignore` and `case:strict` tag options"),
678 }, {
679 name: jsontest.Name("InlineOption"),
680 in: struct {
681 FieldName int `json:",inline"`
682 }{},
683 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, inline: true},
684 }, {
685 name: jsontest.Name("UnknownOption"),
686 in: struct {
687 FieldName int `json:",unknown"`
688 }{},
689 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, unknown: true},
690 }, {
691 name: jsontest.Name("OmitZeroOption"),
692 in: struct {
693 FieldName int `json:",omitzero"`
694 }{},
695 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, omitzero: true},
696 }, {
697 name: jsontest.Name("OmitEmptyOption"),
698 in: struct {
699 FieldName int `json:",omitempty"`
700 }{},
701 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, omitempty: true},
702 }, {
703 name: jsontest.Name("StringOption"),
704 in: struct {
705 FieldName int `json:",string"`
706 }{},
707 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, string: true},
708 }, {
709 name: jsontest.Name("FormatOptionEqual"),
710 in: struct {
711 FieldName int `json:",format=fizzbuzz"`
712 }{},
713 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`},
714 wantErr: errors.New("Go struct field FieldName is missing value for `format` tag option"),
715 }, {
716 name: jsontest.Name("FormatOptionColon"),
717 in: struct {
718 FieldName int `json:",format:fizzbuzz"`
719 }{},
720 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, format: "fizzbuzz"},
721 }, {
722 name: jsontest.Name("FormatOptionQuoted"),
723 in: struct {
724 FieldName int `json:",format:'2006-01-02'"`
725 }{},
726 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, format: "2006-01-02"},
727 }, {
728 name: jsontest.Name("FormatOptionInvalid"),
729 in: struct {
730 FieldName int `json:",format:'2006-01-02"`
731 }{},
732 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`},
733 wantErr: errors.New("Go struct field FieldName has malformed value for `format` tag option: single-quoted string not terminated: '2006-01-0..."),
734 }, {
735 name: jsontest.Name("FormatOptionNotLast"),
736 in: struct {
737 FieldName int `json:",format:alpha,ordered"`
738 }{},
739 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, format: "alpha"},
740 wantErr: errors.New("Go struct field FieldName has `format` tag option that was not specified last"),
741 }, {
742 name: jsontest.Name("AllOptions"),
743 in: struct {
744 FieldName int `json:",case:ignore,inline,unknown,omitzero,omitempty,string,format:format"`
745 }{},
746 wantOpts: fieldOptions{
747 name: "FieldName",
748 quotedName: `"FieldName"`,
749 casing: caseIgnore,
750 inline: true,
751 unknown: true,
752 omitzero: true,
753 omitempty: true,
754 string: true,
755 format: "format",
756 },
757 }, {
758 name: jsontest.Name("AllOptionsQuoted"),
759 in: struct {
760 FieldName int `json:",'case':'ignore','inline','unknown','omitzero','omitempty','string','format':'format'"`
761 }{},
762 wantOpts: fieldOptions{
763 name: "FieldName",
764 quotedName: `"FieldName"`,
765 casing: caseIgnore,
766 inline: true,
767 unknown: true,
768 omitzero: true,
769 omitempty: true,
770 string: true,
771 format: "format",
772 },
773 wantErr: errors.New("Go struct field FieldName has unnecessarily quoted appearance of `'case'` tag option; specify `case` instead"),
774 }, {
775 name: jsontest.Name("AllOptionsCaseSensitive"),
776 in: struct {
777 FieldName int `json:",CASE:IGNORE,INLINE,UNKNOWN,OMITZERO,OMITEMPTY,STRING,FORMAT:FORMAT"`
778 }{},
779 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`},
780 wantErr: errors.New("Go struct field FieldName has invalid appearance of `CASE` tag option; specify `case` instead"),
781 }, {
782 name: jsontest.Name("AllOptionsSpaceSensitive"),
783 in: struct {
784 FieldName int `json:", case:ignore , inline , unknown , omitzero , omitempty , string , format:format "`
785 }{},
786 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`},
787 wantErr: errors.New("Go struct field FieldName has malformed `json` tag: invalid character ' ' at start of option (expecting Unicode letter or single quote)"),
788 }, {
789 name: jsontest.Name("UnknownTagOption"),
790 in: struct {
791 FieldName int `json:",inline,whoknows,string"`
792 }{},
793 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, inline: true, string: true},
794 }, {
795 name: jsontest.Name("MalformedQuotedString/MissingQuote"),
796 in: struct {
797 FieldName int `json:"'hello,string"`
798 }{},
799 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, string: true},
800 wantErr: errors.New("Go struct field FieldName has malformed `json` tag: single-quoted string not terminated: 'hello,str..."),
801 }, {
802 name: jsontest.Name("MalformedQuotedString/MissingComma"),
803 in: struct {
804 FieldName int `json:"'hello'inline,string"`
805 }{},
806 wantOpts: fieldOptions{hasName: true, name: "hello", quotedName: `"hello"`, inline: true, string: true},
807 wantErr: errors.New("Go struct field FieldName has malformed `json` tag: invalid character 'i' before next option (expecting ',')"),
808 }, {
809 name: jsontest.Name("MalformedQuotedString/InvalidEscape"),
810 in: struct {
811 FieldName int `json:"'hello\\u####',inline,string"`
812 }{},
813 wantOpts: fieldOptions{name: "FieldName", quotedName: `"FieldName"`, inline: true, string: true},
814 wantErr: errors.New("Go struct field FieldName has malformed `json` tag: invalid single-quoted string: 'hello\\u####'"),
815 }, {
816 name: jsontest.Name("MisnamedTag"),
817 in: struct {
818 V int `jsom:"Misnamed"`
819 }{},
820 wantOpts: fieldOptions{name: "V", quotedName: `"V"`},
821 }}
822
823 for _, tt := range tests {
824 t.Run(tt.name.Name, func(t *testing.T) {
825 fs := reflect.TypeOf(tt.in).Field(0)
826 gotOpts, gotIgnored, gotErr := parseFieldOptions(fs)
827 if !reflect.DeepEqual(gotOpts, tt.wantOpts) || gotIgnored != tt.wantIgnored || !reflect.DeepEqual(gotErr, tt.wantErr) {
828 t.Errorf("%s: parseFieldOptions(%T) = (\n\t%v,\n\t%v,\n\t%v\n), want (\n\t%v,\n\t%v,\n\t%v\n)", tt.name.Where, tt.in, gotOpts, gotIgnored, gotErr, tt.wantOpts, tt.wantIgnored, tt.wantErr)
829 }
830 })
831 }
832 }