Demonstrate composing two component consumers
This commit is contained in:
86
examples/consumers/result_composition.go
Normal file
86
examples/consumers/result_composition.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package consumers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
// AuditSettings is a second library's independent configuration type.
|
||||
type AuditSettings struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Rules []AuditRule `yaml:"rules"`
|
||||
}
|
||||
|
||||
// AuditRule has an identity used by the application's assembly policy.
|
||||
type AuditRule struct {
|
||||
ID string `yaml:"id"`
|
||||
}
|
||||
|
||||
// ComposedApplication combines application and two library settings.
|
||||
type ComposedApplication struct {
|
||||
Label string `yaml:"label"`
|
||||
Component ComponentSettings `yaml:"component"`
|
||||
Audit AuditSettings `yaml:"audit"`
|
||||
}
|
||||
|
||||
// AuditResult is the smaller interface needed by the audit consumer.
|
||||
type AuditResult interface {
|
||||
Value(configkit.Path) (any, bool)
|
||||
}
|
||||
|
||||
// NewAuditSettings takes a copy of the second library's settings.
|
||||
func NewAuditSettings(result AuditResult) (AuditSettings, error) {
|
||||
value, ok := result.Value(configkit.Keys("audit"))
|
||||
settings, typed := value.(AuditSettings)
|
||||
if !ok || !typed {
|
||||
return AuditSettings{}, errors.New("audit settings unavailable")
|
||||
}
|
||||
settings.Rules = append([]AuditRule(nil), settings.Rules...)
|
||||
return settings, nil
|
||||
}
|
||||
|
||||
func auditDefinition[T any](selectSettings func(*T) *AuditSettings) configkit.Definition[T] {
|
||||
audit := configkit.Keys("audit")
|
||||
return configkit.Definition[T]{
|
||||
SequenceIdentities: []configkit.SequenceIdentity{configkit.SequenceIdentityAt(audit.Key("rules"), "id")},
|
||||
Resolve: func(root *T, resolution *configkit.Resolution) error {
|
||||
configkit.Default(resolution, audit.Key("enabled"), &selectSettings(root).Enabled, true, "enable audit by default")
|
||||
return nil
|
||||
},
|
||||
Validate: func(root *T, validation *configkit.Validation) {
|
||||
if selectSettings(root).Enabled && len(selectSettings(root).Rules) == 0 {
|
||||
validation.Error(audit.Key("rules"), "enabled audit requires a rule")
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// LoadComposedApplication applies application and library callbacks in order in
|
||||
// one load. A placed component input remains an ordinary underlay.
|
||||
func LoadComposedApplication(main, component configkit.Input) (*configkit.Result[ComposedApplication], error) {
|
||||
componentRules := ComponentDefinition(func(root *ComposedApplication) *ComponentSettings { return &root.Component })
|
||||
auditRules := auditDefinition(func(root *ComposedApplication) *AuditSettings { return &root.Audit })
|
||||
identities := append(componentRules.SequenceIdentities, auditRules.SequenceIdentities...)
|
||||
definition := configkit.Definition[ComposedApplication]{
|
||||
SequenceIdentities: identities,
|
||||
Sensitive: componentRules.Sensitive,
|
||||
Resolve: func(root *ComposedApplication, resolution *configkit.Resolution) error {
|
||||
configkit.Normalize(resolution, configkit.Keys("label"), &root.Label, strings.TrimSpace(root.Label), "trim application label")
|
||||
if err := componentRules.Resolve(root, resolution); err != nil {
|
||||
return err
|
||||
}
|
||||
return auditRules.Resolve(root, resolution)
|
||||
},
|
||||
Validate: func(root *ComposedApplication, validation *configkit.Validation) {
|
||||
if root.Label == "" {
|
||||
validation.Error(configkit.Keys("label"), "application label is required")
|
||||
}
|
||||
componentRules.Validate(root, validation)
|
||||
auditRules.Validate(root, validation)
|
||||
},
|
||||
}
|
||||
component.Destination = configkit.Keys("component")
|
||||
return configkit.LoadLayered(main, configkit.LayeredOptions[ComposedApplication]{Underlays: []configkit.Underlay{{Inputs: []configkit.Input{component}}}, Definition: definition})
|
||||
}
|
||||
Reference in New Issue
Block a user