Compose shared glossary files with ConfigKit

This commit is contained in:
2026-11-29 18:10:21 +00:00
parent 3e287503eb
commit 009373130c
4 changed files with 175 additions and 8 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"path/filepath"
"gitea.maximumdirect.net/eric/audita/v4/internal/appconfig/sharedglossary"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/responseschema"
"gitea.maximumdirect.net/eric/audita/v4/internal/prompts"
@@ -112,14 +113,7 @@ func Load(ctx context.Context, input ck.Input, options Options) (*Acquisition, *
}
inputs[i] = ck.Input{Source: ck.File(absolute), Imports: true}
}
underlays := make([]ck.Underlay, 0, len(inputs)-1)
for _, item := range inputs[:len(inputs)-1] {
underlays = append(underlays, ck.Underlay{Inputs: []ck.Input{item}})
}
glossaries, err := ck.LoadLayered(inputs[len(inputs)-1], ck.LayeredOptions[map[string]any]{
Definition: ck.Definition[map[string]any]{SequenceIdentities: []ck.SequenceIdentity{config.GlossaryEntryIdentity(nil)}},
Underlays: underlays,
})
glossaries, err := sharedglossary.Load(ctx, inputs)
if canceled := ctx.Err(); canceled != nil {
return nil, report, errors.Join(err, canceled)
}

View File

@@ -0,0 +1,52 @@
package sharedglossary
import (
"context"
"errors"
"strings"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
ck "go.maximumdirect.net/configkit"
)
// Load composes ordered shared documents and validates the completed envelope.
// A nil result means no documents were supplied.
func Load(ctx context.Context, inputs []ck.Input) (*ck.Result[map[string]any], error) {
if err := ctx.Err(); err != nil {
return nil, err
}
if len(inputs) == 0 {
return nil, nil
}
validator, err := NewValidator()
if err != nil {
return nil, err
}
underlays := make([]ck.Underlay, 0, len(inputs)-1)
for _, input := range inputs[:len(inputs)-1] {
input.Imports = true
underlays = append(underlays, ck.Underlay{Inputs: []ck.Input{input}})
}
main := inputs[len(inputs)-1]
main.Imports = true
result, err := ck.LoadLayered(main, ck.LayeredOptions[map[string]any]{
Definition: ck.Definition[map[string]any]{SequenceIdentities: []ck.SequenceIdentity{config.GlossaryEntryIdentity(ck.SequenceSelector{ck.SequenceLiteralKey("collections")})}},
Underlays: underlays,
})
if canceled := ctx.Err(); canceled != nil {
return nil, errors.Join(err, canceled)
}
if err != nil {
return nil, err
}
var sources []string
for _, source := range result.Sources() {
if source.Path != "" {
sources = append(sources, source.Path)
}
}
if err := validator.Validate(result.Config, strings.Join(sources, ", ")); err != nil {
return nil, err
}
return result, nil
}

View File

@@ -0,0 +1,119 @@
package sharedglossary
import (
"context"
"errors"
"os"
"path/filepath"
"slices"
"strings"
"testing"
ck "go.maximumdirect.net/configkit"
)
func TestLoadComposition(t *testing.T) {
dir := t.TempDir()
files := map[string]string{
"first.yml": `config:
imports: [{path: fragment.yml, required: true}]
schema_version: maximumdirect.glossary.v1
collections:
people:
entries:
- {id: one, name: Old, category: person}
- {id: two, name: Two, category: person}
places:
entries: []
`,
"fragment.yml": `collections:
lore:
entries: [{id: lore, name: Lore, category: topic}]
`,
"second.yml": `schema_version: maximumdirect.glossary.v1
collections:
people:
entries: [{id: one, name: New, category: person}]
`,
"third.json": `{"schema_version":"maximumdirect.glossary.v1","collections":{"spells":{"entries":[{"id":"one","name":"Spell","category":"spell"}]}}}`,
}
for name, body := range files {
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0644); err != nil {
t.Fatal(err)
}
}
for _, tc := range []struct {
name string
files []string
collections []string
}{
{"one multi collection file", []string{"first.yml"}, []string{"people", "places", "lore"}},
{"multiple single collection files", []string{"second.yml", "third.json"}, []string{"people", "spells"}},
{"mixed files", []string{"first.yml", "second.yml", "third.json"}, []string{"people", "places", "lore", "spells"}},
} {
t.Run(tc.name, func(t *testing.T) {
var inputs []ck.Input
for _, name := range tc.files {
inputs = append(inputs, ck.Input{Source: ck.File(filepath.Join(dir, name))})
}
result, err := Load(context.Background(), inputs)
if err != nil {
t.Fatal(err)
}
collections := result.Config["collections"].(map[string]any)
for _, name := range tc.collections {
if _, ok := collections[name]; !ok {
t.Errorf("missing collection %s in %v", name, collections)
}
}
if slices.Contains(tc.files, "first.yml") {
var paths []string
for _, source := range result.Sources() {
paths = append(paths, source.Path)
}
if !slices.Contains(paths, filepath.Join(dir, "fragment.yml")) {
t.Errorf("missing imported source: %v", paths)
}
}
if slices.Contains(tc.files, "first.yml") && slices.Contains(tc.files, "second.yml") {
entries := collections["people"].(map[string]any)["entries"].([]any)
if len(entries) != 2 || entries[0].(map[string]any)["name"] != "New" {
t.Fatalf("layered entries = %#v", entries)
}
}
})
}
}
func TestLoadErrorsAndEmptyInput(t *testing.T) {
result, err := Load(context.Background(), nil)
if err != nil || result != nil {
t.Fatalf("no inputs = %v, %v", result, err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := Load(ctx, nil); !errors.Is(err, context.Canceled) {
t.Fatalf("cancellation = %v", err)
}
dir := t.TempDir()
for _, tc := range []struct{ name, body, contains string }{
{"duplicates", "schema_version: maximumdirect.glossary.v1\ncollections:\n people:\n entries: [{id: a, name: A, category: person}, {id: a, name: B, category: person}]\n", "a"},
{"missing marker", "collections: {}\n", "schema_version"},
{"invalid type", "schema_version: maximumdirect.glossary.v1\ncollections: []\n", "collections"},
{"unsupported input", "schema_version: !!str maximumdirect.glossary.v1\ncollections: {}\n", ""},
} {
t.Run(tc.name, func(t *testing.T) {
path := filepath.Join(dir, strings.ReplaceAll(tc.name, " ", "_")+".yml")
if err := os.WriteFile(path, []byte(tc.body), 0644); err != nil {
t.Fatal(err)
}
_, err := Load(context.Background(), []ck.Input{{Source: ck.File(path)}})
if err == nil || !strings.Contains(err.Error(), tc.contains) {
t.Fatalf("Load() = %v", err)
}
if tc.name == "missing marker" && !strings.Contains(err.Error(), path) {
t.Fatalf("missing source context: %v", err)
}
})
}
}