116 lines
4.1 KiB
Go
116 lines
4.1 KiB
Go
package glossary
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"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 != "" {
|
|
absolute, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("resolve %s vocabulary path %q: %w", name, path, 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})
|
|
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
|
|
}
|