summaryrefslogtreecommitdiff
path: root/sequence.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2021-04-19 13:21:04 -0700
committerAlex Flint <[email protected]>2021-04-19 13:21:04 -0700
commit9949860eb3d60d374df3a47ebc0a22ca55bba399 (patch)
tree7249f91dec445c4403c715eb4747754452d808bc /sequence.go
parent23b96d7aacf62828675decc309eae5b9dce5bd51 (diff)
change "kind" to "cardinality", add support for maps to parser
Diffstat (limited to 'sequence.go')
-rw-r--r--sequence.go29
1 files changed, 22 insertions, 7 deletions
diff --git a/sequence.go b/sequence.go
index 8971341..35a3614 100644
--- a/sequence.go
+++ b/sequence.go
@@ -8,13 +8,32 @@ import (
scalar "github.com/alexflint/go-scalar"
)
-// setSlice parses a sequence of strings and inserts them into a slice. If clear
-// is true then any values already in the slice are removed.
-func setSlice(dest reflect.Value, values []string, clear bool) error {
+// setSliceOrMap parses a sequence of strings into a slice or map. If clear is
+// true then any values already in the slice or map are first removed.
+func setSliceOrMap(dest reflect.Value, values []string, clear bool) error {
if !dest.CanSet() {
return fmt.Errorf("field is not writable")
}
+ t := dest.Type()
+ if t.Kind() == reflect.Ptr {
+ dest = dest.Elem()
+ t = t.Elem()
+ }
+
+ switch t.Kind() {
+ case reflect.Slice:
+ return setSlice(dest, values, clear)
+ case reflect.Map:
+ return setMap(dest, values, clear)
+ default:
+ return fmt.Errorf("setSliceOrMap cannot insert values into a %v", t)
+ }
+}
+
+// setSlice parses a sequence of strings and inserts them into a slice. If clear
+// is true then any values already in the slice are removed.
+func setSlice(dest reflect.Value, values []string, clear bool) error {
var ptr bool
elem := dest.Type().Elem()
if elem.Kind() == reflect.Ptr && !elem.Implements(textUnmarshalerType) {
@@ -44,10 +63,6 @@ func setSlice(dest reflect.Value, values []string, clear bool) error {
// setMap parses a sequence of name=value strings and inserts them into a map.
// If clear is true then any values already in the map are removed.
func setMap(dest reflect.Value, values []string, clear bool) error {
- if !dest.CanSet() {
- return fmt.Errorf("field is not writable")
- }
-
// determine the key and value type
var keyIsPtr bool
keyType := dest.Type().Key()