Skip to content

Commit 0855c8a

Browse files
Fabrice Vaillantcedric-cordenier
Fabrice Vaillant
authored andcommitted
Also precompute hook in decoder
1 parent 1036125 commit 0855c8a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

mapstructure.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ type DecoderConfig struct {
283283
// structure. The top-level Decode method is just a convenience that sets
284284
// up the most basic Decoder.
285285
type Decoder struct {
286-
config *DecoderConfig
286+
config *DecoderConfig
287+
cachedDecodeHook func(from reflect.Value, to reflect.Value) (interface{}, error)
287288
}
288289

289290
// Metadata contains information about decoding a structure that
@@ -408,6 +409,9 @@ func NewDecoder(config *DecoderConfig) (*Decoder, error) {
408409
result := &Decoder{
409410
config: config,
410411
}
412+
if config.DecodeHook != nil {
413+
result.cachedDecodeHook = cachedDecodeHook(config.DecodeHook)
414+
}
411415

412416
return result, nil
413417
}
@@ -462,10 +466,10 @@ func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) e
462466
return nil
463467
}
464468

465-
if d.config.DecodeHook != nil {
469+
if d.cachedDecodeHook != nil {
466470
// We have a DecodeHook, so let's pre-process the input.
467471
var err error
468-
input, err = DecodeHookExec(d.config.DecodeHook, inputVal, outVal)
472+
input, err = d.cachedDecodeHook(inputVal, outVal)
469473
if err != nil {
470474
return fmt.Errorf("error decoding '%s': %w", name, err)
471475
}

0 commit comments

Comments
 (0)