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.
This commit is contained in:
James Bardin 2017-01-04 16:03:24 -05:00
parent 497010ce42
commit 85d8fba3bd
1 changed files with 21 additions and 10 deletions

View File

@ -2,7 +2,6 @@ package flatmap
import ( import (
"fmt" "fmt"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -44,16 +43,28 @@ func expandArray(m map[string]string, prefix string) []interface{} {
panic(err) panic(err)
} }
keySet := make(map[int]struct{}) keySet := map[int]bool{}
listElementKey := regexp.MustCompile("^" + prefix + "\\.([0-9]+)(?:\\..*)?$") for k := range m {
for key := range m { if !strings.HasPrefix(k, prefix+".") {
if matches := listElementKey.FindStringSubmatch(key); matches != nil { continue
k, err := strconv.ParseInt(matches[1], 0, 0)
if err != nil {
panic(err)
}
keySet[int(k)] = struct{}{}
} }
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) keysList := make([]int, 0, num)