From 50cf1e1a883f91b1eaed85b7bd694b024761f113 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 6 Apr 2021 00:30:32 +0200 Subject: chore(users/Profpatsch/struct-edit): factor out non-val enumeration 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 --- users/Profpatsch/struct-edit/main.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'users/Profpatsch/struct-edit') 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 } -- cgit 1.4.1