Skip to content

Commit

Permalink
Implement encoding.TextUnmarshaler in mapstructure unmarshal. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Aug 19, 2022
1 parent ed3461c commit 0e9e5e9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion koanf.go
Expand Up @@ -239,7 +239,9 @@ func (ko *Koanf) UnmarshalWithConf(path string, o interface{}, c UnmarshalConf)
if c.DecoderConfig == nil {
c.DecoderConfig = &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc()),
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
mapstructure.TextUnmarshallerHookFunc()),
Metadata: nil,
Result: o,
WeaklyTypedInput: true,
Expand Down
42 changes: 42 additions & 0 deletions koanf_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -245,6 +246,26 @@ type testStructFlat struct {
Parent1Child1Grandchild1On bool `koanf:"parent1.child1.grandchild1.on"`
}

type customText int

func (c *customText) UnmarshalText(text []byte) error {
s := strings.ToLower(string(text))

switch {
case strings.HasSuffix(s, "mb"):
s = strings.TrimSuffix(s, "mb")
default:
s = "0"
}

v, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
*c = customText(v)
return nil
}

type Case struct {
koanf *koanf.Koanf
file string
Expand Down Expand Up @@ -922,6 +943,27 @@ func TestUnmarshalFlat(t *testing.T) {
}
}

func TestUnmarshalCustomText(t *testing.T) {
test := struct {
V1 customText `koanf:"v1"`
V2 string `koanf:"v2"`
V3 customText `koanf:"v3"`
}{}

// Test custom unmarshalling of strings via mapstructure's UnmarshalText()
// methods. customText is a int type that strips of the `mb` suffix and parses
// the rest into a number.

k := koanf.New(delim)
err := k.Load(rawbytes.Provider([]byte(`{"v1": "42mb", "v2": "42mb"}`)), json.Parser())
assert.NoError(t, err)

k.Unmarshal("", &test)
assert.Equal(t, int(test.V1), 42)
assert.Equal(t, test.V2, "42mb")
assert.Equal(t, int(test.V3), 0)
}

func TestMarshal(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit 0e9e5e9

Please sign in to comment.