add support for url.URL
This commit is contained in:
parent
f33108a28e
commit
9e23a0e262
11
scalar.go
11
scalar.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -19,6 +20,7 @@ var (
|
||||||
durationType = reflect.TypeOf(time.Duration(0))
|
durationType = reflect.TypeOf(time.Duration(0))
|
||||||
mailAddressType = reflect.TypeOf(mail.Address{})
|
mailAddressType = reflect.TypeOf(mail.Address{})
|
||||||
macType = reflect.TypeOf(net.HardwareAddr{})
|
macType = reflect.TypeOf(net.HardwareAddr{})
|
||||||
|
urlType = reflect.TypeOf(url.URL{})
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -86,6 +88,13 @@ func ParseValue(v reflect.Value, s string) error {
|
||||||
}
|
}
|
||||||
v.Set(reflect.ValueOf(ip))
|
v.Set(reflect.ValueOf(ip))
|
||||||
return nil
|
return nil
|
||||||
|
case url.URL:
|
||||||
|
url, err := url.Parse(s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
v.Set(reflect.ValueOf(*url))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Switch on kind so that we can handle derived types
|
// Switch on kind so that we can handle derived types
|
||||||
|
@ -136,7 +145,7 @@ func CanParse(t reflect.Type) bool {
|
||||||
|
|
||||||
// Check for other special types
|
// Check for other special types
|
||||||
switch t {
|
switch t {
|
||||||
case durationType, mailAddressType, macType:
|
case durationType, mailAddressType, macType, urlType:
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package scalar
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -77,6 +78,9 @@ func TestParseValue(t *testing.T) {
|
||||||
// MAC addresses
|
// MAC addresses
|
||||||
assertParse(t, net.HardwareAddr("\x01\x23\x45\x67\x89\xab"), "01:23:45:67:89:ab")
|
assertParse(t, net.HardwareAddr("\x01\x23\x45\x67\x89\xab"), "01:23:45:67:89:ab")
|
||||||
|
|
||||||
|
// URL
|
||||||
|
assertParse(t, url.URL{Scheme: "https", Host: "example.com", Path: "/a/b/c"}, "https://example.com/a/b/c")
|
||||||
|
|
||||||
// custom text unmarshaler
|
// custom text unmarshaler
|
||||||
assertParse(t, textUnmarshaler{3}, "abc")
|
assertParse(t, textUnmarshaler{3}, "abc")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue