Skip to content

ErrorUnset conflicts with UntaggedFields #89

@fedorkanin

Description

@fedorkanin

Issue mitchellh#350 in the original repo by mateusoliveira43 should be addressed:

If I use ErrorUnset = true and add mapstructure:"-" to my struct field, I get the following error:

* '' has unset fields: -

But since that field is ignored, it should not trigger an error, right?

Also tried using with IgnoreUntaggedFields = true, but got the same result

Current workaround is to check the mapstructure.Metadata manually. Minimal example:

md := mapstructure.Metadata{}
opts := func(mcfg *mapstructure.DecoderConfig) {
	mcfg.Metadata = &md
}
viper.Unmarshal(&cfg, opts)

// Ignore unset fields that have the "-" mapstruct tag.
md.Unset = slices.DeleteFunc(md.Unset, func(s string) bool {
	return strings.HasSuffix(s, ".-")
})

require.Empty(t, md.Unused, "unused config keys: %v", md.Unused)

Here is also a playground example, however it's build times out, playground has limited resources.

If this is by design? Any clarification is appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions