Skip to content

Commit 2d8433b

Browse files
authored
Encode: don't inherit omitempty (#803)
Fixes #786.
1 parent 67bc542 commit 2d8433b

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

marshaler.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ func isNil(v reflect.Value) bool {
346346
}
347347
}
348348

349-
func shouldOmitEmpty(ctx encoderCtx, options valueOptions, v reflect.Value) bool {
350-
return (ctx.options.omitempty || options.omitempty) && isEmptyValue(v)
349+
func shouldOmitEmpty(options valueOptions, v reflect.Value) bool {
350+
return options.omitempty && isEmptyValue(v)
351351
}
352352

353353
func (enc *Encoder) encodeKv(b []byte, ctx encoderCtx, options valueOptions, v reflect.Value) ([]byte, error) {
@@ -801,7 +801,7 @@ func (enc *Encoder) encodeTable(b []byte, ctx encoderCtx, t table) ([]byte, erro
801801

802802
hasNonEmptyKV := false
803803
for _, kv := range t.kvs {
804-
if shouldOmitEmpty(ctx, kv.Options, kv.Value) {
804+
if shouldOmitEmpty(kv.Options, kv.Value) {
805805
continue
806806
}
807807
hasNonEmptyKV = true
@@ -818,7 +818,7 @@ func (enc *Encoder) encodeTable(b []byte, ctx encoderCtx, t table) ([]byte, erro
818818

819819
first := true
820820
for _, table := range t.tables {
821-
if shouldOmitEmpty(ctx, table.Options, table.Value) {
821+
if shouldOmitEmpty(table.Options, table.Value) {
822822
continue
823823
}
824824
if first {
@@ -850,7 +850,7 @@ func (enc *Encoder) encodeTableInline(b []byte, ctx encoderCtx, t table) ([]byte
850850

851851
first := true
852852
for _, kv := range t.kvs {
853-
if shouldOmitEmpty(ctx, kv.Options, kv.Value) {
853+
if shouldOmitEmpty(kv.Options, kv.Value) {
854854
continue
855855
}
856856

marshaler_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,51 @@ func TestIssue786(t *testing.T) {
10481048
require.NoError(t, err)
10491049

10501050
require.Equal(t, "", string(b))
1051+
1052+
type General struct {
1053+
From string `toml:"from,omitempty" json:"from,omitempty" comment:"from in graphite-web format, the local TZ is used"`
1054+
Randomize bool `toml:"randomize" json:"randomize" comment:"randomize starting time with [0,step)"`
1055+
}
1056+
1057+
type Custom struct {
1058+
Name string `toml:"name" json:"name,omitempty" comment:"names for generator, braces are expanded like in shell"`
1059+
Type string `toml:"type,omitempty" json:"type,omitempty" comment:"type of generator"`
1060+
General
1061+
}
1062+
type Config struct {
1063+
General
1064+
Custom []Custom `toml:"custom,omitempty" json:"custom,omitempty" comment:"generators with custom parameters can be specified separately"`
1065+
}
1066+
1067+
buf := new(bytes.Buffer)
1068+
config := &Config{General: General{From: "-2d", Randomize: true}}
1069+
config.Custom = []Custom{{Name: "omit", General: General{Randomize: false}}}
1070+
config.Custom = append(config.Custom, Custom{Name: "present", General: General{From: "-2d", Randomize: true}})
1071+
encoder := toml.NewEncoder(buf)
1072+
encoder.Encode(config)
1073+
1074+
expected := `# from in graphite-web format, the local TZ is used
1075+
from = '-2d'
1076+
# randomize starting time with [0,step)
1077+
randomize = true
1078+
1079+
# generators with custom parameters can be specified separately
1080+
[[custom]]
1081+
# names for generator, braces are expanded like in shell
1082+
name = 'omit'
1083+
# randomize starting time with [0,step)
1084+
randomize = false
1085+
1086+
[[custom]]
1087+
# names for generator, braces are expanded like in shell
1088+
name = 'present'
1089+
# from in graphite-web format, the local TZ is used
1090+
from = '-2d'
1091+
# randomize starting time with [0,step)
1092+
randomize = true
1093+
`
1094+
1095+
require.Equal(t, expected, buf.String())
10511096
}
10521097

10531098
func TestMarshalNestedAnonymousStructs(t *testing.T) {

0 commit comments

Comments
 (0)