From ac1dc6c7afc2ec71aae592cf24a711c3017e6a5d Mon Sep 17 00:00:00 2001 From: benma Date: Mon, 20 Apr 2015 13:09:44 +0000 Subject: [PATCH] Add configuration option: ErrorUnsetFields. Allows the user to see if a a struct has not been populated fully. --- mapstructure.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mapstructure.go b/mapstructure.go index 381ba5d4..29ac92c0 100644 --- a/mapstructure.go +++ b/mapstructure.go @@ -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: // @@ -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 } }