From 0855c8a7b1fccea7a83e271a658636d4b0e07e39 Mon Sep 17 00:00:00 2001 From: Fabrice Vaillant Date: Fri, 14 Apr 2023 11:41:37 +0200 Subject: [PATCH] Also precompute hook in decoder --- mapstructure.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mapstructure.go b/mapstructure.go index 4b54fae..39c555d 100644 --- a/mapstructure.go +++ b/mapstructure.go @@ -283,7 +283,8 @@ type DecoderConfig struct { // structure. The top-level Decode method is just a convenience that sets // up the most basic Decoder. type Decoder struct { - config *DecoderConfig + config *DecoderConfig + cachedDecodeHook func(from reflect.Value, to reflect.Value) (interface{}, error) } // Metadata contains information about decoding a structure that @@ -408,6 +409,9 @@ func NewDecoder(config *DecoderConfig) (*Decoder, error) { result := &Decoder{ config: config, } + if config.DecodeHook != nil { + result.cachedDecodeHook = cachedDecodeHook(config.DecodeHook) + } return result, nil } @@ -462,10 +466,10 @@ func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) e return nil } - if d.config.DecodeHook != nil { + if d.cachedDecodeHook != nil { // We have a DecodeHook, so let's pre-process the input. var err error - input, err = DecodeHookExec(d.config.DecodeHook, inputVal, outVal) + input, err = d.cachedDecodeHook(inputVal, outVal) if err != nil { return fmt.Errorf("error decoding '%s': %w", name, err) }