Skip to content

Commit 01e5516

Browse files
committed
Skip fields with toml:"-", even when they're unsupported types
Previously something like this would fail to encode due to func being an unsupported type: struct { Str string `toml:"str" Func func() `toml:"-"` } But that's not really needed in this case, since we will never encode it anyway. Fixes #345
1 parent 87b9f05 commit 01e5516

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

encode.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
398398
if f.PkgPath != "" && !f.Anonymous { /// Skip unexported fields.
399399
continue
400400
}
401+
opts := getOptions(f.Tag)
402+
if opts.skip {
403+
continue
404+
}
401405

402406
frv := rv.Field(i)
403407

encode_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,33 @@ func TestEncode32bit(t *testing.T) {
468468
nil)
469469
}
470470

471+
// Skip invalid types if it has toml:"-"
472+
//
473+
// https://github.com/BurntSushi/toml/issues/345
474+
func TestEncodeSkipInvalidType(t *testing.T) {
475+
buf := new(bytes.Buffer)
476+
err := NewEncoder(buf).Encode(struct {
477+
Str string `toml:"str"`
478+
Arr []func() `toml:"-"`
479+
Map map[string]interface{} `toml:"-"`
480+
Func func() `toml:"-"`
481+
}{
482+
Str: "a",
483+
Arr: []func(){func() {}},
484+
Map: map[string]interface{}{"f": func() {}},
485+
Func: func() {},
486+
})
487+
if err != nil {
488+
t.Fatal(err)
489+
}
490+
491+
have := buf.String()
492+
want := "str = \"a\"\n"
493+
if have != want {
494+
t.Errorf("\nwant: %q\nhave: %q\n", want, have)
495+
}
496+
}
497+
471498
func encodeExpected(t *testing.T, label string, val interface{}, want string, wantErr error) {
472499
t.Helper()
473500

0 commit comments

Comments
 (0)