Prepare shared glossary envelopes and combined input
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/domaininput"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -19,97 +20,113 @@ type Sources struct {
|
||||
|
||||
// Character contains the roster fields needed for a party vocabulary entry.
|
||||
type Character struct {
|
||||
ID string
|
||||
Name string
|
||||
Aliases []string
|
||||
ID, Name string
|
||||
Aliases []string
|
||||
}
|
||||
|
||||
// Documents holds the four completed standalone named collections.
|
||||
// Documents holds individual and combined standalone shared envelopes.
|
||||
type Documents map[string][]byte
|
||||
|
||||
// Resolve builds independent, validated vocabulary snapshots using native composition.
|
||||
const glossaryMarker = "maximumdirect.glossary.v1"
|
||||
|
||||
func envelope(collections map[string]any) map[string]any {
|
||||
return map[string]any{"schema_version": glossaryMarker, "collections": collections}
|
||||
}
|
||||
|
||||
// Resolve builds and validates individual collections and their combined projection.
|
||||
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 })
|
||||
entries := make([]any, 0, len(ordered))
|
||||
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
|
||||
aliases := make([]any, len(c.Aliases))
|
||||
for i, alias := range c.Aliases {
|
||||
aliases[i] = alias
|
||||
}
|
||||
entry["aliases"] = aliases
|
||||
}
|
||||
party = append(party, entry)
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
collections := map[string]any{"party": map[string]any{"entries": party}}
|
||||
selected := map[string]map[string]any{"party": envelope(map[string]any{"party": map[string]any{"entries": entries}})}
|
||||
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)
|
||||
}{{"campaign", sources.CampaignFile, sources.CampaignInline}, {"setting", sources.SettingFile, nil}, {"spells", sources.SpellsFile, nil}} {
|
||||
doc, err := resolveCollection(item.name, item.path, item.inline)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
collections[item.name] = value
|
||||
selected[item.name] = doc
|
||||
}
|
||||
result := make(Documents, len(collections))
|
||||
combined := make(map[string]any, len(selected))
|
||||
result := make(Documents, 5)
|
||||
for _, name := range []string{"party", "campaign", "setting", "spells"} {
|
||||
doc := map[string]any{name: collections[name]}
|
||||
if err := ValidateDocument(doc); err != nil {
|
||||
doc := selected[name]
|
||||
if err := domaininput.ValidateGlossary(doc); err != nil {
|
||||
return nil, fmt.Errorf("%s vocabulary: %w", name, err)
|
||||
}
|
||||
combined[name] = doc["collections"].(map[string]any)[name]
|
||||
data, err := yaml.Marshal(doc)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("serialize %s vocabulary: %w", name, err)
|
||||
}
|
||||
result[name] = data
|
||||
}
|
||||
doc := envelope(combined)
|
||||
if err := domaininput.ValidateGlossary(doc); err != nil {
|
||||
return nil, fmt.Errorf("combined vocabulary: %w", err)
|
||||
}
|
||||
data, err := yaml.Marshal(doc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result["combined"] = 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 != "" {
|
||||
func resolveCollection(name, path string, inline map[string]any) (map[string]any, error) {
|
||||
selector := configkit.SequenceSelector{configkit.SequenceLiteralKey("collections"), configkit.SequenceAnyMappingKey(), configkit.SequenceLiteralKey("entries")}
|
||||
definition := configkit.Definition[map[string]any]{SequenceIdentities: []configkit.SequenceIdentity{configkit.SequenceIdentityMatching(selector, "id")}}
|
||||
var base configkit.Input
|
||||
if path == "" {
|
||||
base = configkit.Input{Source: configkit.Mapping("empty vocabulary", envelope(map[string]any{name: map[string]any{"entries": []any{}}}))}
|
||||
} else {
|
||||
absolute, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resolve %s vocabulary path %q: %w", name, path, err)
|
||||
return nil, fmt.Errorf("resolve %s vocabulary: %w", name, err)
|
||||
}
|
||||
// Vocabulary files may import sibling fragments relative to their own
|
||||
// locations; let ConfigKit resolve those imports in the host filesystem.
|
||||
root := filepath.VolumeName(absolute) + string(filepath.Separator)
|
||||
file := configkit.Input{Source: configkit.File(absolute), Imports: true, Filesystem: &configkit.FilesystemContext{Root: root}}
|
||||
loaded, err := configkit.LoadLayered(file, configkit.LayeredOptions[map[string]any]{Definition: definition})
|
||||
base = configkit.Input{Source: configkit.File(absolute), Imports: true, Filesystem: &configkit.FilesystemContext{Root: root}}
|
||||
}
|
||||
var doc map[string]any
|
||||
if inline == nil {
|
||||
loaded, err := configkit.LoadLayered(base, configkit.LayeredOptions[map[string]any]{Definition: definition})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load %s vocabulary %q: %w", name, path, err)
|
||||
return nil, fmt.Errorf("load %s vocabulary: %w", name, err)
|
||||
}
|
||||
if _, ok := loaded.Config[name]; !ok {
|
||||
return nil, fmt.Errorf("%s vocabulary %q does not define collection %q", name, path, name)
|
||||
doc = loaded.Config
|
||||
} else {
|
||||
main := configkit.Input{Source: configkit.Mapping("inline vocabulary", inline), Destination: configkit.Keys("collections", name)}
|
||||
loaded, err := configkit.LoadLayered(main, configkit.LayeredOptions[map[string]any]{Definition: definition, Underlays: []configkit.Underlay{{Inputs: []configkit.Input{base}}}})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("compose %s vocabulary: %w", name, err)
|
||||
}
|
||||
if inline == nil {
|
||||
return loaded.Config[name], nil
|
||||
}
|
||||
underlays = []configkit.Underlay{{Inputs: []configkit.Input{file}}}
|
||||
doc = loaded.Config
|
||||
}
|
||||
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)
|
||||
if err := domaininput.ValidateGlossary(doc); err != nil {
|
||||
return nil, fmt.Errorf("%s vocabulary: %w", name, err)
|
||||
}
|
||||
value, ok := loaded.Config[name]
|
||||
collections := doc["collections"].(map[string]any)
|
||||
collection, ok := collections[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s vocabulary is missing", name)
|
||||
return nil, fmt.Errorf("%s vocabulary does not define collection %q", name, name)
|
||||
}
|
||||
return value, nil
|
||||
projection := make(map[string]any, len(doc))
|
||||
for key, value := range doc {
|
||||
projection[key] = value
|
||||
}
|
||||
projection["collections"] = map[string]any{name: collection}
|
||||
return projection, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user