summaryrefslogtreecommitdiff
path: root/sequence.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2021-04-19 12:10:53 -0700
committerAlex Flint <[email protected]>2021-04-19 12:10:53 -0700
commit1dfefdc43e8a9a06b532b5c29f876eb38f86a928 (patch)
tree9d1be86c6864268c976ecc360fcc2015bbbbe424 /sequence.go
parentf4eb7f3a585abd65b0568428b2b9fde8cebffb6a (diff)
factor setSlice into its own file, add setMap, and add tests for both
Diffstat (limited to 'sequence.go')
-rw-r--r--sequence.go108
1 files changed, 108 insertions, 0 deletions
diff --git a/sequence.go b/sequence.go
new file mode 100644
index 0000000..8971341
--- /dev/null
+++ b/sequence.go
@@ -0,0 +1,108 @@
+package arg
+
+import (
+ "fmt"
+ "reflect"
+ "strings"
+
+ 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 {
+ if !dest.CanSet() {
+ return fmt.Errorf("field is not writable")
+ }
+
+ var ptr bool
+ elem := dest.Type().Elem()
+ if elem.Kind() == reflect.Ptr && !elem.Implements(textUnmarshalerType) {
+ ptr = true
+ elem = elem.Elem()
+ }
+
+ // clear the slice in case default values exist
+ if clear && !dest.IsNil() {
+ dest.SetLen(0)
+ }
+
+ // parse the values one-by-one
+ for _, s := range values {
+ v := reflect.New(elem)
+ if err := scalar.ParseValue(v.Elem(), s); err != nil {
+ return err
+ }
+ if !ptr {
+ v = v.Elem()
+ }
+ dest.Set(reflect.Append(dest, v))
+ }
+ return nil
+}
+
+// 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()
+ if keyType.Kind() == reflect.Ptr && !keyType.Implements(textUnmarshalerType) {
+ keyIsPtr = true
+ keyType = keyType.Elem()
+ }
+
+ var valIsPtr bool
+ valType := dest.Type().Elem()
+ if valType.Kind() == reflect.Ptr && !valType.Implements(textUnmarshalerType) {
+ valIsPtr = true
+ valType = valType.Elem()
+ }
+
+ // clear the slice in case default values exist
+ if clear && !dest.IsNil() {
+ for _, k := range dest.MapKeys() {
+ dest.SetMapIndex(k, reflect.Value{})
+ }
+ }
+
+ // allocate the map if it is not allocated
+ if dest.IsNil() {
+ dest.Set(reflect.MakeMap(dest.Type()))
+ }
+
+ // parse the values one-by-one
+ for _, s := range values {
+ // split at the first equals sign
+ pos := strings.Index(s, "=")
+ if pos == -1 {
+ return fmt.Errorf("cannot parse %q into a map, expected format key=value", s)
+ }
+
+ // parse the key
+ k := reflect.New(keyType)
+ if err := scalar.ParseValue(k.Elem(), s[:pos]); err != nil {
+ return err
+ }
+ if !keyIsPtr {
+ k = k.Elem()
+ }
+
+ // parse the value
+ v := reflect.New(valType)
+ if err := scalar.ParseValue(v.Elem(), s[pos+1:]); err != nil {
+ return err
+ }
+ if !valIsPtr {
+ v = v.Elem()
+ }
+
+ // add it to the map
+ dest.SetMapIndex(k, v)
+ }
+ return nil
+}