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

Function TestDecode_NilPointerHook

mapstructure_test.go:1103–1138  ·  mapstructure_test.go::TestDecode_NilPointerHook
(t *testing.T)

Source from the content-addressed store, hash-verified

1101}
1102
1103func TestDecode_NilPointerHook(t *testing.T) {
1104 t.Parallel()
1105
1106 input := map[string]interface{}{
1107 "value": "",
1108 }
1109
1110 decodeHook := func(f, t reflect.Type, v interface{}) (interface{}, error) {
1111 if typed, ok := v.(string); ok {
1112 if typed == "" {
1113 return nil, nil
1114 }
1115 }
1116 return v, nil
1117 }
1118
1119 var result NilPointer
1120 config := &DecoderConfig{
1121 DecodeHook: decodeHook,
1122 Result: &result,
1123 }
1124
1125 decoder, err := NewDecoder(config)
1126 if err != nil {
1127 t.Fatalf("err: %s", err)
1128 }
1129
1130 err = decoder.Decode(input)
1131 if err != nil {
1132 t.Fatalf("got an err: %s", err)
1133 }
1134
1135 if result.Value != nil {
1136 t.Errorf("W should be nil: %#v", result.Value)
1137 }
1138}
1139
1140func TestDecode_FuncHook(t *testing.T) {
1141 t.Parallel()

Callers

nothing calls this directly

Calls 2

DecodeMethod · 0.95
NewDecoderFunction · 0.85

Tested by

no test coverage detected