(t *testing.T)
| 895 | } |
| 896 | |
| 897 | func TestUnmarshalWithDefaultDecodeHook(t *testing.T) { |
| 898 | opt := mapstructure.ComposeDecodeHookFunc( |
| 899 | mapstructure.StringToTimeDurationHookFunc(), |
| 900 | mapstructure.StringToSliceHookFunc(","), |
| 901 | // Custom Decode Hook Function |
| 902 | func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) { |
| 903 | if rf != reflect.String || rt != reflect.Map { |
| 904 | return data, nil |
| 905 | } |
| 906 | m := map[string]string{} |
| 907 | raw := data.(string) |
| 908 | if raw == "" { |
| 909 | return m, nil |
| 910 | } |
| 911 | err := json.Unmarshal([]byte(raw), &m) |
| 912 | return m, err |
| 913 | }, |
| 914 | ) |
| 915 | |
| 916 | v := NewWithOptions(WithDecodeHook(opt)) |
| 917 | v.Set("credentials", "{\"foo\":\"bar\"}") |
| 918 | |
| 919 | type config struct { |
| 920 | Credentials map[string]string |
| 921 | } |
| 922 | |
| 923 | var C config |
| 924 | |
| 925 | require.NoError(t, v.Unmarshal(&C), "unable to decode into struct") |
| 926 | |
| 927 | assert.Equal(t, &config{ |
| 928 | Credentials: map[string]string{"foo": "bar"}, |
| 929 | }, &C) |
| 930 | } |
| 931 | |
| 932 | func TestUnmarshalWithDecoderOptions(t *testing.T) { |
| 933 | v := New() |
nothing calls this directly
no test coverage detected