From 01b720cb1936eee10fc8e066054a5f5b4e2d9ad1 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Fri, 10 Jan 2020 12:15:38 +1100 Subject: [PATCH] Test and docs for TextUnmarshaler and json.Unmarshaler. --- README.md | 6 +++++- go.mod | 1 - go.sum | 2 -- mapper_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 50d3a2e..75c8c7e 100644 --- a/README.md +++ b/README.md @@ -392,7 +392,11 @@ specifies the element type. For maps, the tag has the format ## Custom decoders (mappers) -If a field implements the [MapperValue](https://godoc.org/github.com/alecthomas/kong#MapperValue) +Any field implementing `encoding.TextUnmarshaler` or `json.Unmarshaler` will use those interfaces +for decoding values. + +For more fine-grained control, if a field implements the +[MapperValue](https://godoc.org/github.com/alecthomas/kong#MapperValue) interface it will be used to decode arguments into the field. diff --git a/go.mod b/go.mod index c6f418f..8bbc12d 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,6 @@ module github.com/alecthomas/kong require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/mitchellh/mapstructure v1.1.2 github.com/pkg/errors v0.8.1 github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/testify v1.2.2 diff --git a/go.sum b/go.sum index 22a1f3c..c935d68 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= -github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/mapper_test.go b/mapper_test.go index 12def43..3338cff 100644 --- a/mapper_test.go +++ b/mapper_test.go @@ -2,11 +2,13 @@ package kong_test import ( "bytes" + "encoding/json" "fmt" "io/ioutil" "net/url" "os" "reflect" + "strings" "testing" "time" @@ -28,6 +30,45 @@ func TestValueMapper(t *testing.T) { require.Equal(t, "MOO", cli.Flag) } +type textUnmarshalerValue string + +func (m *textUnmarshalerValue) UnmarshalText(text []byte) error { + *m = textUnmarshalerValue(strings.ToUpper(string(text))) + return nil +} + +func TestTextUnmarshaler(t *testing.T) { + var cli struct { + Value textUnmarshalerValue + } + p := mustNew(t, &cli) + _, err := p.Parse([]string{"--value=hello"}) + require.NoError(t, err) + require.Equal(t, "HELLO", string(cli.Value)) +} + +type jsonUnmarshalerValue string + +func (j *jsonUnmarshalerValue) UnmarshalJSON(text []byte) error { + var v string + err := json.Unmarshal(text, &v) + if err != nil { + return err + } + *j = jsonUnmarshalerValue(strings.ToUpper(v)) + return nil +} + +func TestJSONUnmarshaler(t *testing.T) { + var cli struct { + Value jsonUnmarshalerValue + } + p := mustNew(t, &cli) + _, err := p.Parse([]string{"--value=hello"}) + require.NoError(t, err) + require.Equal(t, "HELLO", string(cli.Value)) +} + func TestNamedMapper(t *testing.T) { var cli struct { Flag string `type:"moo"`