Content-Length: 415057 | pFad | https://github.com/go-viper/mapstructure/commit/17cdcb0741054e2a33938adf6bd1f2a5c0aa8f30

CB feat: add back previous StringToSlice as a weak function · go-viper/mapstructure@17cdcb0 · GitHub
Skip to content

Commit 17cdcb0

Browse files
committed
feat: add back previous StringToSlice as a weak function
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
1 parent 3caca36 commit 17cdcb0

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

decode_hooks.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,29 @@ func StringToSliceHookFunc(sep string) DecodeHookFunc {
161161
}
162162
}
163163

164+
// StringToWeakSliceHookFunc brings back the old (pre-v2) behavior of [StringToSliceHookFunc].
165+
//
166+
// As of mapstructure v2.0.0 [StringToSliceHookFunc] checks if the return type is a string slice.
167+
// This function removes that check.
168+
func StringToWeakSliceHookFunc(sep string) DecodeHookFunc {
169+
return func(
170+
f reflect.Type,
171+
t reflect.Type,
172+
data any,
173+
) (any, error) {
174+
if f.Kind() != reflect.String || t.Kind() != reflect.Slice {
175+
return data, nil
176+
}
177+
178+
raw := data.(string)
179+
if raw == "" {
180+
return []string{}, nil
181+
}
182+
183+
return strings.Split(raw, sep), nil
184+
}
185+
}
186+
164187
// StringToTimeDurationHookFunc returns a DecodeHookFunc that converts
165188
// strings to time.Duration.
166189
func StringToTimeDurationHookFunc() DecodeHookFunc {

decode_hooks_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,63 @@ func TestStringToSliceHookFunc(t *testing.T) {
448448
})
449449
}
450450

451+
func TestStringToWeakSliceHookFunc(t *testing.T) {
452+
f := StringToWeakSliceHookFunc(",")
453+
454+
strValue := reflect.ValueOf("42")
455+
sliceValue := reflect.ValueOf([]string{"42"})
456+
sliceValue2 := reflect.ValueOf([]byte("42"))
457+
458+
cases := []struct {
459+
f, t reflect.Value
460+
result any
461+
err bool
462+
}{
463+
{sliceValue, sliceValue, []string{"42"}, false},
464+
{sliceValue2, sliceValue2, []byte("42"), false},
465+
{reflect.ValueOf([]byte("42")), reflect.ValueOf([]byte{}), []byte("42"), false},
466+
{strValue, strValue, "42", false},
467+
{
468+
reflect.ValueOf("foo,bar,baz"),
469+
sliceValue,
470+
[]string{"foo", "bar", "baz"},
471+
false,
472+
},
473+
{
474+
reflect.ValueOf("foo,bar,baz"),
475+
sliceValue2,
476+
[]string{"foo", "bar", "baz"},
477+
false,
478+
},
479+
{
480+
reflect.ValueOf(""),
481+
sliceValue,
482+
[]string{},
483+
false,
484+
},
485+
{
486+
reflect.ValueOf(""),
487+
sliceValue2,
488+
[]string{},
489+
false,
490+
},
491+
}
492+
493+
for i, tc := range cases {
494+
actual, err := DecodeHookExec(f, tc.f, tc.t)
495+
496+
if tc.err != (err != nil) {
497+
t.Fatalf("case %d: expected err %#v", i, tc.err)
498+
}
499+
500+
if !reflect.DeepEqual(actual, tc.result) {
501+
t.Fatalf(
502+
"case %d: expected %#v, got %#v",
503+
i, tc.result, actual)
504+
}
505+
}
506+
}
507+
451508
func TestStringToTimeDurationHookFunc(t *testing.T) {
452509
suite := decodeHookTestSuite[string, time.Duration]{
453510
fn: StringToTimeDurationHookFunc(),

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/go-viper/mapstructure/commit/17cdcb0741054e2a33938adf6bd1f2a5c0aa8f30

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy