diff options
| author | Pavel Borzenkov <[email protected]> | 2018-11-20 12:18:45 +0300 |
|---|---|---|
| committer | Pavel Borzenkov <[email protected]> | 2018-11-20 12:18:45 +0300 |
| commit | 5225bc2f3cdb8a14f4eb49fdf46170da10ba49fc (patch) | |
| tree | 2aeac70c462acd97b73a9162ce7de04b934b6e16 /vendor/github.com/alexflint/go-scalar/scalar.go | |
| parent | f7c0423bd11ee80ab81d25c6d46f492998af8547 (diff) | |
vendor: update go-scalar to the latest version
Allows to use values (not pointer) with custom TextUnmarshaler.
Signed-off-by: Pavel Borzenkov <[email protected]>
Diffstat (limited to 'vendor/github.com/alexflint/go-scalar/scalar.go')
| -rw-r--r-- | vendor/github.com/alexflint/go-scalar/scalar.go | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/vendor/github.com/alexflint/go-scalar/scalar.go b/vendor/github.com/alexflint/go-scalar/scalar.go index 663f143..073392c 100644 --- a/vendor/github.com/alexflint/go-scalar/scalar.go +++ b/vendor/github.com/alexflint/go-scalar/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{}) ) @@ -47,6 +46,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 { @@ -73,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 { @@ -126,7 +125,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 } @@ -137,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 } |
