MCPcopy
hub / github.com/mitchellh/mapstructure / TestDecode_FuncHook

Function TestDecode_FuncHook

mapstructure_test.go:1140–1174  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1138}
1139
1140func TestDecode_FuncHook(t *testing.T) {
1141 t.Parallel()
1142
1143 input := map[string]interface{}{
1144 "foo": "baz",
1145 }
1146
1147 decodeHook := func(f, t reflect.Type, v interface{}) (interface{}, error) {
1148 if t.Kind() != reflect.Func {
1149 return v, nil
1150 }
1151 val := v.(string)
1152 return func() string { return val }, nil
1153 }
1154
1155 var result Func
1156 config := &DecoderConfig{
1157 DecodeHook: decodeHook,
1158 Result: &result,
1159 }
1160
1161 decoder, err := NewDecoder(config)
1162 if err != nil {
1163 t.Fatalf("err: %s", err)
1164 }
1165
1166 err = decoder.Decode(input)
1167 if err != nil {
1168 t.Fatalf("got an err: %s", err)
1169 }
1170
1171 if result.Foo() != "baz" {
1172 t.Errorf("Foo call result should be 'baz': %s", result.Foo())
1173 }
1174}
1175
1176func TestDecode_NonStruct(t *testing.T) {
1177 t.Parallel()

Callers

nothing calls this directly

Calls 2

DecodeMethod · 0.95
NewDecoderFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…