| 1 |
// Copyright (c) 2013 Mitchell Hashimoto |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
// This code is a modified version of https://github.com/go-viper/mapstructure |
| 4 |
|
| 5 |
package composehook // import "go.opentelemetry.io/collector/confmap/internal/third_party/composehook" |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"reflect" |
| 10 |
|
| 11 |
"github.com/go-viper/mapstructure/v2" |
| 12 |
) |
| 13 |
|
| 14 |
// typedDecodeHook takes a raw DecodeHookFunc (an any) and turns |
| 15 |
// it into the proper DecodeHookFunc type, such as DecodeHookFuncType. |
| 16 |
func typedDecodeHook(h mapstructure.DecodeHookFunc) mapstructure.DecodeHookFunc { |
| 17 |
// Create variables here so we can reference them with the reflect pkg |
| 18 |
var f1 mapstructure.DecodeHookFuncType |
| 19 |
var f2 mapstructure.DecodeHookFuncKind |
| 20 |
var f3 mapstructure.DecodeHookFuncValue |
| 21 |
|
| 22 |
// Fill in the variables into this interface and the rest is done |
| 23 |
// automatically using the reflect package. |
| 24 |
potential := []any{f3, f1, f2} |
| 25 |
|
| 26 |
v := reflect.ValueOf(h) |
| 27 |
vt := v.Type() |
| 28 |
for _, raw := range potential { |
| 29 |
pt := reflect.ValueOf(raw).Type() |
| 30 |
if vt.ConvertibleTo(pt) { |
| 31 |
return v.Convert(pt).Interface() |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
// cachedDecodeHook takes a raw DecodeHookFunc (an any) and turns |
| 39 |
// it into a closure to be used directly |
| 40 |
// if the type fails to convert we return a closure always erroring to keep the previous behavior |
| 41 |
func cachedDecodeHook(raw mapstructure.DecodeHookFunc) func(reflect.Value, reflect.Value) (any, error) { |
| 42 |
switch f := typedDecodeHook(raw).(type) { |
| 43 |
case mapstructure.DecodeHookFuncType: |
| 44 |
return func(from, to reflect.Value) (any, error) { |
| 45 |
// CHANGE FROM UPSTREAM: check if from is valid and return nil if not |
| 46 |
if !from.IsValid() { |
| 47 |
return nil, nil |
| 48 |
} |
| 49 |
return f(from.Type(), to.Type(), from.Interface()) |
| 50 |
} |
| 51 |
case mapstructure.DecodeHookFuncKind: |
| 52 |
return func(from, to reflect.Value) (any, error) { |
| 53 |
// CHANGE FROM UPSTREAM: check if from is valid and return nil if not |
| 54 |
if !from.IsValid() { |
| 55 |
return nil, nil |
| 56 |
} |
| 57 |
return f(from.Kind(), to.Kind(), from.Interface()) |
| 58 |
} |
| 59 |
case mapstructure.DecodeHookFuncValue: |
| 60 |
return func(from, to reflect.Value) (any, error) { |
| 61 |
return f(from, to) |
| 62 |
} |
| 63 |
default: |
| 64 |
return func(reflect.Value, reflect.Value) (any, error) { |
| 65 |
return nil, errors.New("invalid decode hook signature") |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// ComposeDecodeHookFunc creates a single DecodeHookFunc that |
| 71 |
// automatically composes multiple DecodeHookFuncs. |
| 72 |
// |
| 73 |
// The composed funcs are called in order, with the result of the |
| 74 |
// previous transformation. |
| 75 |
// |
| 76 |
// This is a copy of [mapstructure.ComposeDecodeHookFunc] but with |
| 77 |
// validation added. |
| 78 |
func ComposeDecodeHookFunc(fs ...mapstructure.DecodeHookFunc) mapstructure.DecodeHookFunc { |
| 79 |
cached := make([]func(reflect.Value, reflect.Value) (any, error), 0, len(fs)) |
| 80 |
for _, f := range fs { |
| 81 |
cached = append(cached, cachedDecodeHook(f)) |
| 82 |
} |
| 83 |
return func(f, t reflect.Value) (any, error) { |
| 84 |
var err error |
| 85 |
|
| 86 |
// CHANGE FROM UPSTREAM: check if f is valid before calling f.Interface() |
| 87 |
var data any |
| 88 |
if f.IsValid() { |
| 89 |
data = f.Interface() |
| 90 |
} |
| 91 |
|
| 92 |
newFrom := f |
| 93 |
for _, c := range cached { |
| 94 |
data, err = c(newFrom, t) |
| 95 |
if err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
newFrom = reflect.ValueOf(data) |
| 99 |
} |
| 100 |
|
| 101 |
return data, nil |
| 102 |
} |
| 103 |
} |