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 <mail@profpatsch.de>
This commit is contained in:
Profpatsch 2021-04-06 00:30:32 +02:00
parent 77840fba2c
commit 50cf1e1a88

View file

@ -152,20 +152,32 @@ type enumerate struct {
// for scalars its 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 lets 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
}