Resolve and project shared glossary collections
This commit is contained in:
107
internal/glossary/resolve.go
Normal file
107
internal/glossary/resolve.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package glossary
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Sources identifies the selected, already source-resolved vocabulary files.
|
||||
type Sources struct {
|
||||
CampaignFile string
|
||||
CampaignInline map[string]any
|
||||
SettingFile string
|
||||
SpellsFile string
|
||||
}
|
||||
|
||||
// Character contains the roster fields needed for a party vocabulary entry.
|
||||
type Character struct {
|
||||
ID string
|
||||
Name string
|
||||
Aliases []string
|
||||
}
|
||||
|
||||
// Documents holds the four completed standalone named collections.
|
||||
type Documents map[string][]byte
|
||||
|
||||
// Resolve builds independent, validated vocabulary snapshots using native composition.
|
||||
func Resolve(sources Sources, characters []Character) (Documents, error) {
|
||||
party := make([]any, 0, len(characters))
|
||||
ordered := append([]Character(nil), characters...)
|
||||
sort.Slice(ordered, func(i, j int) bool { return ordered[i].ID < ordered[j].ID })
|
||||
for _, c := range ordered {
|
||||
entry := map[string]any{"id": c.ID, "name": c.Name, "category": "person"}
|
||||
if len(c.Aliases) > 0 {
|
||||
entry["aliases"] = c.Aliases
|
||||
}
|
||||
party = append(party, entry)
|
||||
}
|
||||
collections := map[string]any{"party": map[string]any{"entries": party}}
|
||||
for _, item := range []struct {
|
||||
name, path string
|
||||
inline map[string]any
|
||||
}{
|
||||
{"campaign", sources.CampaignFile, sources.CampaignInline},
|
||||
{"setting", sources.SettingFile, nil},
|
||||
{"spells", sources.SpellsFile, nil},
|
||||
} {
|
||||
value, err := resolveCollection(item.name, item.path, item.inline)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
collections[item.name] = value
|
||||
}
|
||||
result := make(Documents, len(collections))
|
||||
for _, name := range []string{"party", "campaign", "setting", "spells"} {
|
||||
doc := map[string]any{name: collections[name]}
|
||||
if err := ValidateDocument(doc); err != nil {
|
||||
return nil, fmt.Errorf("%s vocabulary: %w", name, err)
|
||||
}
|
||||
data, err := yaml.Marshal(doc)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("serialize %s vocabulary: %w", name, err)
|
||||
}
|
||||
result[name] = data
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func resolveCollection(name, path string, inline map[string]any) (any, error) {
|
||||
if path == "" && inline == nil {
|
||||
return map[string]any{"entries": []any{}}, nil
|
||||
}
|
||||
definition := configkit.Definition[map[string]any]{SequenceIdentities: []configkit.SequenceIdentity{
|
||||
configkit.SequenceIdentityMatching(configkit.SequenceSelector{configkit.SequenceAnyMappingKey(), configkit.SequenceLiteralKey("entries")}, "id"),
|
||||
}}
|
||||
var underlays []configkit.Underlay
|
||||
if path == "" && inline != nil {
|
||||
empty := configkit.Mapping("empty vocabulary", map[string]any{name: map[string]any{"entries": []any{}}})
|
||||
underlays = append(underlays, configkit.Underlay{Inputs: []configkit.Input{{Source: empty}}})
|
||||
}
|
||||
if path != "" {
|
||||
file := configkit.Input{Source: configkit.File(path), Imports: true}
|
||||
loaded, err := configkit.LoadLayered(file, configkit.LayeredOptions[map[string]any]{Definition: definition})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load %s vocabulary %q: %w", name, path, err)
|
||||
}
|
||||
if _, ok := loaded.Config[name]; !ok {
|
||||
return nil, fmt.Errorf("%s vocabulary %q does not define collection %q", name, path, name)
|
||||
}
|
||||
if inline == nil {
|
||||
return loaded.Config[name], nil
|
||||
}
|
||||
underlays = []configkit.Underlay{{Inputs: []configkit.Input{file}}}
|
||||
}
|
||||
main := configkit.Input{Source: configkit.Mapping("inline vocabulary", inline), Destination: configkit.Keys(name)}
|
||||
loaded, err := configkit.LoadLayered(main, configkit.LayeredOptions[map[string]any]{Definition: definition, Underlays: underlays})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("compose %s vocabulary: %w", name, err)
|
||||
}
|
||||
value, ok := loaded.Config[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s vocabulary is missing", name)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
83
internal/glossary/resolve_test.go
Normal file
83
internal/glossary/resolve_test.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package glossary
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestResolveComposesAndProjectsVocabulary(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "campaign.yml")
|
||||
data := `campaign:
|
||||
instruction: Use established names.
|
||||
entries:
|
||||
- {id: watch, name: Old Watch, category: organization, aliases: [Old]}
|
||||
- {id: harbor, name: North Harbor, category: place, source_note: synthetic}
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
inline := map[string]any{"entries": []any{map[string]any{"id": "watch", "name": "Silver Watch", "category": "organization"}}}
|
||||
docs, err := Resolve(Sources{CampaignFile: path, CampaignInline: inline}, []Character{{ID: "z", Name: "Zed"}, {ID: "a", Name: "Ada", Aliases: []string{"A"}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var campaign map[string]map[string]any
|
||||
if err := yaml.Unmarshal(docs["campaign"], &campaign); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries := campaign["campaign"]["entries"].([]any)
|
||||
first := entries[0].(map[string]any)
|
||||
if first["name"] != "Silver Watch" || first["aliases"] != nil || !strings.Contains(string(docs["campaign"]), "source_note: synthetic") {
|
||||
t.Fatalf("campaign: %s", docs["campaign"])
|
||||
}
|
||||
if !strings.Contains(string(docs["campaign"]), "instruction: Use established names.") {
|
||||
t.Fatalf("instruction: %s", docs["campaign"])
|
||||
}
|
||||
if strings.Index(string(docs["party"]), "id: a") > strings.Index(string(docs["party"]), "id: z") {
|
||||
t.Fatalf("party order: %s", docs["party"])
|
||||
}
|
||||
for _, name := range []string{"setting", "spells"} {
|
||||
if !strings.Contains(string(docs[name]), "entries: []") {
|
||||
t.Fatalf("empty %s: %s", name, docs[name])
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("config:\n sequences:\n - destination: [campaign, entries]\n mode: replace\n"+data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
docs, err = Resolve(Sources{CampaignFile: path, CampaignInline: map[string]any{"entries": []any{}}}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(docs["campaign"]), "entries: []") || !strings.Contains(string(docs["campaign"]), "instruction:") {
|
||||
t.Fatalf("replacement: %s", docs["campaign"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveHandlesInheritancePartialAndFailures(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "setting.yml")
|
||||
if err := os.WriteFile(path, []byte("common:\n entries: [{id: tower, name: Tower, category: place}]\nsetting:\n extends: common\n instruction: ''\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
docs, err := Resolve(Sources{SettingFile: path, CampaignInline: map[string]any{"instruction": "Only instruction"}}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(docs["setting"]), "Tower") || !strings.Contains(string(docs["setting"]), "instruction: \"\"") {
|
||||
t.Fatalf("inherited: %s", docs["setting"])
|
||||
}
|
||||
if !strings.Contains(string(docs["campaign"]), "entries: []") {
|
||||
t.Fatalf("partial: %s", docs["campaign"])
|
||||
}
|
||||
if _, err := Resolve(Sources{SpellsFile: path}, nil); err == nil || !strings.Contains(err.Error(), "spells") {
|
||||
t.Fatalf("missing selection: %v", err)
|
||||
}
|
||||
if _, err := Resolve(Sources{CampaignFile: filepath.Join(dir, "missing.yml")}, nil); err == nil {
|
||||
t.Fatal("missing file accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user