Skip to content
Open
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
5 changes: 5 additions & 0 deletions mapstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,11 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
continue
}
tagValue = strings.SplitN(tagValue, ",", 2)[0]
// `mapstructure:"-"` opts the field out of decoding entirely. Don't
// then turn around and report a "-" key as Unset/missing.
if tagValue == "-" {
continue
}
if tagValue != "" {
fieldName = tagValue
} else {
Expand Down
28 changes: 28 additions & 0 deletions mapstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,34 @@ func TestDecoder_ErrorUnset(t *testing.T) {
}
}

func TestDecoder_ErrorUnset_IgnoresDashTaggedField(t *testing.T) {
// Regression for #89: a field tagged `mapstructure:"-"` is opted out
// of decoding, so ErrorUnset shouldn't flag it as missing.
t.Parallel()

input := map[string]any{
"foo": "bar",
}

var result struct {
Foo string `mapstructure:"foo"`
Bar string `mapstructure:"-"`
}
config := &DecoderConfig{
ErrorUnset: true,
Result: &result,
}

decoder, err := NewDecoder(config)
if err != nil {
t.Fatalf("err: %s", err)
}

if err := decoder.Decode(input); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

func TestDecoder_ErrorUnset_AllowUnsetPointer(t *testing.T) {
t.Parallel()

Expand Down