Allow disjoint glossary overrides alongside shared files

This commit is contained in:
2026-11-29 18:43:13 +00:00
parent e72f69c68b
commit fb612b361d
6 changed files with 127 additions and 9 deletions

View File

@@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"path/filepath"
"sort"
"strings"
"gitea.maximumdirect.net/eric/audita/v4/internal/appconfig/sharedglossary"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
@@ -119,7 +121,7 @@ func Load(ctx context.Context, input ck.Input, options Options) (*Acquisition, *
collection := value.(map[string]any)
projected[name] = map[string]any{"entries": collection["entries"]}
}
overrides = append(overrides, ck.Override{Path: ck.Keys("glossary_collections"), Value: projected, Flag: "--glossary-file"})
overrides = append(overrides, glossaryOverrides(projected, options.Overrides)...)
}
embedded, err := pk.FilesystemInputs("Audita embedded prompts", prompts.Filesystem(), prompts.AssetRoot)
if err != nil {
@@ -155,3 +157,30 @@ func Load(ctx context.Context, input ck.Input, options Options) (*Acquisition, *
}
return &Acquisition{Result: result, GlossarySources: glossarySources}, report, nil
}
func glossaryOverrides(projected map[string]any, supplied []ck.Override) []ck.Override {
if len(projected) == 0 {
return nil
}
root := ck.Keys("glossary_collections")
for _, override := range supplied {
path := override.Path.String()
if path != root.String() && !strings.HasPrefix(path, root.String()+"/") {
continue
}
// Only split when caller overrides need to share this namespace. The
// usual batch keeps one native record regardless of collection count.
names := make([]string, 0, len(projected))
for name := range projected {
names = append(names, name)
}
sort.Strings(names)
result := make([]ck.Override, 0, len(names))
for _, name := range names {
entries := projected[name].(map[string]any)["entries"]
result = append(result, ck.Override{Path: root.Key(name).Key("entries"), Value: entries, Flag: "--glossary-file"})
}
return result
}
return []ck.Override{{Path: root, Value: projected, Flag: "--glossary-file"}}
}

View File

@@ -2,7 +2,9 @@ package appconfig
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"slices"
@@ -109,6 +111,31 @@ func TestRuntimeGlossaryLoadErrors(t *testing.T) {
}
}
func TestRuntimeGlossaryLargeCollectionBatch(t *testing.T) {
collections := make(map[string]any)
for i := range 1100 {
collections[fmt.Sprintf("collection-%d", i)] = map[string]any{"entries": []any{}}
}
data, err := json.Marshal(map[string]any{"schema_version": "maximumdirect.glossary.v1", "collections": collections})
if err != nil {
t.Fatal(err)
}
file := filepath.Join(t.TempDir(), "shared.json")
if err := os.WriteFile(file, data, 0600); err != nil {
t.Fatal(err)
}
got, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, Options{
GlossaryFiles: []string{file},
Overrides: []ck.Override{{Path: ck.Keys("context", "description"), Value: "batch", Flag: "--set"}},
})
if err != nil {
t.Fatal(err)
}
if len(got.Config.Application.GlossaryCollections) != len(collections) {
t.Fatalf("loaded %d collections, want %d", len(got.Config.Application.GlossaryCollections), len(collections))
}
}
func TestRuntimeGlossaryPresence(t *testing.T) {
for _, tc := range []struct {
name, body string