Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DecoderConfig: Introduce IgnoreUntaggedFields #277

Merged
merged 1 commit into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions mapstructure.go
Expand Up @@ -259,6 +259,10 @@ type DecoderConfig struct {
// defaults to "mapstructure"
TagName string

// IgnoreUntaggedFields ignores all struct fields without explicit
// TagName, comparable to `mapstructure:"-"` as default behaviour.
IgnoreUntaggedFields bool

// MatchName is the function used to match the map key to the struct
// field name or tag. Defaults to `strings.EqualFold`. This can be used
// to implement case-sensitive tag values, support snake casing, etc.
Expand Down Expand Up @@ -906,6 +910,10 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
tagValue := f.Tag.Get(d.config.TagName)
keyName := f.Name

if tagValue == "" && d.config.IgnoreUntaggedFields {
continue
}

// If Squash is set in the config, we squash the field down.
squash := d.config.Squash && v.Kind() == reflect.Struct && f.Anonymous

Expand Down
40 changes: 40 additions & 0 deletions mapstructure_test.go
Expand Up @@ -2482,6 +2482,46 @@ func TestDecoder_MatchName(t *testing.T) {
}
}

func TestDecoder_IgnoreUntaggedFields(t *testing.T) {
type Input struct {
UntaggedNumber int
TaggedNumber int `mapstructure:"tagged_number"`
UntaggedString string
TaggedString string `mapstructure:"tagged_string"`
}
input := &Input{
UntaggedNumber: 31,
TaggedNumber: 42,
UntaggedString: "hidden",
TaggedString: "visible",
}

actual := make(map[string]interface{})
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 := map[string]interface{}{
"tagged_number": 42,
"tagged_string": "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)
Expand Down