133 lines
4.7 KiB
Go
133 lines
4.7 KiB
Go
package glossary
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"sort"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/domaininput"
|
|
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, Name string
|
|
Aliases []string
|
|
}
|
|
|
|
// Documents holds individual and combined standalone shared envelopes.
|
|
type Documents map[string][]byte
|
|
|
|
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) {
|
|
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 {
|
|
aliases := make([]any, len(c.Aliases))
|
|
for i, alias := range c.Aliases {
|
|
aliases[i] = alias
|
|
}
|
|
entry["aliases"] = aliases
|
|
}
|
|
entries = append(entries, entry)
|
|
}
|
|
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}} {
|
|
doc, err := resolveCollection(item.name, item.path, item.inline)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
selected[item.name] = doc
|
|
}
|
|
combined := make(map[string]any, len(selected))
|
|
result := make(Documents, 5)
|
|
for _, name := range []string{"party", "campaign", "setting", "spells"} {
|
|
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) (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: %w", name, err)
|
|
}
|
|
root := filepath.VolumeName(absolute) + string(filepath.Separator)
|
|
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: %w", name, err)
|
|
}
|
|
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)
|
|
}
|
|
doc = loaded.Config
|
|
}
|
|
if err := domaininput.ValidateGlossary(doc); err != nil {
|
|
return nil, fmt.Errorf("%s vocabulary: %w", name, err)
|
|
}
|
|
collections := doc["collections"].(map[string]any)
|
|
collection, ok := collections[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("%s vocabulary does not define collection %q", name, name)
|
|
}
|
|
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
|
|
}
|