@@ -752,3 +752,159 @@ func TestInvalidInnerXMLType(t *testing.T) {
752
752
t .Errorf ("NotInnerXML = %v, want nil" , v .NotInnerXML )
753
753
}
754
754
}
755
+
756
+ type Child struct {
757
+ G struct {
758
+ I int
759
+ }
760
+ }
761
+
762
+ type ChildToEmbed struct {
763
+ X bool
764
+ }
765
+
766
+ type Parent struct {
767
+ I int
768
+ IPtr * int
769
+ Is []int
770
+ IPtrs []* int
771
+ F float32
772
+ FPtr * float32
773
+ Fs []float32
774
+ FPtrs []* float32
775
+ B bool
776
+ BPtr * bool
777
+ Bs []bool
778
+ BPtrs []* bool
779
+ Bytes []byte
780
+ BytesPtr * []byte
781
+ S string
782
+ SPtr * string
783
+ Ss []string
784
+ SPtrs []* string
785
+ MyI MyInt
786
+ Child Child
787
+ Children []Child
788
+ ChildPtr * Child
789
+ ChildToEmbed
790
+ }
791
+
792
+ const (
793
+ emptyXML = `
794
+ <Parent>
795
+ <I></I>
796
+ <IPtr></IPtr>
797
+ <Is></Is>
798
+ <IPtrs></IPtrs>
799
+ <F></F>
800
+ <FPtr></FPtr>
801
+ <Fs></Fs>
802
+ <FPtrs></FPtrs>
803
+ <B></B>
804
+ <BPtr></BPtr>
805
+ <Bs></Bs>
806
+ <BPtrs></BPtrs>
807
+ <Bytes></Bytes>
808
+ <BytesPtr></BytesPtr>
809
+ <S></S>
810
+ <SPtr></SPtr>
811
+ <Ss></Ss>
812
+ <SPtrs></SPtrs>
813
+ <MyI></MyI>
814
+ <Child></Child>
815
+ <Children></Children>
816
+ <ChildPtr></ChildPtr>
817
+ <X></X>
818
+ </Parent>
819
+ `
820
+ )
821
+
822
+ // github.com/golang/go/issues/13417
823
+ func TestUnmarshalEmptyValues (t * testing.T ) {
824
+ // Test first with a zero-valued dst.
825
+ v := new (Parent )
826
+ if err := Unmarshal ([]byte (emptyXML ), v ); err != nil {
827
+ t .Fatalf ("zero: Unmarshal failed: got %v" , err )
828
+ }
829
+
830
+ zBytes , zInt , zStr , zFloat , zBool := []byte {}, 0 , "" , float32 (0 ), false
831
+ want := & Parent {
832
+ IPtr : & zInt ,
833
+ Is : []int {zInt },
834
+ IPtrs : []* int {& zInt },
835
+ FPtr : & zFloat ,
836
+ Fs : []float32 {zFloat },
837
+ FPtrs : []* float32 {& zFloat },
838
+ BPtr : & zBool ,
839
+ Bs : []bool {zBool },
840
+ BPtrs : []* bool {& zBool },
841
+ Bytes : []byte {},
842
+ BytesPtr : & zBytes ,
843
+ SPtr : & zStr ,
844
+ Ss : []string {zStr },
845
+ SPtrs : []* string {& zStr },
846
+ Children : []Child {{}},
847
+ ChildPtr : new (Child ),
848
+ ChildToEmbed : ChildToEmbed {},
849
+ }
850
+ if ! reflect .DeepEqual (v , want ) {
851
+ t .Fatalf ("zero: Unmarshal:\n have: %#+v\n want: %#+v" , v , want )
852
+ }
853
+
854
+ // Test with a pre-populated dst.
855
+ // Multiple addressable copies, as pointer-to fields will replace value during unmarshal.
856
+ vBytes0 , vInt0 , vStr0 , vFloat0 , vBool0 := []byte ("x" ), 1 , "x" , float32 (1 ), true
857
+ vBytes1 , vInt1 , vStr1 , vFloat1 , vBool1 := []byte ("x" ), 1 , "x" , float32 (1 ), true
858
+ vInt2 , vStr2 , vFloat2 , vBool2 := 1 , "x" , float32 (1 ), true
859
+ v = & Parent {
860
+ I : vInt0 ,
861
+ IPtr : & vInt1 ,
862
+ Is : []int {vInt0 },
863
+ IPtrs : []* int {& vInt2 },
864
+ F : vFloat0 ,
865
+ FPtr : & vFloat1 ,
866
+ Fs : []float32 {vFloat0 },
867
+ FPtrs : []* float32 {& vFloat2 },
868
+ B : vBool0 ,
869
+ BPtr : & vBool1 ,
870
+ Bs : []bool {vBool0 },
871
+ BPtrs : []* bool {& vBool2 },
872
+ Bytes : vBytes0 ,
873
+ BytesPtr : & vBytes1 ,
874
+ S : vStr0 ,
875
+ SPtr : & vStr1 ,
876
+ Ss : []string {vStr0 },
877
+ SPtrs : []* string {& vStr2 },
878
+ MyI : MyInt (vInt0 ),
879
+ Child : Child {G : struct { I int }{I : vInt0 }},
880
+ Children : []Child {{G : struct { I int }{I : vInt0 }}},
881
+ ChildPtr : & Child {G : struct { I int }{I : vInt0 }},
882
+ ChildToEmbed : ChildToEmbed {X : vBool0 },
883
+ }
884
+ if err := Unmarshal ([]byte (emptyXML ), v ); err != nil {
885
+ t .Fatalf ("populated: Unmarshal failed: got %v" , err )
886
+ }
887
+
888
+ want = & Parent {
889
+ IPtr : & zInt ,
890
+ Is : []int {vInt0 , zInt },
891
+ IPtrs : []* int {& vInt0 , & zInt },
892
+ FPtr : & zFloat ,
893
+ Fs : []float32 {vFloat0 , zFloat },
894
+ FPtrs : []* float32 {& vFloat0 , & zFloat },
895
+ BPtr : & zBool ,
896
+ Bs : []bool {vBool0 , zBool },
897
+ BPtrs : []* bool {& vBool0 , & zBool },
898
+ Bytes : []byte {},
899
+ BytesPtr : & zBytes ,
900
+ SPtr : & zStr ,
901
+ Ss : []string {vStr0 , zStr },
902
+ SPtrs : []* string {& vStr0 , & zStr },
903
+ Child : Child {G : struct { I int }{I : vInt0 }}, // I should == zInt0? (zero value)
904
+ Children : []Child {{G : struct { I int }{I : vInt0 }}, {}},
905
+ ChildPtr : & Child {G : struct { I int }{I : vInt0 }}, // I should == zInt0? (zero value)
906
+ }
907
+ if ! reflect .DeepEqual (v , want ) {
908
+ t .Fatalf ("populated: Unmarshal:\n have: %#+v\n want: %#+v" , v , want )
909
+ }
910
+ }
0 commit comments