diff --git a/mapstructure.go b/mapstructure.go index 7a16974..557f22c 100644 --- a/mapstructure.go +++ b/mapstructure.go @@ -1363,6 +1363,9 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e fieldName := field.Name tagValue := field.Tag.Get(d.config.TagName) + if tagValue == "" && d.config.IgnoreUntaggedFields { + continue + } tagValue = strings.SplitN(tagValue, ",", 2)[0] if tagValue != "" { fieldName = tagValue diff --git a/mapstructure_test.go b/mapstructure_test.go index 7e0ad4f..94a9e40 100644 --- a/mapstructure_test.go +++ b/mapstructure_test.go @@ -2735,6 +2735,44 @@ func TestDecoder_IgnoreUntaggedFields(t *testing.T) { } } +func TestDecoder_IgnoreUntaggedFieldsWithStruct(t *testing.T) { + type Output struct { + UntaggedInt int + TaggedNumber int `mapstructure:"tagged_number"` + UntaggedString string + TaggedString string `mapstructure:"tagged_string"` + } + input := map[interface{}]interface{}{ + "untaggedint": 31, + "tagged_number": 41, + "untagged_string": "hidden", + "tagged_string": "visible", + } + actual := Output{} + config := &DecoderConfig{ + Result: &actual, + IgnoreUntaggedFields: true, + } + + decoder, err := NewDecoder(config) + if err != nil { + t.Fatalf("err: %s", err) + } + + err = decoder.Decode(input) + if err != nil { + t.Fatalf("err: %s", err) + } + + expected := Output{ + TaggedNumber: 41, + TaggedString: "visible", + } + if !reflect.DeepEqual(expected, actual) { + t.Fatalf("Decode() expected: %#v\ngot: %#v", expected, actual) + } +} + func testSliceInput(t *testing.T, input map[string]interface{}, expected *Slice) { var result Slice err := Decode(input, &result)