sort map keys when generating asserts (#2494)

* sort map keys when generating asserts
This commit is contained in:
Thibault "bui" Koechlin 2023-10-16 09:54:19 +02:00 committed by GitHub
parent b2a6eb92bf
commit 3cd4847093
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View file

@ -232,30 +232,34 @@ func (p *ParserAssert) AutoGenParserAssert() string {
if !result.Success {
continue
}
for pkey, pval := range result.Evt.Parsed {
for _, pkey := range sortedMapKeys(result.Evt.Parsed) {
pval := result.Evt.Parsed[pkey]
if pval == "" {
continue
}
ret += fmt.Sprintf(`results["%s"]["%s"][%d].Evt.Parsed["%s"] == "%s"`+"\n", stage, parser, pidx, pkey, Escape(pval))
}
for mkey, mval := range result.Evt.Meta {
for _, mkey := range sortedMapKeys(result.Evt.Meta) {
mval := result.Evt.Meta[mkey]
if mval == "" {
continue
}
ret += fmt.Sprintf(`results["%s"]["%s"][%d].Evt.Meta["%s"] == "%s"`+"\n", stage, parser, pidx, mkey, Escape(mval))
}
for ekey, eval := range result.Evt.Enriched {
for _, ekey := range sortedMapKeys(result.Evt.Enriched) {
eval := result.Evt.Enriched[ekey]
if eval == "" {
continue
}
ret += fmt.Sprintf(`results["%s"]["%s"][%d].Evt.Enriched["%s"] == "%s"`+"\n", stage, parser, pidx, ekey, Escape(eval))
}
for ekey, eval := range result.Evt.Unmarshaled {
if eval == "" {
for _, ukey := range sortedMapKeys(result.Evt.Unmarshaled) {
uval := result.Evt.Unmarshaled[ukey]
if uval == "" {
continue
}
base := fmt.Sprintf(`results["%s"]["%s"][%d].Evt.Unmarshaled["%s"]`, stage, parser, pidx, ekey)
for _, line := range p.buildUnmarshaledAssert(base, eval) {
base := fmt.Sprintf(`results["%s"]["%s"][%d].Evt.Unmarshaled["%s"]`, stage, parser, pidx, ukey)
for _, line := range p.buildUnmarshaledAssert(base, uval) {
ret += line
}
}

View file

@ -4,8 +4,18 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
)
func sortedMapKeys[V any](m map[string]V) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func Copy(src string, dst string) error {
content, err := os.ReadFile(src)
if err != nil {