Skip to content

fix: do not report unset error for ignored or untagged fields when ErrorUnset is enabled - #202

Open
wwww-deeeee wants to merge 1 commit into
go-viper:mainfrom
wwww-deeeee:fix-issue-350-errorunset-ignored-fields
Open

wwww-deeeee wants to merge 1 commit into
go-viper:mainfrom
wwww-deeeee:fix-issue-350-errorunset-ignored-fields

Conversation

@wwww-deeeee

Copy link
Copy Markdown

Summary

Fixes mitchellh#350: ErrorUnset conflicts with UntaggedFields / ignored fields.

Root Cause

When ErrorUnset: true is set, decodeStruct iterates over struct fields. For fields with mapstructure:"-" tag (or untagged fields when IgnoreUntaggedFields: true), the field was not skipped. Instead, fieldName was set to "-", leading to a lookup in the map that failed, causing the field to be added to targetValKeysUnused and reporting '...' has unset fields: -.

Changes

  1. In decodeStruct, skip fields where tagValue == "-" or (tagValue == "" && d.config.IgnoreUntaggedFields).
  2. Added unit tests in mapstructure_test.go:
    • TestDecoder_ErrorUnset_IgnoredField
    • TestDecoder_ErrorUnset_IgnoredFieldWithConfig
    • TestDecoder_ErrorUnset_IgnoredUntaggedField
  3. All tests pass: go test ./... -> ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ErrorUnset conflicts with UntaggedFields

1 participant