Demonstrate composed configuration comparison
This commit is contained in:
@@ -31,10 +31,15 @@ type inspectionInput struct {
|
||||
Limit commandkit.Supplied[int]
|
||||
}
|
||||
|
||||
type applicationCacheKey struct {
|
||||
selection ckconfig.Selection
|
||||
campaign commandkit.Supplied[string]
|
||||
}
|
||||
|
||||
// runCache belongs to one invocation and is shared only where the application
|
||||
// chooses to pass it to its loaders.
|
||||
type runCache struct {
|
||||
application *configkit.Result[appConfiguration]
|
||||
applications map[applicationCacheKey]*configkit.Result[appConfiguration]
|
||||
}
|
||||
|
||||
type restoreInput struct {
|
||||
@@ -53,22 +58,44 @@ func campaignSelector() commandkit.Flag[*inspectionInput] {
|
||||
func(input **inspectionInput, value commandkit.Supplied[string]) { (*input).Campaign = value })
|
||||
}
|
||||
|
||||
func loadApplicationConfig(ctx context.Context, _ ckconfig.Selection, _ *inspectionInput, cache *runCache) (*configkit.Result[appConfiguration], error) {
|
||||
func loadApplicationConfig(ctx context.Context, selection ckconfig.Selection, input *inspectionInput, cache *runCache) (*configkit.Result[appConfiguration], error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cache != nil && cache.application != nil {
|
||||
return cache.application, nil
|
||||
campaignInput := commandkit.Supplied[string]{}
|
||||
if input != nil {
|
||||
campaignInput = input.Campaign
|
||||
}
|
||||
result, err := configkit.Load[appConfiguration](configkit.Mapping("composed application settings", map[string]any{
|
||||
key := applicationCacheKey{selection: selection, campaign: campaignInput}
|
||||
if cache != nil {
|
||||
if cached, ok := cache.applications[key]; ok {
|
||||
return cached, nil
|
||||
}
|
||||
}
|
||||
source := configkit.Mapping("composed application settings", map[string]any{
|
||||
"default_campaign": "spring",
|
||||
"campaigns": map[string]any{
|
||||
"spring": map[string]any{"prompt_id": "briefing", "prompt_version": "1", "profile_id": "spring", "model": "spring-model"},
|
||||
"autumn": map[string]any{"prompt_id": "briefing", "prompt_version": "1", "profile_id": "autumn", "model": "autumn-model"},
|
||||
},
|
||||
}))
|
||||
if err == nil && cache != nil {
|
||||
cache.application = result
|
||||
})
|
||||
if selection.Config.Set {
|
||||
source = configkit.File(selection.Config.Value)
|
||||
}
|
||||
options := configkit.Options[appConfiguration]{}
|
||||
if selection.Overlay.Set {
|
||||
options.Overlay = selection.Overlay.Value
|
||||
options.Filesystem = &configkit.FilesystemContext{Root: ".", BaseDir: "."}
|
||||
}
|
||||
if campaignInput.Set {
|
||||
options.Overrides = []configkit.Override{{Path: configkit.Keys("default_campaign"), Value: campaignInput.Value, Flag: "--campaign"}}
|
||||
}
|
||||
result, err := configkit.Load(source, options)
|
||||
if err == nil && result != nil && cache != nil {
|
||||
if cache.applications == nil {
|
||||
cache.applications = make(map[applicationCacheKey]*configkit.Result[appConfiguration])
|
||||
}
|
||||
cache.applications[key] = result
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user