From 85d8fba3bdca20c06ebe95c06690c0170a6c8114 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 4 Jan 2017 16:03:24 -0500 Subject: [PATCH] Minor fixups to expandArray Find the index keys by comparing the strings directly, so we don't need to worry about the prefix value altering the regex. --- flatmap/expand.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/flatmap/expand.go b/flatmap/expand.go index 0e3ebe003..dfaf74e7b 100644 --- a/flatmap/expand.go +++ b/flatmap/expand.go @@ -2,7 +2,6 @@ package flatmap import ( "fmt" - "regexp" "sort" "strconv" "strings" @@ -44,16 +43,28 @@ func expandArray(m map[string]string, prefix string) []interface{} { panic(err) } - keySet := make(map[int]struct{}) - listElementKey := regexp.MustCompile("^" + prefix + "\\.([0-9]+)(?:\\..*)?$") - for key := range m { - if matches := listElementKey.FindStringSubmatch(key); matches != nil { - k, err := strconv.ParseInt(matches[1], 0, 0) - if err != nil { - panic(err) - } - keySet[int(k)] = struct{}{} + keySet := map[int]bool{} + for k := range m { + if !strings.HasPrefix(k, prefix+".") { + continue } + + key := k[len(prefix)+1:] + idx := strings.Index(key, ".") + if idx != -1 { + key = key[:idx] + } + + // skip the count value + if key == "#" { + continue + } + + k, err := strconv.Atoi(key) + if err != nil { + panic(err) + } + keySet[int(k)] = true } keysList := make([]int, 0, num)