From 38f8eb7c6bf0819f9f2e4e980067994d372a7b76 Mon Sep 17 00:00:00 2001 From: Pavel Borzenkov Date: Fri, 16 Nov 2018 17:09:52 +0300 Subject: [PATCH 1/2] Allow to use values (not pointers) with TextUnmarshaler The patch makes sure that both values and pointer to values are checked for custom TextUnmarshal implementation. This will allow to use go-arg custom parsing as follows: var args struct { Arg CustomType } instead of var args struct { Arg *CustomType } Signed-off-by: Pavel Borzenkov --- scalar.go | 9 ++++++++- scalar_test.go | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/scalar.go b/scalar.go index 663f143..e4525b3 100644 --- a/scalar.go +++ b/scalar.go @@ -47,6 +47,13 @@ func ParseValue(v reflect.Value, s string) error { if scalar, ok := v.Interface().(encoding.TextUnmarshaler); ok { return scalar.UnmarshalText([]byte(s)) } + // If it's a value instead of a pointer, check that we can unmarshal it + // via TextUnmarshaler as well + if v.CanAddr() { + if scalar, ok := v.Addr().Interface().(encoding.TextUnmarshaler); ok { + return scalar.UnmarshalText([]byte(s)) + } + } // If we have a pointer then dereference it if v.Kind() == reflect.Ptr { @@ -126,7 +133,7 @@ func ParseValue(v reflect.Value, s string) error { // CanParse returns true if the type can be parsed from a string. func CanParse(t reflect.Type) bool { // If it implements encoding.TextUnmarshaler then use that - if t.Implements(textUnmarshalerType) { + if t.Implements(textUnmarshalerType) || reflect.PtrTo(t).Implements(textUnmarshalerType) { return true } diff --git a/scalar_test.go b/scalar_test.go index d70bd32..9a1ef6a 100644 --- a/scalar_test.go +++ b/scalar_test.go @@ -10,6 +10,15 @@ import ( "github.com/stretchr/testify/require" ) +type textUnmarshaler struct { + val int +} + +func (f *textUnmarshaler) UnmarshalText(b []byte) error { + f.val = len(b) + return nil +} + func assertParse(t *testing.T, expected interface{}, str string) { v := reflect.New(reflect.TypeOf(expected)).Elem() err := ParseValue(v, str) @@ -67,6 +76,9 @@ func TestParseValue(t *testing.T) { // MAC addresses assertParse(t, net.HardwareAddr("\x01\x23\x45\x67\x89\xab"), "01:23:45:67:89:ab") + + // custom text unmarshaler + assertParse(t, textUnmarshaler{3}, "abc") } func TestParse(t *testing.T) { From e1338aeff04713f53befe44b42323f55fd60338c Mon Sep 17 00:00:00 2001 From: Pavel Borzenkov Date: Sun, 18 Nov 2018 14:02:17 +0300 Subject: [PATCH 2/2] Drop special handling of net.IP type It's now completely covered by generic TextUnmarshaler case. Signed-off-by: Pavel Borzenkov --- scalar.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/scalar.go b/scalar.go index e4525b3..073392c 100644 --- a/scalar.go +++ b/scalar.go @@ -18,7 +18,6 @@ var ( textUnmarshalerType = reflect.TypeOf([]encoding.TextUnmarshaler{}).Elem() durationType = reflect.TypeOf(time.Duration(0)) mailAddressType = reflect.TypeOf(mail.Address{}) - ipType = reflect.TypeOf(net.IP{}) macType = reflect.TypeOf(net.HardwareAddr{}) ) @@ -80,13 +79,6 @@ func ParseValue(v reflect.Value, s string) error { } v.Set(reflect.ValueOf(*addr)) return nil - case net.IP: - ip := net.ParseIP(s) - if ip == nil { - return fmt.Errorf(`invalid IP address: "%s"`, s) - } - v.Set(reflect.ValueOf(ip)) - return nil case net.HardwareAddr: ip, err := net.ParseMAC(s) if err != nil { @@ -144,7 +136,7 @@ func CanParse(t reflect.Type) bool { // Check for other special types switch t { - case durationType, mailAddressType, ipType, macType: + case durationType, mailAddressType, macType: return true }