refactor(eda): use slices.Sorted over maps.Keys for ordered keys

This commit is contained in:
sam committed 2026-10-10 11:05:58 +11:00
1 parent 84b581fde6
commit b070ce4715
3 files changed
+12 -28

No files matched your search

+3 -5
View File
@@ -3,7 +3,9 @@
package expr
import (
"maps"
"reflect"
"slices"
"sort"
"strings"
@@ -252,11 +254,7 @@ func actionReferences(ast *cel.Ast) ([]string, bool) {
}
})
celast.PreOrderVisit(checked.Expr(), visitor)
ids := make([]string, 0, len(seen))
for id := range seen {
ids = append(ids, id)
}
sort.Strings(ids)
ids := slices.Sorted(maps.Keys(seen))
return ids, identCount == staticCount
}
+3 -6
View File
@@ -8,8 +8,9 @@ import (
"context"
"fmt"
"log/slog"
"maps"
"reflect"
"sort"
"slices"
"strings"
"github.com/samcharles93/archie-core/internal/domain/eda/module/log"
@@ -164,11 +165,7 @@ func decodeResultStruct(kind string, t reflect.Type, raw map[string]any) (any, e
byName[name] = out.Field(i)
}
keys := make([]string, 0, len(raw))
for key := range raw {
keys = append(keys, key)
}
sort.Strings(keys)
keys := slices.Sorted(maps.Keys(raw))
for _, key := range keys {
if _, ok := byName[key]; !ok {
return nil, fmt.Errorf("module %s: unknown result field %q", kind, key)
+6 -17
View File
@@ -6,10 +6,11 @@ import (
"context"
"crypto/sha256"
"fmt"
"maps"
"os"
"path/filepath"
"reflect"
"sort"
"slices"
"strings"
"gopkg.in/yaml.v3"
@@ -235,7 +236,7 @@ func Load(dir string, modules Modules) (*Store, error) {
// Compile validates and compiles playbook documents in id order. Any invalid
// playbook fails the whole set: a partially valid set never runs.
func Compile(docs []Document, modules Modules) (*Store, error) {
sort.Slice(docs, func(i, j int) bool { return docs[i].ID < docs[j].ID })
slices.SortFunc(docs, func(a, b Document) int { return strings.Compare(a.ID, b.ID) })
store := &Store{modules: modules}
for _, doc := range docs {
pb, err := compileOne(doc, modules)
@@ -481,11 +482,7 @@ func compileArgs(path string, lines map[string]int, label, kind string, raw map[
return nil, err
}
}
keys := make([]string, 0, len(raw))
for key := range raw {
keys = append(keys, key)
}
sort.Strings(keys)
keys := slices.Sorted(maps.Keys(raw))
args := make(map[string]*expr.Program, len(raw))
for _, key := range keys {
prg, err := compileExpr(at(path, lines[key]), argsLabel(label, key), raw[key], env)
@@ -512,11 +509,7 @@ func validateArgsKeys(path string, lines map[string]int, label, kind string, arg
for field := range t.Fields() {
fields[strings.ToLower(field.Name)] = struct{}{}
}
keys := make([]string, 0, len(raw))
for key := range raw {
keys = append(keys, key)
}
sort.Strings(keys)
keys := slices.Sorted(maps.Keys(raw))
loc := kind + " kind"
if label != "" {
loc = label + " " + loc
@@ -671,11 +664,7 @@ func (a Action) whenHolds(ctx expr.Context) bool {
// the first evaluation error.
func (a Action) evalArgs(ctx expr.Context) (map[string]any, error) {
out := make(map[string]any, len(a.Args))
keys := make([]string, 0, len(a.Args))
for key := range a.Args {
keys = append(keys, key)
}
sort.Strings(keys)
keys := slices.Sorted(maps.Keys(a.Args))
for _, key := range keys {
if a.Args[key] == nil {
continue