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

Commit e13a952

Browse files
committed
Add configuration option: ErrorUnsetFields.
Allows the user to see if a a struct has not been populated fully.
1 parent 442e588 commit e13a952

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

mapstructure.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ type DecoderConfig struct {
4040
// (extra keys).
4141
ErrorUnused bool
4242

43+
// If ErrorUnset is true, then it is an error for there to exist fields in a struct that
44+
// were not populated in the decoding process.
45+
ErrorUnsetFields bool
46+
4347
// If WeaklyTypedInput is true, the decoder will make the following
4448
// "weak" conversions:
4549
//
@@ -628,7 +632,10 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
628632

629633
if !rawMapVal.IsValid() {
630634
// There was no matching key in the map for the value in
631-
// the struct. Just ignore.
635+
// the struct. Just ignore, unless configured to produce an error.
636+
if d.config.ErrorUnsetFields {
637+
errors = appendErrors(errors, fmt.Errorf("unset struct key: %s", fieldName))
638+
}
632639
continue
633640
}
634641
}

0 commit comments

Comments
 (0)