@@ -255,6 +255,21 @@ type StructWithOmitEmpty struct {
255
255
OmitNestedField * Nested `mapstructure:"omittable-nested,omitempty"`
256
256
}
257
257
258
+ type StructWithOmitZero struct {
259
+ VisibleStringField string `mapstructure:"visible-string"`
260
+ OmitStringField string `mapstructure:"omittable-string,omitzero"`
261
+ VisibleIntField int `mapstructure:"visible-int"`
262
+ OmitIntField int `mapstructure:"omittable-int,omitzero"`
263
+ VisibleFloatField float64 `mapstructure:"visible-float"`
264
+ OmitFloatField float64 `mapstructure:"omittable-float,omitzero"`
265
+ VisibleSliceField []interface {} `mapstructure:"visible-slice"`
266
+ OmitSliceField []interface {} `mapstructure:"omittable-slice,omitzero"`
267
+ VisibleMapField map [string ]interface {} `mapstructure:"visible-map"`
268
+ OmitMapField map [string ]interface {} `mapstructure:"omittable-map,omitzero"`
269
+ NestedField * Nested `mapstructure:"visible-nested"`
270
+ OmitNestedField * Nested `mapstructure:"omittable-nested,omitzero"`
271
+ }
272
+
258
273
type TypeConversionResult struct {
259
274
IntToFloat float32
260
275
IntToUint uint
@@ -2932,6 +2947,88 @@ func TestDecode_StructTaggedWithOmitempty_KeepNonEmptyValues(t *testing.T) {
2932
2947
}
2933
2948
}
2934
2949
2950
+ func TestDecode_StructTaggedWithOmitzero_KeepNonZeroValues (t * testing.T ) {
2951
+ t .Parallel ()
2952
+
2953
+ input := & StructWithOmitZero {
2954
+ VisibleStringField : "" ,
2955
+ OmitStringField : "string" ,
2956
+ VisibleIntField : 0 ,
2957
+ OmitIntField : 1 ,
2958
+ VisibleFloatField : 0.0 ,
2959
+ OmitFloatField : 1.0 ,
2960
+ VisibleSliceField : nil ,
2961
+ OmitSliceField : []interface {}{},
2962
+ VisibleMapField : nil ,
2963
+ OmitMapField : map [string ]interface {}{},
2964
+ NestedField : nil ,
2965
+ OmitNestedField : & Nested {},
2966
+ }
2967
+
2968
+ var emptySlice []interface {}
2969
+ var emptyMap map [string ]interface {}
2970
+ var emptyNested * Nested
2971
+ expected := & map [string ]interface {}{
2972
+ "visible-string" : "" ,
2973
+ "omittable-string" : "string" ,
2974
+ "visible-int" : 0 ,
2975
+ "omittable-int" : 1 ,
2976
+ "visible-float" : 0.0 ,
2977
+ "omittable-float" : 1.0 ,
2978
+ "visible-slice" : emptySlice ,
2979
+ "omittable-slice" : []interface {}{},
2980
+ "visible-map" : emptyMap ,
2981
+ "omittable-map" : map [string ]interface {}{},
2982
+ "visible-nested" : emptyNested ,
2983
+ "omittable-nested" : & Nested {},
2984
+ }
2985
+
2986
+ actual := & map [string ]interface {}{}
2987
+ Decode (input , actual )
2988
+
2989
+ if ! reflect .DeepEqual (actual , expected ) {
2990
+ t .Fatalf ("Decode() expected: %#v, got: %#v" , expected , actual )
2991
+ }
2992
+ }
2993
+
2994
+ func TestDecode_StructTaggedWithOmitzero_DropZeroValues (t * testing.T ) {
2995
+ t .Parallel ()
2996
+
2997
+ input := & StructWithOmitZero {
2998
+ VisibleStringField : "" ,
2999
+ OmitStringField : "" ,
3000
+ VisibleIntField : 0 ,
3001
+ OmitIntField : 0 ,
3002
+ VisibleFloatField : 0.0 ,
3003
+ OmitFloatField : 0.0 ,
3004
+ VisibleSliceField : nil ,
3005
+ OmitSliceField : nil ,
3006
+ VisibleMapField : nil ,
3007
+ OmitMapField : nil ,
3008
+ NestedField : nil ,
3009
+ OmitNestedField : nil ,
3010
+ }
3011
+
3012
+ var emptySlice []interface {}
3013
+ var emptyMap map [string ]interface {}
3014
+ var emptyNested * Nested
3015
+ expected := & map [string ]interface {}{
3016
+ "visible-string" : "" ,
3017
+ "visible-int" : 0 ,
3018
+ "visible-float" : 0.0 ,
3019
+ "visible-slice" : emptySlice ,
3020
+ "visible-map" : emptyMap ,
3021
+ "visible-nested" : emptyNested ,
3022
+ }
3023
+
3024
+ actual := & map [string ]interface {}{}
3025
+ Decode (input , actual )
3026
+
3027
+ if ! reflect .DeepEqual (actual , expected ) {
3028
+ t .Fatalf ("Decode() expected: %#v, got: %#v" , expected , actual )
3029
+ }
3030
+ }
3031
+
2935
3032
func TestDecode_mapToStruct (t * testing.T ) {
2936
3033
type Target struct {
2937
3034
String string
0 commit comments