diff options
| author | Alex Flint <[email protected]> | 2022-06-09 11:21:29 -0400 |
|---|---|---|
| committer | Alex Flint <[email protected]> | 2022-06-09 11:21:29 -0400 |
| commit | 23b2b67fe299b63a072a3541f34d57757d0b8df0 (patch) | |
| tree | b5abb6cece5d2829bb134cf7e7c0d58216595035 /reflect.go | |
| parent | f0f44b65d1179ccedb4c56f493f97ec569a6654e (diff) | |
fix issue #184
Diffstat (limited to 'reflect.go')
| -rw-r--r-- | reflect.go | 19 |
1 files changed, 16 insertions, 3 deletions
@@ -10,7 +10,10 @@ import ( scalar "github.com/alexflint/go-scalar" ) -var textUnmarshalerType = reflect.TypeOf([]encoding.TextUnmarshaler{}).Elem() +var ( + textMarshalerType = reflect.TypeOf([]encoding.TextMarshaler{}).Elem() + textUnmarshalerType = reflect.TypeOf([]encoding.TextUnmarshaler{}).Elem() +) // cardinality tracks how many tokens are expected for a given spec // - zero is a boolean, which does to expect any value @@ -74,10 +77,10 @@ func cardinalityOf(t reflect.Type) (cardinality, error) { } } -// isBoolean returns true if the type can be parsed from a single string +// isBoolean returns true if the type is a boolean or a pointer to a boolean func isBoolean(t reflect.Type) bool { switch { - case t.Implements(textUnmarshalerType): + case isTextUnmarshaler(t): return false case t.Kind() == reflect.Bool: return true @@ -88,6 +91,16 @@ func isBoolean(t reflect.Type) bool { } } +// isTextMarshaler returns true if the type or its pointer implements encoding.TextMarshaler +func isTextMarshaler(t reflect.Type) bool { + return t.Implements(textMarshalerType) || reflect.PtrTo(t).Implements(textMarshalerType) +} + +// isTextUnmarshaler returns true if the type or its pointer implements encoding.TextUnmarshaler +func isTextUnmarshaler(t reflect.Type) bool { + return t.Implements(textUnmarshalerType) || reflect.PtrTo(t).Implements(textUnmarshalerType) +} + // isExported returns true if the struct field name is exported func isExported(field string) bool { r, _ := utf8.DecodeRuneInString(field) // returns RuneError for empty string or invalid UTF8 |
