about summary refs log tree commit diff
path: root/users/Profpatsch
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2021-04-05T22·30+0200
committerProfpatsch <mail@profpatsch.de>2021-04-23T18·30+0000
commit50cf1e1a883f91b1eaed85b7bd694b024761f113 (patch)
tree9c20ea77b0c051171fbd094c6a97b8a68fa353b7 /users/Profpatsch
parent77840fba2c1e4e900e27df0163f39ccc7a0c48a6 (diff)
chore(users/Profpatsch/struct-edit): factor out non-val enumeration r/2541
This will be needed to factor the current cursor position into vals.

Change-Id: I73635b13c29b6b8925c68005c8db1c4dda93f15d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2865
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch')
-rw-r--r--users/Profpatsch/struct-edit/main.go22
1 files changed, 17 insertions, 5 deletions
diff --git a/users/Profpatsch/struct-edit/main.go b/users/Profpatsch/struct-edit/main.go
index f40c6df66db8..76e346ed04ed 100644
--- a/users/Profpatsch/struct-edit/main.go
+++ b/users/Profpatsch/struct-edit/main.go
@@ -152,20 +152,32 @@ type enumerate struct {
 // for scalars it’s just a nil index & the val itself.
 // Guaranteed to always return at least one element.
 func (v val) enumerate() (e []enumerate) {
-	switch v.tag {
+	e = enumerateInner(v.tag, v.val)
+	if e == nil {
+		e = append(e, enumerate{
+			i: nil,
+			v: v,
+		})
+	}
+	return
+}
+
+// like enumerate, but returns an empty slice for scalars without inner vals.
+func enumerateInner(tag tag, v interface{}) (e []enumerate) {
+	switch tag {
 	case tagString:
 		fallthrough
 	case tagFloat:
-		e = []enumerate{enumerate{i: index(nil), v: v}}
+		e = nil
 	case tagList:
-		for i, v := range v.val.([]val) {
+		for i, v := range v.([]val) {
 			e = append(e, enumerate{i: index(uint(i)), v: v})
 		}
 	case tagMap:
 		// map sorting order is not stable (actually randomized thank jabber)
 		// so let’s sort them
 		keys := []string{}
-		m := v.val.(map[string]val)
+		m := v.(map[string]val)
 		for k, _ := range m {
 			keys = append(keys, k)
 		}
@@ -174,7 +186,7 @@ func (v val) enumerate() (e []enumerate) {
 			e = append(e, enumerate{i: index(k), v: m[k]})
 		}
 	default:
-		log.Fatalf("unknown val tag %s, %v", v.tag, v)
+		log.Fatalf("unknown val tag %s, %v", tag, v)
 	}
 	return
 }