From 18f04e66072764ddcfc0efc002cee13ac8b940c7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 14 Sep 2021 10:23:34 -0700 Subject: [PATCH] Default MatchName in NewDecoder --- mapstructure.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/mapstructure.go b/mapstructure.go index 3de5b089..dcee0f2d 100644 --- a/mapstructure.go +++ b/mapstructure.go @@ -381,6 +381,10 @@ func NewDecoder(config *DecoderConfig) (*Decoder, error) { config.TagName = "mapstructure" } + if config.MatchName == nil { + config.MatchName = strings.EqualFold + } + result := &Decoder{ config: config, } @@ -1345,7 +1349,7 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e continue } - if d.matchName(mK, fieldName) { + if d.config.MatchName(mK, fieldName) { rawMapKey = dataValKey rawMapVal = dataVal.MapIndex(dataValKey) break @@ -1433,14 +1437,6 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e return nil } -func (d *Decoder) matchName(mapKey, fieldName string) bool { - if d.config.MatchName != nil { - return d.config.MatchName(mapKey, fieldName) - } - - return strings.EqualFold(mapKey, fieldName) -} - func isEmptyValue(v reflect.Value) bool { switch getKind(v) { case reflect.Array, reflect.Map, reflect.Slice, reflect.String: