Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 1 addition & 49 deletions marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func isNil(v reflect.Value) bool {
}

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

func (enc *Encoder) encodeKv(b []byte, ctx encoderCtx, options valueOptions, v reflect.Value) ([]byte, error) {
Expand Down Expand Up @@ -418,54 +418,6 @@ func (enc *Encoder) commented(commented bool, b []byte) []byte {
return b
}

func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Struct:
return isEmptyStruct(v)
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
return false
}

func isEmptyStruct(v reflect.Value) bool {
// TODO: merge with walkStruct and cache.
typ := v.Type()
for i := 0; i < typ.NumField(); i++ {
fieldType := typ.Field(i)

// only consider exported fields
if fieldType.PkgPath != "" {
continue
}

tag := fieldType.Tag.Get("toml")

// special field name to skip field
if tag == "-" {
continue
}

f := v.Field(i)

if !isEmptyValue(f) {
return false
}
}

return true
}

const literalQuote = '\''

func (enc *Encoder) encodeString(b []byte, v string, options valueOptions) []byte {
Expand Down
22 changes: 22 additions & 0 deletions marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math"
"math/big"
"net/netip"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -1069,6 +1070,8 @@ func TestEncoderOmitempty(t *testing.T) {
Ptr *string `toml:",omitempty,multiline"`
Iface interface{} `toml:",omitempty,multiline"`
Struct struct{} `toml:",omitempty,multiline"`
Time time.Time `toml:",omitempty,multiline"`
IP netip.Addr `toml:",omitempty,multiline"`
}

d := doc{}
Expand All @@ -1081,6 +1084,25 @@ func TestEncoderOmitempty(t *testing.T) {
assert.Equal(t, expected, string(b))
}

func TestEncoderOmitemptyComparableStruct(t *testing.T) {
type doc struct {
Time time.Time `toml:",omitempty"`
IP netip.Addr `toml:",omitempty"`
}

d := doc{
Time: time.Date(2001, 2, 3, 4, 5, 6, 7, time.UTC),
IP: netip.MustParseAddr("192.168.178.35"),
}

b, err := toml.Marshal(d)
assert.NoError(t, err)

expected := "Time = 2001-02-03T04:05:06.000000007Z\nIP = '192.168.178.35'\n"

assert.Equal(t, expected, string(b))
}

func TestEncoderTagFieldName(t *testing.T) {
type doc struct {
String string `toml:"hello"`
Expand Down