Allow disjoint glossary overrides alongside shared files
This commit is contained in:
@@ -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"}}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -247,3 +247,51 @@ func TestSharedGlossaryReadinessBeforeRequests(t *testing.T) {
|
||||
t.Fatalf("local preflight: status=%d calls=%d stderr=%s", status, calls.Load(), stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedGlossaryWithDisjointNativeOverrides(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
if err := os.WriteFile(main, []byte("version: 3\nglossary_collections:\n people:\n entries: [{id: configured, name: Configured, category: person}]\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct{ name, collections, wantName string }{
|
||||
{"populated", "{people: {entries: [{id: a, name: Alice, category: person}]}}", "Alice"},
|
||||
{"empty", "{}", "Configured"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
file := filepath.Join(dir, tc.name+".yml")
|
||||
if err := os.WriteFile(file, []byte("schema_version: maximumdirect.glossary.v1\ncollections: "+tc.collections+"\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
common := []string{"--scope=app", "--config", main, "--glossary-file", file, "--set", `/glossary_collections/other/entries=[{"id":"b","name":"Bob","category":"person"}]`}
|
||||
var out, stderr bytes.Buffer
|
||||
if code := Run(context.Background(), append([]string{"config", "show"}, common...), &out, &stderr); code != 0 || !strings.Contains(out.String(), "name: "+tc.wantName) || !strings.Contains(out.String(), "name: Bob") {
|
||||
t.Fatalf("show: code=%d out=%s stderr=%s", code, out.String(), stderr.String())
|
||||
}
|
||||
for _, origin := range []struct{ path, flag string }{
|
||||
{"/glossary_collections/other/entries", "--set"},
|
||||
{"/glossary_collections/people/entries", "--glossary-file"},
|
||||
} {
|
||||
if tc.name == "empty" && origin.flag == "--glossary-file" {
|
||||
continue
|
||||
}
|
||||
out.Reset()
|
||||
stderr.Reset()
|
||||
args := append([]string{"config", "explain"}, common...)
|
||||
args = append(args, "--path", origin.path)
|
||||
if code := Run(context.Background(), args, &out, &stderr); code != 0 || !strings.Contains(out.String(), origin.flag) {
|
||||
t.Fatalf("explain: code=%d out=%s stderr=%s", code, out.String(), stderr.String())
|
||||
}
|
||||
}
|
||||
if tc.name == "populated" {
|
||||
out.Reset()
|
||||
stderr.Reset()
|
||||
args := append([]string{"config", "show"}, common...)
|
||||
args = append(args, "--set", `/glossary_collections/people={"entries":[]}`)
|
||||
if code := Run(context.Background(), args, &out, &stderr); code != 2 || !strings.Contains(stderr.String(), "override.overlap") {
|
||||
t.Fatalf("overlap: code=%d stderr=%s", code, stderr.String())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user