From 8249b7bc958927e7a8b392f66adbe4d5ead737d9 Mon Sep 17 00:00:00 2001 From: Cameron Howey Date: Mon, 3 Aug 2015 11:06:26 -0700 Subject: [PATCH 1/2] Ignore field options (omitempty,omitzero) during Decode --- type_fields.go | 1 + 1 file changed, 1 insertion(+) diff --git a/type_fields.go b/type_fields.go index 7592f87a..ca3118ae 100644 --- a/type_fields.go +++ b/type_fields.go @@ -112,6 +112,7 @@ func typeFields(t reflect.Type) []field { // Record found field and index sequence. if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct { tagged := name != "" + name, _ = getOptions(name) if name == "" { name = sf.Name } From b7ce90f31dffcf069d41133c95fac4a3cfaf9abb Mon Sep 17 00:00:00 2001 From: Cameron Howey Date: Fri, 4 Sep 2015 15:02:08 -0700 Subject: [PATCH 2/2] Add test for omitempty on Decode --- decode_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/decode_test.go b/decode_test.go index 3805931f..1f38ceb3 100644 --- a/decode_test.go +++ b/decode_test.go @@ -349,6 +349,25 @@ func TestDecodeSizedInts(t *testing.T) { } } +func TestDecodeWithOmitEmpty(t *testing.T) { + type userpass struct { + User string `toml:"user"` + Pass string `toml:"password,omitempty"` + } + value := userpass{"Testing", "Filled"} + toml := ` + user = "Testing" + password = "Filled" + ` + var up userpass + if _, err := Decode(toml, &up); err != nil { + t.Fatal(err.Error()) + } + if value != up { + t.Fatalf("Expected %#v but got %#v", value, up) + } +} + func TestUnmarshaler(t *testing.T) { var tomlBlob = `