Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Add configuration option: ErrorUnsetFields. #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion mapstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type DecoderConfig struct {
// (extra keys).
ErrorUnused bool

// If ErrorUnset is true, then it is an error for there to exist fields in a struct that
// were not populated in the decoding process.
ErrorUnsetFields bool

// If WeaklyTypedInput is true, the decoder will make the following
// "weak" conversions:
//
Expand Down Expand Up @@ -628,7 +632,11 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)

if !rawMapVal.IsValid() {
// There was no matching key in the map for the value in
// the struct. Just ignore.
// the struct. Just ignore, unless configured to produce an error.
// Pointer fields are treated as optional.
if d.config.ErrorUnsetFields && field.Kind() != reflect.Ptr {
errors = appendErrors(errors, fmt.Errorf("unset struct key: %s", fieldName))
}
continue
}
}
Expand Down