Complete session defaults within the ConfigKit lifecycle
This commit is contained in:
@@ -26,6 +26,20 @@ func (cfg *CampaignConfig) ConfigKitResult() *configkit.Result[CampaignConfig] {
|
||||
return cfg.resolution.campaign
|
||||
}
|
||||
|
||||
// ConfigKitResult returns the session's actual completed native load result.
|
||||
// Constructed session values have no result.
|
||||
func (cfg *SessionConfig) ConfigKitResult() *configkit.Result[SessionConfig] {
|
||||
if cfg == nil || cfg.resolution == nil {
|
||||
return nil
|
||||
}
|
||||
return cfg.resolution.session
|
||||
}
|
||||
|
||||
func sessionFieldExplicit(cfg *SessionConfig, name string) bool {
|
||||
result := cfg.ConfigKitResult()
|
||||
return result != nil && result.Metadata().IsExplicit(configkit.Keys("inputs", name))
|
||||
}
|
||||
|
||||
func documentResource(resolution *configkit.Resolution, path configkit.Path, value *string, invocationDir string, sources map[string]string) {
|
||||
if info, ok := resolution.EntryInfo(path); ok && len(info.Origin.Locations) == 1 {
|
||||
if source, ok := resolution.Source(info.Origin.Locations[0].SourceID); ok {
|
||||
@@ -57,8 +71,14 @@ func campaignDefinition(invocationDir string, sources map[string]string) configk
|
||||
}, Validate: validateCampaignConfigKit}
|
||||
}
|
||||
|
||||
func sessionDefinition(invocationDir string, sources map[string]string) configkit.Definition[SessionConfig] {
|
||||
func sessionDefinition(invocationDir string, sources map[string]string, label string, opts SessionLoadOptions) configkit.Definition[SessionConfig] {
|
||||
hasSource := false
|
||||
return configkit.Definition[SessionConfig]{Resolve: func(cfg *SessionConfig, resolution *configkit.Resolution) error {
|
||||
info, ok := resolution.EntryInfo(configkit.Keys())
|
||||
hasSource = ok && len(info.Origin.Locations) > 0
|
||||
if !hasSource {
|
||||
return nil
|
||||
}
|
||||
inputs := configkit.Keys("inputs")
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
@@ -73,10 +93,51 @@ func sessionDefinition(invocationDir string, sources map[string]string) configki
|
||||
for index := range cfg.Inputs.AudioFiles {
|
||||
documentResource(resolution, inputs.Key("audio_files").Index(index), &cfg.Inputs.AudioFiles[index], invocationDir, sources)
|
||||
}
|
||||
applyCampaignSessionDefaults(cfg, resolution, opts)
|
||||
return nil
|
||||
}, Validate: func(cfg *SessionConfig, validation *configkit.Validation) {
|
||||
// Optional discovery with no selected document leaves acquisition to
|
||||
// the caller's next source. There is no session to validate yet.
|
||||
if !hasSource {
|
||||
return
|
||||
}
|
||||
data, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
validation.Error(configkit.Keys(), "cannot inspect session configuration")
|
||||
return
|
||||
}
|
||||
if err := rejectSessionTemplatePlaceholders(label, string(data)); err != nil {
|
||||
validation.Error(configkit.Keys(), err.Error())
|
||||
return
|
||||
}
|
||||
if path, err := sessionConfigurationError(cfg); err != nil {
|
||||
validation.Error(path, err.Error())
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
func applyCampaignSessionDefaults(cfg *SessionConfig, resolution *configkit.Resolution, opts SessionLoadOptions) {
|
||||
if opts.Campaign == nil {
|
||||
return
|
||||
}
|
||||
campaign := opts.Campaign
|
||||
reason := fmt.Sprintf("default from campaign %q (%s)", CampaignID(campaign), opts.CampaignPath)
|
||||
configkit.Default(resolution, configkit.Keys("campaign"), &cfg.Campaign, CampaignID(campaign), reason)
|
||||
for _, field := range []struct {
|
||||
name, value string
|
||||
target *string
|
||||
}{
|
||||
{"speakers_file", campaign.Inputs.SpeakersFile, &cfg.Inputs.SpeakersFile},
|
||||
{"autocorrect_file", campaign.Inputs.AutocorrectFile, &cfg.Inputs.AutocorrectFile},
|
||||
{"glossary_file", campaign.Inputs.GlossaryFile, &cfg.Inputs.GlossaryFile},
|
||||
{"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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loadCampaignWithConfigKit(path string) (*CampaignConfig, error) {
|
||||
invocationDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
@@ -117,7 +178,7 @@ func loadSessionSourceWithFilesystem(source configkit.Source, label string, opts
|
||||
invocationDir = filesystem.BaseDir
|
||||
}
|
||||
sources := make(map[string]string)
|
||||
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Filesystem: filesystem, Definition: sessionDefinition(invocationDir, sources)})
|
||||
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Filesystem: filesystem, Definition: sessionDefinition(invocationDir, sources, label, opts)})
|
||||
if err != nil {
|
||||
return nil, "", describeConfigKitLoadError("session", err)
|
||||
}
|
||||
@@ -129,19 +190,12 @@ func loadSessionSourceWithFilesystem(source configkit.Source, label string, opts
|
||||
if selected == "" {
|
||||
selected = label
|
||||
}
|
||||
cfg := result.Config
|
||||
data, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("inspect session config: %w", err)
|
||||
}
|
||||
if err := rejectSessionTemplatePlaceholders(selected, string(data)); err != nil {
|
||||
return nil, "", fmt.Errorf("load session config: %w", err)
|
||||
}
|
||||
if err := validateSessionIdentity(selected, &cfg, opts); err != nil {
|
||||
cfg := &result.Config
|
||||
if err := validateSessionIdentity(selected, cfg, opts); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
cfg.resolution = &documentResolution{session: result, sources: sources}
|
||||
return &cfg, filepath.Clean(selected), nil
|
||||
return cfg, filepath.Clean(selected), nil
|
||||
}
|
||||
|
||||
// DiscoverSession loads the first selected local candidate, or returns nil when
|
||||
|
||||
Reference in New Issue
Block a user