Adopt shared campaign and session configuration
This commit is contained in:
@@ -7,20 +7,21 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/domaininput"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
const maxConfigDocumentBytes int64 = 8 << 20
|
||||
|
||||
type documentResolution struct {
|
||||
campaign *configkit.Result[CampaignConfig]
|
||||
campaign *configkit.Result[CampaignDocument]
|
||||
session *configkit.Result[SessionConfig]
|
||||
sources map[string]string
|
||||
}
|
||||
|
||||
// ConfigKitResult returns the campaign's actual completed native load result.
|
||||
// Constructed campaign values have no result.
|
||||
func (cfg *CampaignConfig) ConfigKitResult() *configkit.Result[CampaignConfig] {
|
||||
func (cfg *CampaignConfig) ConfigKitResult() *configkit.Result[CampaignDocument] {
|
||||
if cfg == nil || cfg.resolution == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -75,27 +76,56 @@ func documentOptionalResource(resolution *configkit.Resolution, path configkit.P
|
||||
configkit.Normalize(resolution, path, value, &resolved, "resolved relative resource from its source")
|
||||
}
|
||||
|
||||
func campaignDefinition(invocationDir string, sources map[string]string, expected string) configkit.Definition[CampaignConfig] {
|
||||
return configkit.Definition[CampaignConfig]{Resolve: func(cfg *CampaignConfig, resolution *configkit.Resolution) error {
|
||||
documentResource(resolution, configkit.Keys("session_template_file"), &cfg.SessionTemplateFile, invocationDir, sources)
|
||||
inputs := configkit.Keys("inputs")
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
value *string
|
||||
}{
|
||||
{"speakers_file", &cfg.Inputs.SpeakersFile}, {"autocorrect_file", &cfg.Inputs.AutocorrectFile},
|
||||
{"party_file", &cfg.Inputs.PartyFile},
|
||||
{"spell_catalog_file", &cfg.Inputs.SpellCatalogFile},
|
||||
} {
|
||||
documentResource(resolution, inputs.Key(field.name), field.value, invocationDir, sources)
|
||||
func campaignDefinition(invocationDir string, sources map[string]string, expected string) configkit.Definition[CampaignDocument] {
|
||||
presence := make(map[string]configkit.FieldInfo)
|
||||
return configkit.Definition[CampaignDocument]{Resolve: func(cfg *CampaignDocument, resolution *configkit.Resolution) error {
|
||||
narratio := configkit.Keys("narratio")
|
||||
documentResource(resolution, narratio.Key("session_template_file"), &cfg.Narratio.SessionTemplateFile, invocationDir, sources)
|
||||
inputs := narratio.Key("inputs")
|
||||
documentResource(resolution, inputs.Key("speakers_file"), &cfg.Narratio.Inputs.SpeakersFile, invocationDir, sources)
|
||||
documentOptionalResource(resolution, inputs.Key("glossary_file"), &cfg.Narratio.Inputs.GlossaryFile, invocationDir, sources)
|
||||
if party, ok := cfg.References["party"].(string); ok {
|
||||
documentResource(resolution, configkit.Keys("references", "party"), &party, invocationDir, sources)
|
||||
cfg.References["party"] = party
|
||||
}
|
||||
if cfg.Inputs.GlossaryFile != nil {
|
||||
documentOptionalResource(resolution, inputs.Key("glossary_file"), &cfg.Inputs.GlossaryFile, invocationDir, sources)
|
||||
for _, name := range []string{"schema_version", "id", "name", "description", "setting", "system", "references"} {
|
||||
if info, ok := resolution.EntryInfo(configkit.Keys(name)); ok {
|
||||
presence[name] = info
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}, Validate: func(cfg *CampaignConfig, v *configkit.Validation) {
|
||||
validateCampaignConfigKit(cfg, v)
|
||||
validateIdentity(v, "campaign_id", expected, CampaignID(cfg))
|
||||
}, Validate: func(cfg *CampaignDocument, v *configkit.Validation) {
|
||||
shared := make(map[string]any, len(cfg.Extensions)+7)
|
||||
for key, value := range cfg.Extensions {
|
||||
shared[key] = value
|
||||
}
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
value any
|
||||
}{
|
||||
{"schema_version", cfg.SchemaVersion}, {"id", cfg.ID}, {"name", cfg.Name}, {"description", cfg.Description},
|
||||
{"setting", cfg.Setting}, {"system", cfg.System}, {"references", cfg.References},
|
||||
} {
|
||||
if info, ok := presence[field.name]; ok && info.Explicit {
|
||||
if info.Null {
|
||||
shared[field.name] = nil
|
||||
} else {
|
||||
shared[field.name] = field.value
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := domaininput.ValidateCampaign(shared); err != nil {
|
||||
v.Error(configkit.Keys(), err.Error())
|
||||
}
|
||||
if path, err := campaignConfigurationError(campaignDomain(*cfg)); err != nil {
|
||||
v.Error(path, err.Error())
|
||||
}
|
||||
validateIdentity(v, "id", expected, strings.TrimSpace(cfg.ID))
|
||||
for _, retired := range []string{"campaign_id", "inputs", "session_template_file"} {
|
||||
if _, found := cfg.Extensions[retired]; found {
|
||||
v.Error(configkit.Keys(retired), "retired campaign field")
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
@@ -113,8 +143,6 @@ func sessionDefinition(invocationDir string, sources map[string]string, label st
|
||||
value *string
|
||||
}{
|
||||
{"audio_dir", &cfg.Inputs.AudioDir}, {"speakers_file", &cfg.Inputs.SpeakersFile},
|
||||
{"autocorrect_file", &cfg.Inputs.AutocorrectFile},
|
||||
{"spell_catalog_file", &cfg.Inputs.SpellCatalogFile},
|
||||
} {
|
||||
documentResource(resolution, inputs.Key(field.name), field.value, invocationDir, sources)
|
||||
}
|
||||
@@ -165,8 +193,6 @@ func applyCampaignSessionDefaults(cfg *SessionConfig, resolution *configkit.Reso
|
||||
target *string
|
||||
}{
|
||||
{"speakers_file", campaign.Inputs.SpeakersFile, &cfg.Inputs.SpeakersFile},
|
||||
{"autocorrect_file", campaign.Inputs.AutocorrectFile, &cfg.Inputs.AutocorrectFile},
|
||||
{"spell_catalog_file", campaign.Inputs.SpellCatalogFile, &cfg.Inputs.SpellCatalogFile},
|
||||
} {
|
||||
if field.value != "" {
|
||||
configkit.Default(resolution, configkit.Keys("inputs", field.name), field.target, field.value, reason)
|
||||
@@ -188,6 +214,9 @@ func applyCampaignSessionDefaults(cfg *SessionConfig, resolution *configkit.Reso
|
||||
if campaign.Inputs.Glossary != nil {
|
||||
configkit.Default(resolution, configkit.Keys("inputs", "glossary"), &cfg.Inputs.Glossary, campaign.Inputs.Glossary, reason)
|
||||
}
|
||||
if campaign.Inputs.Autocorrect != nil {
|
||||
configkit.Default(resolution, configkit.Keys("inputs", "autocorrect"), &cfg.Inputs.Autocorrect, campaign.Inputs.Autocorrect, reason)
|
||||
}
|
||||
}
|
||||
|
||||
// LoadCampaignWithIdentity validates an optional asserted identity within the native load.
|
||||
@@ -197,15 +226,33 @@ func LoadCampaignWithIdentity(path, expected string) (*CampaignConfig, error) {
|
||||
return nil, err
|
||||
}
|
||||
sources := make(map[string]string)
|
||||
result, err := configkit.Load(configkit.File(path), configkit.Options[CampaignConfig]{Imports: true, Definition: campaignDefinition(invocationDir, sources, expected)})
|
||||
result, err := configkit.Load(configkit.File(path), configkit.Options[CampaignDocument]{Imports: true, Definition: campaignDefinition(invocationDir, sources, expected)})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load campaign config: %w", err)
|
||||
}
|
||||
cfg := &result.Config
|
||||
doc := result.Config
|
||||
cfg := campaignDomain(doc)
|
||||
cfg.resolution = &documentResolution{campaign: result, sources: sources}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func campaignDomain(doc CampaignDocument) *CampaignConfig {
|
||||
cfg := &CampaignConfig{CampaignID: doc.ID, SessionTemplateFile: doc.Narratio.SessionTemplateFile, Inputs: CampaignInputsConfig{
|
||||
SpeakersFile: doc.Narratio.Inputs.SpeakersFile, GlossaryFile: doc.Narratio.Inputs.GlossaryFile,
|
||||
Glossary: doc.Narratio.Inputs.Glossary, Autocorrect: doc.Narratio.Inputs.Autocorrect,
|
||||
}}
|
||||
if value, ok := doc.Setting.(string); ok {
|
||||
cfg.Inputs.Setting = &value
|
||||
}
|
||||
if value, ok := doc.System.(string); ok {
|
||||
cfg.Inputs.System = &value
|
||||
}
|
||||
if value, ok := doc.References["party"].(string); ok {
|
||||
cfg.Inputs.PartyFile = value
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func loadSessionSource(source configkit.Source, label string, opts SessionLoadOptions, imports bool) (*SessionConfig, string, error) {
|
||||
return loadSessionSourceWithFilesystem(source, label, opts, imports, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user