@@ -2589,3 +2589,191 @@ func TestClose(t *testing.T) {
2589
2589
})
2590
2590
}
2591
2591
}
2592
+
2593
+ type OptionalsEmpty struct {
2594
+ Sr string `xml:"sr"`
2595
+ So string `xml:"so,omitempty"`
2596
+ Sw string `xml:"-"`
2597
+
2598
+ Ir int `xml:"omitempty"` // actually named omitempty, not an option
2599
+ Io int `xml:"io,omitempty"`
2600
+
2601
+ Slr []string `xml:"slr,random"`
2602
+ Slo []string `xml:"slo,omitempty"`
2603
+
2604
+ Fr float64 `xml:"fr"`
2605
+ Fo float64 `xml:"fo,omitempty"`
2606
+
2607
+ Br bool `xml:"br"`
2608
+ Bo bool `xml:"bo,omitempty"`
2609
+
2610
+ Ur uint `xml:"ur"`
2611
+ Uo uint `xml:"uo,omitempty"`
2612
+
2613
+ Str struct {} `xml:"str"`
2614
+ Sto struct {} `xml:"sto,omitempty"`
2615
+ }
2616
+
2617
+ func TestOmitEmpty (t * testing.T ) {
2618
+ const want = `<OptionalsEmpty>
2619
+ <sr></sr>
2620
+ <omitempty>0</omitempty>
2621
+ <fr>0</fr>
2622
+ <br>false</br>
2623
+ <ur>0</ur>
2624
+ <str></str>
2625
+ <sto></sto>
2626
+ </OptionalsEmpty>`
2627
+ var o OptionalsEmpty
2628
+ o .Sw = "something"
2629
+
2630
+ got , err := MarshalIndent (& o , "" , " " )
2631
+ if err != nil {
2632
+ t .Fatalf ("MarshalIndent error: %v" , err )
2633
+ }
2634
+ if got := string (got ); got != want {
2635
+ t .Errorf ("MarshalIndent:\n \t got: %s\n \t want: %s\n " , indentNewlines (got ), indentNewlines (want ))
2636
+ }
2637
+ }
2638
+
2639
+ type NonZeroStruct struct {}
2640
+
2641
+ func (nzs NonZeroStruct ) IsZero () bool {
2642
+ return false
2643
+ }
2644
+
2645
+ type NoPanicStruct struct {
2646
+ Int int `xml:"int,omitzero"`
2647
+ }
2648
+
2649
+ func (nps * NoPanicStruct ) IsZero () bool {
2650
+ return nps .Int != 0
2651
+ }
2652
+
2653
+ type OptionalsZero struct {
2654
+ Sr string `xml:"sr"`
2655
+ So string `xml:"so,omitzero"`
2656
+ Sw string `xml:"-"`
2657
+
2658
+ Ir int `xml:"omitzero"` // actually named omitzero, not an option
2659
+ Io int `xml:"io,omitzero"`
2660
+
2661
+ Slr []string `xml:"slr,random"`
2662
+ Slo []string `xml:"slo,omitzero"`
2663
+ SloNonNil []string `xml:"slononnil,omitzero"`
2664
+
2665
+ Fr float64 `xml:"fr"`
2666
+ Fo float64 `xml:"fo,omitzero"`
2667
+ Foo float64 `xml:"foo,omitzero"`
2668
+ Foo2 [2 ]float64 `xml:"foo2,omitzero"`
2669
+
2670
+ Br bool `xml:"br"`
2671
+ Bo bool `xml:"bo,omitzero"`
2672
+
2673
+ Ur uint `xml:"ur"`
2674
+ Uo uint `xml:"uo,omitzero"`
2675
+
2676
+ Str struct {} `xml:"str"`
2677
+ Sto struct {} `xml:"sto,omitzero"`
2678
+
2679
+ Time time.Time `xml:"time,omitzero"`
2680
+ TimeLocal time.Time `xml:"timelocal,omitzero"`
2681
+ Nzs NonZeroStruct `xml:"nzs,omitzero"`
2682
+
2683
+ NilIsZeroer isZeroer `xml:"niliszeroer,omitzero"` // nil interface
2684
+ NonNilIsZeroer isZeroer `xml:"nonniliszeroer,omitzero"` // non-nil interface
2685
+ NoPanicStruct0 isZeroer `xml:"nps0,omitzero"` // non-nil interface with nil pointer
2686
+ NoPanicStruct1 isZeroer `xml:"nps1,omitzero"` // non-nil interface with non-nil pointer
2687
+ NoPanicStruct2 * NoPanicStruct `xml:"nps2,omitzero"` // nil pointer
2688
+ NoPanicStruct3 * NoPanicStruct `xml:"nps3,omitzero"` // non-nil pointer
2689
+ NoPanicStruct4 NoPanicStruct `xml:"nps4,omitzero"` // concrete type
2690
+ }
2691
+
2692
+ func TestOmitZero (t * testing.T ) {
2693
+ const want = `<OptionalsZero>
2694
+ <sr></sr>
2695
+ <omitzero>0</omitzero>
2696
+ <fr>0</fr>
2697
+ <br>false</br>
2698
+ <ur>0</ur>
2699
+ <str></str>
2700
+ <nzs></nzs>
2701
+ <nps1></nps1>
2702
+ <nps3></nps3>
2703
+ <nps4></nps4>
2704
+ </OptionalsZero>`
2705
+ var o OptionalsZero
2706
+ o .Sw = "something"
2707
+ o .SloNonNil = make ([]string , 0 )
2708
+
2709
+ o .Foo = - 0
2710
+ o .Foo2 = [2 ]float64 {+ 0 , - 0 }
2711
+
2712
+ o .TimeLocal = time.Time {}.Local ()
2713
+
2714
+ o .NonNilIsZeroer = time.Time {}
2715
+ o .NoPanicStruct0 = (* NoPanicStruct )(nil )
2716
+ o .NoPanicStruct1 = & NoPanicStruct {}
2717
+ o .NoPanicStruct3 = & NoPanicStruct {}
2718
+
2719
+ got , err := MarshalIndent (& o , "" , " " )
2720
+ if err != nil {
2721
+ t .Fatalf ("MarshalIndent error: %v" , err )
2722
+ }
2723
+ if got := string (got ); got != want {
2724
+ t .Errorf ("MarshalIndent:\n \t got: %s\n \t want: %s\n " , indentNewlines (got ), indentNewlines (want ))
2725
+ }
2726
+ }
2727
+
2728
+ type OptionalsEmptyZero struct {
2729
+ Sr string `xml:"sr"`
2730
+ So string `xml:"so,omitempty,omitzero"`
2731
+ Sw string `xml:"-"`
2732
+
2733
+ Io int `xml:"io,omitempty,omitzero"`
2734
+
2735
+ Slr []string `xml:"slr,random"`
2736
+ Slo []string `xml:"slo,omitempty,omitzero"`
2737
+ SloNonNil []string `xml:"slononnil,omitempty,omitzero"`
2738
+
2739
+ Fr float64 `xml:"fr"`
2740
+ Fo float64 `xml:"fo,omitempty,omitzero"`
2741
+
2742
+ Br bool `xml:"br"`
2743
+ Bo bool `xml:"bo,omitempty,omitzero"`
2744
+
2745
+ Ur uint `xml:"ur"`
2746
+ Uo uint `xml:"uo,omitempty,omitzero"`
2747
+
2748
+ Str struct {} `xml:"str"`
2749
+ Sto struct {} `xml:"sto,omitempty,omitzero"`
2750
+
2751
+ Time time.Time `xml:"time,omitempty,omitzero"`
2752
+ Nzs NonZeroStruct `xml:"nzs,omitempty,omitzero"`
2753
+ }
2754
+
2755
+ func TestOmitEmptyZero (t * testing.T ) {
2756
+ const want = `<OptionalsEmptyZero>
2757
+ <sr></sr>
2758
+ <fr>0</fr>
2759
+ <br>false</br>
2760
+ <ur>0</ur>
2761
+ <str></str>
2762
+ <nzs></nzs>
2763
+ </OptionalsEmptyZero>`
2764
+ var o OptionalsEmptyZero
2765
+ o .Sw = "something"
2766
+ o .SloNonNil = make ([]string , 0 )
2767
+
2768
+ got , err := MarshalIndent (& o , "" , " " )
2769
+ if err != nil {
2770
+ t .Fatalf ("MarshalIndent error: %v" , err )
2771
+ }
2772
+ if got := string (got ); got != want {
2773
+ t .Errorf ("MarshalIndent:\n \t got: %s\n \t want: %s\n " , indentNewlines (got ), indentNewlines (want ))
2774
+ }
2775
+ }
2776
+
2777
+ func indentNewlines (s string ) string {
2778
+ return strings .Join (strings .Split (s , "\n " ), "\n \t " )
2779
+ }
0 commit comments