-
Notifications
You must be signed in to change notification settings - Fork 7
/
marshal_test.go
2861 lines (2687 loc) · 75.6 KB
/
marshal_test.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xml
import (
"bytes"
"errors"
"fmt"
"io"
"reflect"
"strconv"
"strings"
"sync"
"testing"
"time"
)
type DriveType int
const (
HyperDrive DriveType = iota
ImprobabilityDrive
)
type Passenger struct {
Name []string `xml:"name"`
Weight float32 `xml:"weight"`
}
type Ship struct {
XMLName struct{} `xml:"spaceship"`
Name string `xml:"name,attr"`
Pilot string `xml:"pilot,attr"`
Drive DriveType `xml:"drive"`
Age uint `xml:"age"`
Passenger []*Passenger `xml:"passenger"`
secret string
}
type NamedType string
type Port struct {
XMLName struct{} `xml:"port"`
Type string `xml:"type,attr,omitempty"`
Comment string `xml:",comment"`
Number string `xml:",chardata"`
}
type Domain struct {
XMLName struct{} `xml:"domain"`
Country string `xml:",attr,omitempty"`
Name []byte `xml:",chardata"`
Comment []byte `xml:",comment"`
}
type Book struct {
XMLName struct{} `xml:"book"`
Title string `xml:",chardata"`
}
type Event struct {
XMLName struct{} `xml:"event"`
Year int `xml:",chardata"`
}
type Movie struct {
XMLName struct{} `xml:"movie"`
Length uint `xml:",chardata"`
}
type Pi struct {
XMLName struct{} `xml:"pi"`
Approximation float32 `xml:",chardata"`
}
type Universe struct {
XMLName struct{} `xml:"universe"`
Visible float64 `xml:",chardata"`
}
type Particle struct {
XMLName struct{} `xml:"particle"`
HasMass bool `xml:",chardata"`
}
type Departure struct {
XMLName struct{} `xml:"departure"`
When time.Time `xml:",chardata"`
}
type SecretAgent struct {
XMLName struct{} `xml:"agent"`
Handle string `xml:"handle,attr"`
Identity string
Obfuscate string `xml:",innerxml"`
}
type NestedItems struct {
XMLName struct{} `xml:"result"`
Items []string `xml:">item"`
Item1 []string `xml:"Items>item1"`
}
type NestedOrder struct {
XMLName struct{} `xml:"result"`
Field1 string `xml:"parent>c"`
Field2 string `xml:"parent>b"`
Field3 string `xml:"parent>a"`
}
type MixedNested struct {
XMLName struct{} `xml:"result"`
A string `xml:"parent1>a"`
B string `xml:"b"`
C string `xml:"parent1>parent2>c"`
D string `xml:"parent1>d"`
}
type NilTest struct {
A any `xml:"parent1>parent2>a"`
B any `xml:"parent1>b"`
C any `xml:"parent1>parent2>c"`
}
type Service struct {
XMLName struct{} `xml:"service"`
Domain *Domain `xml:"host>domain"`
Port *Port `xml:"host>port"`
Extra1 any
Extra2 any `xml:"host>extra2"`
}
var nilStruct *Ship
type EmbedA struct {
EmbedC
EmbedB EmbedB
FieldA string
embedD
}
type EmbedB struct {
FieldB string
*EmbedC
}
type EmbedC struct {
FieldA1 string `xml:"FieldA>A1"`
FieldA2 string `xml:"FieldA>A2"`
FieldB string
FieldC string
}
type embedD struct {
fieldD string
FieldE string // Promoted and visible when embedD is embedded.
}
type NameCasing struct {
XMLName struct{} `xml:"casing"`
Xy string
XY string
XyA string `xml:"Xy,attr"`
XYA string `xml:"XY,attr"`
}
type NamePrecedence struct {
XMLName Name `xml:"Parent"`
FromTag XMLNameWithoutTag `xml:"InTag"`
FromNameVal XMLNameWithoutTag
FromNameTag XMLNameWithTag
InFieldName string
}
type XMLNameWithTag struct {
XMLName Name `xml:"InXMLNameTag"`
Value string `xml:",chardata"`
}
type XMLNameWithoutTag struct {
XMLName Name
Value string `xml:",chardata"`
}
type NameInField struct {
Foo Name `xml:"ns foo"`
}
type AttrTest struct {
Int int `xml:",attr"`
Named int `xml:"int,attr"`
Float float64 `xml:",attr"`
Uint8 uint8 `xml:",attr"`
Bool bool `xml:",attr"`
Str string `xml:",attr"`
Bytes []byte `xml:",attr"`
}
type AttrsTest struct {
Attrs []Attr `xml:",any,attr"`
Int int `xml:",attr"`
Named int `xml:"int,attr"`
Float float64 `xml:",attr"`
Uint8 uint8 `xml:",attr"`
Bool bool `xml:",attr"`
Str string `xml:",attr"`
Bytes []byte `xml:",attr"`
}
type OmitAttrTest struct {
Int int `xml:",attr,omitempty"`
Named int `xml:"int,attr,omitempty"`
Float float64 `xml:",attr,omitempty"`
Uint8 uint8 `xml:",attr,omitempty"`
Bool bool `xml:",attr,omitempty"`
Str string `xml:",attr,omitempty"`
Bytes []byte `xml:",attr,omitempty"`
PStr *string `xml:",attr,omitempty"`
}
type OmitFieldTest struct {
Int int `xml:",omitempty"`
Named int `xml:"int,omitempty"`
Float float64 `xml:",omitempty"`
Uint8 uint8 `xml:",omitempty"`
Bool bool `xml:",omitempty"`
Str string `xml:",omitempty"`
Bytes []byte `xml:",omitempty"`
PStr *string `xml:",omitempty"`
Ptr *PresenceTest `xml:",omitempty"`
}
type AnyTest struct {
XMLName struct{} `xml:"a"`
Nested string `xml:"nested>value"`
AnyField AnyHolder `xml:",any"`
}
type AnyOmitTest struct {
XMLName struct{} `xml:"a"`
Nested string `xml:"nested>value"`
AnyField *AnyHolder `xml:",any,omitempty"`
}
type AnySliceTest struct {
XMLName struct{} `xml:"a"`
Nested string `xml:"nested>value"`
AnyField []AnyHolder `xml:",any"`
}
type AnyHolder struct {
XMLName Name
XML string `xml:",innerxml"`
}
type RecurseA struct {
A string
B *RecurseB
}
type RecurseB struct {
A *RecurseA
B string
}
type PresenceTest struct {
Exists *struct{}
}
type IgnoreTest struct {
PublicSecret string `xml:"-"`
}
type MyBytes []byte
type Data struct {
Bytes []byte
Attr []byte `xml:",attr"`
Custom MyBytes
}
type Plain struct {
V any
}
type MyInt int
type EmbedInt struct {
MyInt
}
type Strings struct {
X []string `xml:"A>B,omitempty"`
}
type PointerFieldsTest struct {
XMLName Name `xml:"dummy"`
Name *string `xml:"name,attr"`
Age *uint `xml:"age,attr"`
Empty *string `xml:"empty,attr"`
Contents *string `xml:",chardata"`
}
type ChardataEmptyTest struct {
XMLName Name `xml:"test"`
Contents *string `xml:",chardata"`
}
type PointerAnonFields struct {
*MyInt
*NamedType
}
type MyMarshalerTest struct {
}
var _ Marshaler = (*MyMarshalerTest)(nil)
func (m *MyMarshalerTest) MarshalXML(e *Encoder, start StartElement) error {
e.EncodeToken(start)
e.EncodeToken(CharData([]byte("hello world")))
e.EncodeToken(EndElement{start.Name})
return nil
}
type MyMarshalerAttrTest struct {
}
var _ MarshalerAttr = (*MyMarshalerAttrTest)(nil)
func (m *MyMarshalerAttrTest) MarshalXMLAttr(name Name) (Attr, error) {
return Attr{name, "hello world"}, nil
}
func (m *MyMarshalerAttrTest) UnmarshalXMLAttr(attr Attr) error {
return nil
}
type MarshalerStruct struct {
Foo MyMarshalerAttrTest `xml:",attr"`
}
type InnerStruct struct {
XMLName Name `xml:"testns outer"`
}
type OuterStruct struct {
InnerStruct
IntAttr int `xml:"int,attr"`
}
type OuterNamedStruct struct {
InnerStruct
XMLName Name `xml:"outerns test"`
IntAttr int `xml:"int,attr"`
}
type OuterNamedOrderedStruct struct {
XMLName Name `xml:"outerns test"`
InnerStruct
IntAttr int `xml:"int,attr"`
}
type OuterOuterStruct struct {
OuterStruct
}
type NestedAndChardata struct {
AB []string `xml:"A>B"`
Chardata string `xml:",chardata"`
}
type NestedAndComment struct {
AB []string `xml:"A>B"`
Comment string `xml:",comment"`
}
type CDataTest struct {
Chardata string `xml:",cdata"`
}
type NestedAndCData struct {
AB []string `xml:"A>B"`
CDATA string `xml:",cdata"`
}
func ifaceptr(x any) any {
return &x
}
func stringptr(x string) *string {
return &x
}
type T1 struct{}
type T2 struct{}
type IndirComment struct {
T1 T1
Comment *string `xml:",comment"`
T2 T2
}
type DirectComment struct {
T1 T1
Comment string `xml:",comment"`
T2 T2
}
type IfaceComment struct {
T1 T1
Comment any `xml:",comment"`
T2 T2
}
type IndirChardata struct {
T1 T1
Chardata *string `xml:",chardata"`
T2 T2
}
type DirectChardata struct {
T1 T1
Chardata string `xml:",chardata"`
T2 T2
}
type IfaceChardata struct {
T1 T1
Chardata any `xml:",chardata"`
T2 T2
}
type IndirCDATA struct {
T1 T1
CDATA *string `xml:",cdata"`
T2 T2
}
type DirectCDATA struct {
T1 T1
CDATA string `xml:",cdata"`
T2 T2
}
type IfaceCDATA struct {
T1 T1
CDATA any `xml:",cdata"`
T2 T2
}
type IndirInnerXML struct {
T1 T1
InnerXML *string `xml:",innerxml"`
T2 T2
}
type DirectInnerXML struct {
T1 T1
InnerXML string `xml:",innerxml"`
T2 T2
}
type IfaceInnerXML struct {
T1 T1
InnerXML any `xml:",innerxml"`
T2 T2
}
type IndirElement struct {
T1 T1
Element *string
T2 T2
}
type DirectElement struct {
T1 T1
Element string
T2 T2
}
type IfaceElement struct {
T1 T1
Element any
T2 T2
}
type IndirOmitEmpty struct {
T1 T1
OmitEmpty *string `xml:",omitempty"`
T2 T2
}
type DirectOmitEmpty struct {
T1 T1
OmitEmpty string `xml:",omitempty"`
T2 T2
}
type IfaceOmitEmpty struct {
T1 T1
OmitEmpty any `xml:",omitempty"`
T2 T2
}
type IndirAny struct {
T1 T1
Any *string `xml:",any"`
T2 T2
}
type DirectAny struct {
T1 T1
Any string `xml:",any"`
T2 T2
}
type IfaceAny struct {
T1 T1
Any any `xml:",any"`
T2 T2
}
type Generic[T any] struct {
X T
}
type EPP struct {
XMLName struct{} `xml:"urn:ietf:params:xml:ns:epp-1.0 epp"`
Command *Command `xml:"command,omitempty"`
}
type Command struct {
Check *Check `xml:"urn:ietf:params:xml:ns:epp-1.0 check,omitempty"`
}
type Check struct {
DomainCheck *DomainCheck `xml:"urn:ietf:params:xml:ns:domain-1.0 domain:check,omitempty"`
}
type DomainCheck struct {
DomainNames []string `xml:"urn:ietf:params:xml:ns:domain-1.0 domain:name,omitempty"`
}
type SecureEnvelope struct {
XMLName struct{} `xml:"urn:test:secure-1.0 sec:envelope"`
Message *SecureMessage `xml:"urn:test:message-1.0 msg,omitempty"`
}
type SecureMessage struct {
Body string `xml:"urn:test:message-1.0 body,omitempty"`
Signer string `xml:"urn:test:secure-1.0 sec:signer,attr,omitempty"`
}
type NamespacedNested struct {
XMLName struct{} `xml:"urn:test:nested-1.0 nested"`
Value string `xml:"urn:test:nested-1.0 nested:wrapper>nested:value"`
}
var (
nameAttr = "Sarah"
ageAttr = uint(12)
contentsAttr = "lorem ipsum"
empty = ""
)
// Unless explicitly stated as such (or *Plain), all of the
// tests below are two-way tests. When introducing new tests,
// please try to make them two-way as well to ensure that
// marshaling and unmarshaling are as symmetrical as feasible.
var marshalTests = []struct {
Value any
ExpectXML string
MarshalOnly bool
MarshalError string
UnmarshalOnly bool
UnmarshalError string
}{
// Test nil marshals to nothing
{Value: nil, ExpectXML: ``, MarshalOnly: true},
{Value: nilStruct, ExpectXML: ``, MarshalOnly: true},
// Test value types
{Value: &Plain{true}, ExpectXML: `<Plain><V>true</V></Plain>`},
{Value: &Plain{false}, ExpectXML: `<Plain><V>false</V></Plain>`},
{Value: &Plain{int(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
{Value: &Plain{int8(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
{Value: &Plain{int16(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
{Value: &Plain{int32(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
{Value: &Plain{uint(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
{Value: &Plain{uint8(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
{Value: &Plain{uint16(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
{Value: &Plain{uint32(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
{Value: &Plain{float32(1.25)}, ExpectXML: `<Plain><V>1.25</V></Plain>`},
{Value: &Plain{float64(1.25)}, ExpectXML: `<Plain><V>1.25</V></Plain>`},
{Value: &Plain{uintptr(0xFFDD)}, ExpectXML: `<Plain><V>65501</V></Plain>`},
{Value: &Plain{"gopher"}, ExpectXML: `<Plain><V>gopher</V></Plain>`},
{Value: &Plain{[]byte("gopher")}, ExpectXML: `<Plain><V>gopher</V></Plain>`},
{Value: &Plain{"</>"}, ExpectXML: `<Plain><V></></V></Plain>`},
{Value: &Plain{[]byte("</>")}, ExpectXML: `<Plain><V></></V></Plain>`},
{Value: &Plain{[3]byte{'<', '/', '>'}}, ExpectXML: `<Plain><V></></V></Plain>`},
{Value: &Plain{NamedType("potato")}, ExpectXML: `<Plain><V>potato</V></Plain>`},
{Value: &Plain{[]int{1, 2, 3}}, ExpectXML: `<Plain><V>1</V><V>2</V><V>3</V></Plain>`},
{Value: &Plain{[3]int{1, 2, 3}}, ExpectXML: `<Plain><V>1</V><V>2</V><V>3</V></Plain>`},
{Value: ifaceptr(true), MarshalOnly: true, ExpectXML: `<bool>true</bool>`},
// Test time.
{
Value: &Plain{time.Unix(1e9, 123456789).UTC()},
ExpectXML: `<Plain><V>2001-09-09T01:46:40.123456789Z</V></Plain>`,
},
// A pointer to struct{} may be used to test for an element's presence.
{
Value: &PresenceTest{new(struct{})},
ExpectXML: `<PresenceTest><Exists></Exists></PresenceTest>`,
},
{
Value: &PresenceTest{},
ExpectXML: `<PresenceTest></PresenceTest>`,
},
// A []byte field is only nil if the element was not found.
{
Value: &Data{},
ExpectXML: `<Data></Data>`,
UnmarshalOnly: true,
},
{
Value: &Data{Bytes: []byte{}, Custom: MyBytes{}, Attr: []byte{}},
ExpectXML: `<Data Attr=""><Bytes></Bytes><Custom></Custom></Data>`,
UnmarshalOnly: true,
},
// Check that []byte works, including named []byte types.
{
Value: &Data{Bytes: []byte("ab"), Custom: MyBytes("cd"), Attr: []byte{'v'}},
ExpectXML: `<Data Attr="v"><Bytes>ab</Bytes><Custom>cd</Custom></Data>`,
},
// Test innerxml
{
Value: &SecretAgent{
Handle: "007",
Identity: "James Bond",
Obfuscate: "<redacted/>",
},
ExpectXML: `<agent handle="007"><Identity>James Bond</Identity><redacted/></agent>`,
MarshalOnly: true,
},
{
Value: &SecretAgent{
Handle: "007",
Identity: "James Bond",
Obfuscate: "<Identity>James Bond</Identity><redacted/>",
},
ExpectXML: `<agent handle="007"><Identity>James Bond</Identity><redacted/></agent>`,
UnmarshalOnly: true,
},
// Test structs
{Value: &Port{Type: "ssl", Number: "443"}, ExpectXML: `<port type="ssl">443</port>`},
{Value: &Port{Number: "443"}, ExpectXML: `<port>443</port>`},
{Value: &Port{Type: "<unix>"}, ExpectXML: `<port type="<unix>"></port>`},
// Marshal is not symmetric to Unmarshal for these oddities because &apos is written as '
{Value: &Port{Type: "<un'ix>"}, ExpectXML: `<port type="<un'ix>"></port>`, UnmarshalOnly: true},
{Value: &Port{Type: "<un\"ix>"}, ExpectXML: `<port type="<un"ix>"></port>`, UnmarshalOnly: true},
{Value: &Port{Type: "<un&ix>"}, ExpectXML: `<port type="<un&ix>"></port>`},
{Value: &Port{Type: "<unix>"}, ExpectXML: `<port type="<unix>"></port>`, UnmarshalOnly: true},
{Value: &Port{Number: "443", Comment: "https"}, ExpectXML: `<port><!--https-->443</port>`},
{Value: &Port{Number: "443", Comment: "add space-"}, ExpectXML: `<port><!--add space- -->443</port>`, MarshalOnly: true},
{Value: &Domain{Name: []byte("google.com&friends")}, ExpectXML: `<domain>google.com&friends</domain>`},
{Value: &Domain{Name: []byte("google.com"), Comment: []byte(" &friends ")}, ExpectXML: `<domain>google.com<!-- &friends --></domain>`},
{Value: &Book{Title: "Pride & Prejudice"}, ExpectXML: `<book>Pride & Prejudice</book>`},
{Value: &Event{Year: -3114}, ExpectXML: `<event>-3114</event>`},
{Value: &Movie{Length: 13440}, ExpectXML: `<movie>13440</movie>`},
{Value: &Pi{Approximation: 3.14159265}, ExpectXML: `<pi>3.1415927</pi>`},
{Value: &Universe{Visible: 9.3e13}, ExpectXML: `<universe>9.3e+13</universe>`},
{Value: &Particle{HasMass: true}, ExpectXML: `<particle>true</particle>`},
{Value: &Departure{When: ParseTime("2013-01-09T00:15:00-09:00")}, ExpectXML: `<departure>2013-01-09T00:15:00-09:00</departure>`},
{Value: atomValue, ExpectXML: atomXML},
{Value: &Generic[int]{1}, ExpectXML: `<Generic><X>1</X></Generic>`},
{
Value: &Ship{
Name: "Heart of Gold",
Pilot: "Computer",
Age: 1,
Drive: ImprobabilityDrive,
Passenger: []*Passenger{
{
Name: []string{"Zaphod", "Beeblebrox"},
Weight: 7.25,
},
{
Name: []string{"Trisha", "McMillen"},
Weight: 5.5,
},
{
Name: []string{"Ford", "Prefect"},
Weight: 7,
},
{
Name: []string{"Arthur", "Dent"},
Weight: 6.75,
},
},
},
ExpectXML: `<spaceship name="Heart of Gold" pilot="Computer">` +
`<drive>` + strconv.Itoa(int(ImprobabilityDrive)) + `</drive>` +
`<age>1</age>` +
`<passenger>` +
`<name>Zaphod</name>` +
`<name>Beeblebrox</name>` +
`<weight>7.25</weight>` +
`</passenger>` +
`<passenger>` +
`<name>Trisha</name>` +
`<name>McMillen</name>` +
`<weight>5.5</weight>` +
`</passenger>` +
`<passenger>` +
`<name>Ford</name>` +
`<name>Prefect</name>` +
`<weight>7</weight>` +
`</passenger>` +
`<passenger>` +
`<name>Arthur</name>` +
`<name>Dent</name>` +
`<weight>6.75</weight>` +
`</passenger>` +
`</spaceship>`,
},
// Test a>b
{
Value: &NestedItems{Items: nil, Item1: nil},
ExpectXML: `<result>` +
`<Items>` +
`</Items>` +
`</result>`,
},
{
Value: &NestedItems{Items: []string{}, Item1: []string{}},
ExpectXML: `<result>` +
`<Items>` +
`</Items>` +
`</result>`,
MarshalOnly: true,
},
{
Value: &NestedItems{Items: nil, Item1: []string{"A"}},
ExpectXML: `<result>` +
`<Items>` +
`<item1>A</item1>` +
`</Items>` +
`</result>`,
},
{
Value: &NestedItems{Items: []string{"A", "B"}, Item1: nil},
ExpectXML: `<result>` +
`<Items>` +
`<item>A</item>` +
`<item>B</item>` +
`</Items>` +
`</result>`,
},
{
Value: &NestedItems{Items: []string{"A", "B"}, Item1: []string{"C"}},
ExpectXML: `<result>` +
`<Items>` +
`<item>A</item>` +
`<item>B</item>` +
`<item1>C</item1>` +
`</Items>` +
`</result>`,
},
{
Value: &NestedOrder{Field1: "C", Field2: "B", Field3: "A"},
ExpectXML: `<result>` +
`<parent>` +
`<c>C</c>` +
`<b>B</b>` +
`<a>A</a>` +
`</parent>` +
`</result>`,
},
{
Value: &NilTest{A: "A", B: nil, C: "C"},
ExpectXML: `<NilTest>` +
`<parent1>` +
`<parent2><a>A</a></parent2>` +
`<parent2><c>C</c></parent2>` +
`</parent1>` +
`</NilTest>`,
MarshalOnly: true, // Uses interface{}
},
{
Value: &MixedNested{A: "A", B: "B", C: "C", D: "D"},
ExpectXML: `<result>` +
`<parent1><a>A</a></parent1>` +
`<b>B</b>` +
`<parent1>` +
`<parent2><c>C</c></parent2>` +
`<d>D</d>` +
`</parent1>` +
`</result>`,
},
{
Value: &Service{Port: &Port{Number: "80"}},
ExpectXML: `<service><host><port>80</port></host></service>`,
},
{
Value: &Service{},
ExpectXML: `<service></service>`,
},
{
Value: &Service{Port: &Port{Number: "80"}, Extra1: "A", Extra2: "B"},
ExpectXML: `<service>` +
`<host><port>80</port></host>` +
`<Extra1>A</Extra1>` +
`<host><extra2>B</extra2></host>` +
`</service>`,
MarshalOnly: true,
},
{
Value: &Service{Port: &Port{Number: "80"}, Extra2: "example"},
ExpectXML: `<service>` +
`<host><port>80</port></host>` +
`<host><extra2>example</extra2></host>` +
`</service>`,
MarshalOnly: true,
},
{
Value: &struct {
XMLName struct{} `xml:"space top"`
A string `xml:"x>a"`
B string `xml:"x>b"`
C string `xml:"space x>c"`
C1 string `xml:"space1 x>c"`
D1 string `xml:"space1 x>d"`
}{
A: "a",
B: "b",
C: "c",
C1: "c1",
D1: "d1",
},
ExpectXML: `<top xmlns="space">` +
`<x><a>a</a><b>b</b><c>c</c>` +
`<c xmlns="space1">c1</c>` +
`<d xmlns="space1">d1</d>` +
`</x>` +
`</top>`,
},
{
Value: &struct {
XMLName Name
A string `xml:"x>a"`
B string `xml:"x>b"`
C string `xml:"space x>c"`
C1 string `xml:"space1 x>c"`
D1 string `xml:"space1 x>d"`
}{
XMLName: Name{
Space: "space0",
Local: "top",
},
A: "a",
B: "b",
C: "c",
C1: "c1",
D1: "d1",
},
ExpectXML: `<top xmlns="space0">` +
`<x><a>a</a><b>b</b>` +
`<c xmlns="space">c</c>` +
`<c xmlns="space1">c1</c>` +
`<d xmlns="space1">d1</d>` +
`</x>` +
`</top>`,
},
{
Value: &struct {
XMLName struct{} `xml:"top"`
B string `xml:"space x>b"`
B1 string `xml:"space1 x>b"`
}{
B: "b",
B1: "b1",
},
ExpectXML: `<top>` +
`<x><b xmlns="space">b</b>` +
`<b xmlns="space1">b1</b></x>` +
`</top>`,
},
// Test struct embedding
{
Value: &EmbedA{
EmbedC: EmbedC{
FieldA1: "", // Shadowed by A.A
FieldA2: "", // Shadowed by A.A
FieldB: "A.C.B",
FieldC: "A.C.C",
},
EmbedB: EmbedB{
FieldB: "A.B.B",
EmbedC: &EmbedC{
FieldA1: "A.B.C.A1",
FieldA2: "A.B.C.A2",
FieldB: "", // Shadowed by A.B.B
FieldC: "A.B.C.C",
},
},
FieldA: "A.A",
embedD: embedD{
FieldE: "A.D.E",
},
},
ExpectXML: `<EmbedA>` +
`<FieldB>A.C.B</FieldB>` +
`<FieldC>A.C.C</FieldC>` +
`<EmbedB>` +
`<FieldB>A.B.B</FieldB>` +
`<FieldA>` +
`<A1>A.B.C.A1</A1>` +
`<A2>A.B.C.A2</A2>` +
`</FieldA>` +
`<FieldC>A.B.C.C</FieldC>` +
`</EmbedB>` +
`<FieldA>A.A</FieldA>` +
`<FieldE>A.D.E</FieldE>` +
`</EmbedA>`,
},
// Anonymous struct pointer field which is nil
{
Value: &EmbedB{},
ExpectXML: `<EmbedB><FieldB></FieldB></EmbedB>`,
},
// Other kinds of nil anonymous fields
{
Value: &PointerAnonFields{},
ExpectXML: `<PointerAnonFields></PointerAnonFields>`,
},
// Test that name casing matters
{
Value: &NameCasing{Xy: "mixed", XY: "upper", XyA: "mixedA", XYA: "upperA"},
ExpectXML: `<casing Xy="mixedA" XY="upperA"><Xy>mixed</Xy><XY>upper</XY></casing>`,
},
// Test the order in which the XML element name is chosen
{
Value: &NamePrecedence{
FromTag: XMLNameWithoutTag{Value: "A"},
FromNameVal: XMLNameWithoutTag{XMLName: Name{Local: "InXMLName"}, Value: "B"},
FromNameTag: XMLNameWithTag{Value: "C"},
InFieldName: "D",
},
ExpectXML: `<Parent>` +
`<InTag>A</InTag>` +
`<InXMLName>B</InXMLName>` +
`<InXMLNameTag>C</InXMLNameTag>` +
`<InFieldName>D</InFieldName>` +
`</Parent>`,
MarshalOnly: true,
},
{
Value: &NamePrecedence{
XMLName: Name{Local: "Parent"},
FromTag: XMLNameWithoutTag{XMLName: Name{Local: "InTag"}, Value: "A"},
FromNameVal: XMLNameWithoutTag{XMLName: Name{Local: "FromNameVal"}, Value: "B"},
FromNameTag: XMLNameWithTag{XMLName: Name{Local: "InXMLNameTag"}, Value: "C"},
InFieldName: "D",
},
ExpectXML: `<Parent>` +
`<InTag>A</InTag>` +
`<FromNameVal>B</FromNameVal>` +
`<InXMLNameTag>C</InXMLNameTag>` +
`<InFieldName>D</InFieldName>` +
`</Parent>`,
UnmarshalOnly: true,
},
// xml.Name works in a plain field as well.
{
Value: &NameInField{Name{Space: "ns", Local: "foo"}},
ExpectXML: `<NameInField><foo xmlns="ns"></foo></NameInField>`,
},
{
Value: &NameInField{Name{Space: "ns", Local: "foo"}},
ExpectXML: `<NameInField><foo xmlns="ns"><ignore></ignore></foo></NameInField>`,
UnmarshalOnly: true,
},