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

Function TestDecode_DecodeHook

mapstructure_test.go:976–1010  ·  mapstructure_test.go::TestDecode_DecodeHook
(t *testing.T)

Source from the content-addressed store, hash-verified

974}
975
976func TestDecode_DecodeHook(t *testing.T) {
977 t.Parallel()
978
979 input := map[string]interface{}{
980 "vint": "WHAT",
981 }
982
983 decodeHook := func(from reflect.Kind, to reflect.Kind, v interface{}) (interface{}, error) {
984 if from == reflect.String && to != reflect.String {
985 return 5, nil
986 }
987
988 return v, nil
989 }
990
991 var result Basic
992 config := &DecoderConfig{
993 DecodeHook: decodeHook,
994 Result: &result,
995 }
996
997 decoder, err := NewDecoder(config)
998 if err != nil {
999 t.Fatalf("err: %s", err)
1000 }
1001
1002 err = decoder.Decode(input)
1003 if err != nil {
1004 t.Fatalf("got an err: %s", err)
1005 }
1006
1007 if result.Vint != 5 {
1008 t.Errorf("vint should be 5: %#v", result.Vint)
1009 }
1010}
1011
1012func TestDecode_DecodeHookType(t *testing.T) {
1013 t.Parallel()

Callers

nothing calls this directly

Calls 2

DecodeMethod · 0.95
NewDecoderFunction · 0.85

Tested by

no test coverage detected