From b85f81d52926f8f3e3527d14fd48651766bc5a84 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Tue, 5 Mar 2024 21:39:43 -0500 Subject: [PATCH] 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! --- lang/types/type.go | 9 +++++++++ lang/types/value.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/lang/types/type.go b/lang/types/type.go index 888d268f..5805a353 100644 --- a/lang/types/type.go +++ b/lang/types/type.go @@ -31,6 +31,7 @@ package types import ( "fmt" + "net" "reflect" "strings" @@ -184,6 +185,14 @@ func ConfigurableTypeOf(t reflect.Type, opts ...TypeOfOption) (*Type, error) { 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 case reflect.Bool: return &Type{ diff --git a/lang/types/value.go b/lang/types/value.go index cf6a8741..bf49de17 100644 --- a/lang/types/value.go +++ b/lang/types/value.go @@ -32,6 +32,7 @@ package types import ( "errors" "fmt" + "net" "reflect" "sort" "strconv" @@ -111,6 +112,14 @@ func ValueOf(v reflect.Value) (Value, error) { 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 case reflect.Bool: return &BoolValue{V: value.Bool()}, nil