Skip to content

Commit 093ac0b

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 093ac0b

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,62 @@ func TestStringToSliceHookFunc(t *testing.T) {
447447
}
448448
})
449449
}
450+
func TestStringToWeakSliceHookFunc(t *testing.T) {
451+
f := StringToWeakSliceHookFunc(",")
452+
453+
strValue := reflect.ValueOf("42")
454+
sliceValue := reflect.ValueOf([]string{"42"})
455+
sliceValue2 := reflect.ValueOf([]byte("42"))
456+
457+
cases := []struct {
458+
f, t reflect.Value
459+
result any
460+
err bool
461+
}{
462+
{sliceValue, sliceValue, []string{"42"}, false},
463+
{sliceValue2, sliceValue2, []byte("42"), false},
464+
{reflect.ValueOf([]byte("42")), reflect.ValueOf([]byte{}), []byte("42"), false},
465+
{strValue, strValue, "42", false},
466+
{
467+
reflect.ValueOf("foo,bar,baz"),
468+
sliceValue,
469+
[]string{"foo", "bar", "baz"},
470+
false,
471+
},
472+
{
473+
reflect.ValueOf("foo,bar,baz"),
474+
sliceValue2,
475+
[]string{"foo", "bar", "baz"},
476+
false,
477+
},
478+
{
479+
reflect.ValueOf(""),
480+
sliceValue,
481+
[]string{},
482+
false,
483+
},
484+
{
485+
reflect.ValueOf(""),
486+
sliceValue2,
487+
[]string{},
488+
false,
489+
},
490+
}
491+
492+
for i, tc := range cases {
493+
actual, err := DecodeHookExec(f, tc.f, tc.t)
494+
495+
if tc.err != (err != nil) {
496+
t.Fatalf("case %d: expected err %#v", i, tc.err)
497+
}
498+
499+
if !reflect.DeepEqual(actual, tc.result) {
500+
t.Fatalf(
501+
"case %d: expected %#v, got %#v",
502+
i, tc.result, actual)
503+
}
504+
}
505+
}
450506

451507
func TestStringToTimeDurationHookFunc(t *testing.T) {
452508
suite := decodeHookTestSuite[string, time.Duration]{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy