lang: types: Add simple parsing of net.HardwareAddr
If we want to use special struct types from our CLI parser, we also need to be able to both identify, and convert them to our language type and value representations. For as long as we don't have fancier types in our language, these should both be strings. Tests and extensions to these additions are welcome!
This commit is contained in:
@@ -31,6 +31,7 @@ package types
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -184,6 +185,14 @@ func ConfigurableTypeOf(t reflect.Type, opts ...TypeOfOption) (*Type, error) {
|
|||||||
kind = typ.Kind()
|
kind = typ.Kind()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Special cases:
|
||||||
|
if reflect.TypeOf(net.HardwareAddr{}) == typ {
|
||||||
|
return &Type{
|
||||||
|
Kind: KindStr,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
// TODO: net/url.URL, time.Duration, etc. Note: avoid net/mail.Address
|
||||||
|
|
||||||
switch kind { // match on destination field kind
|
switch kind { // match on destination field kind
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
return &Type{
|
return &Type{
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ package types
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -111,6 +112,14 @@ func ValueOf(v reflect.Value) (Value, error) {
|
|||||||
value = value.Elem() // XXX: is this correct?
|
value = value.Elem() // XXX: is this correct?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Special cases:
|
||||||
|
if value.CanInterface() {
|
||||||
|
if v, ok := (value.Interface()).(net.HardwareAddr); ok {
|
||||||
|
return &StrValue{V: v.String()}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: net/url.URL, time.Duration, etc. Note: avoid net/mail.Address
|
||||||
|
|
||||||
switch kind { // match on destination field kind
|
switch kind { // match on destination field kind
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
return &BoolValue{V: value.Bool()}, nil
|
return &BoolValue{V: value.Bool()}, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user