Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode: don't inherit omitempty #803

Merged
merged 1 commit into from Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions marshaler.go
Expand Up @@ -346,8 +346,8 @@ func isNil(v reflect.Value) bool {
}
}

func shouldOmitEmpty(ctx encoderCtx, options valueOptions, v reflect.Value) bool {
return (ctx.options.omitempty || options.omitempty) && isEmptyValue(v)
func shouldOmitEmpty(options valueOptions, v reflect.Value) bool {
return options.omitempty && isEmptyValue(v)
}

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

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

first := true
for _, table := range t.tables {
if shouldOmitEmpty(ctx, table.Options, table.Value) {
if shouldOmitEmpty(table.Options, table.Value) {
continue
}
if first {
Expand Down Expand Up @@ -850,7 +850,7 @@ func (enc *Encoder) encodeTableInline(b []byte, ctx encoderCtx, t table) ([]byte

first := true
for _, kv := range t.kvs {
if shouldOmitEmpty(ctx, kv.Options, kv.Value) {
if shouldOmitEmpty(kv.Options, kv.Value) {
continue
}

Expand Down
45 changes: 45 additions & 0 deletions marshaler_test.go
Expand Up @@ -1048,6 +1048,51 @@ func TestIssue786(t *testing.T) {
require.NoError(t, err)

require.Equal(t, "", string(b))

type General struct {
From string `toml:"from,omitempty" json:"from,omitempty" comment:"from in graphite-web format, the local TZ is used"`
Randomize bool `toml:"randomize" json:"randomize" comment:"randomize starting time with [0,step)"`
}

type Custom struct {
Name string `toml:"name" json:"name,omitempty" comment:"names for generator, braces are expanded like in shell"`
Type string `toml:"type,omitempty" json:"type,omitempty" comment:"type of generator"`
General
}
type Config struct {
General
Custom []Custom `toml:"custom,omitempty" json:"custom,omitempty" comment:"generators with custom parameters can be specified separately"`
}

buf := new(bytes.Buffer)
config := &Config{General: General{From: "-2d", Randomize: true}}
config.Custom = []Custom{{Name: "omit", General: General{Randomize: false}}}
config.Custom = append(config.Custom, Custom{Name: "present", General: General{From: "-2d", Randomize: true}})
encoder := toml.NewEncoder(buf)
encoder.Encode(config)

expected := `# from in graphite-web format, the local TZ is used
from = '-2d'
# randomize starting time with [0,step)
randomize = true

# generators with custom parameters can be specified separately
[[custom]]
# names for generator, braces are expanded like in shell
name = 'omit'
# randomize starting time with [0,step)
randomize = false

[[custom]]
# names for generator, braces are expanded like in shell
name = 'present'
# from in graphite-web format, the local TZ is used
from = '-2d'
# randomize starting time with [0,step)
randomize = true
`

require.Equal(t, expected, buf.String())
}

func TestMarshalNestedAnonymousStructs(t *testing.T) {
Expand Down