Add native configuration subtree scopes
This commit is contained in:
@@ -33,29 +33,7 @@ func FromConfigKit[I, S, T any](
|
||||
}
|
||||
|
||||
scope.Acquire = func(ctx context.Context, request Request, input I, state S) (commandkit.Acquired[View], error) {
|
||||
result, err := load(ctx, request.Selection, input, state)
|
||||
acquired := commandkit.Acquired[View]{}
|
||||
if err != nil {
|
||||
var loadError *configkit.LoadError
|
||||
if errors.As(err, &loadError) && loadError != nil {
|
||||
records := loadError.Diagnostics()
|
||||
messages, explanation := configKitDiagnostics(name, loadError.Sources(), records)
|
||||
acquired.Value.Sources = loadError
|
||||
acquired.Value.Findings = &Findings{Records: records, Messages: messages}
|
||||
acquired.Explanation = explanation
|
||||
return acquired, err
|
||||
}
|
||||
}
|
||||
if result == nil {
|
||||
if err != nil {
|
||||
return acquired, err
|
||||
}
|
||||
return commandkit.Acquired[View]{}, fmt.Errorf("ConfigKit scope %q returned a nil result", name)
|
||||
}
|
||||
records := result.Diagnostics()
|
||||
messages, explanation := configKitDiagnostics(name, result.Sources(), records)
|
||||
acquired.Value = View{Sources: result, Findings: &Findings{Records: records, Messages: messages}}
|
||||
acquired.Explanation = explanation
|
||||
result, acquired, err := acquireConfigKit(ctx, name, load, request.Selection, input, state)
|
||||
if err != nil {
|
||||
return acquired, err
|
||||
}
|
||||
@@ -64,33 +42,10 @@ func FromConfigKit[I, S, T any](
|
||||
return acquired, nil
|
||||
}
|
||||
scope.Compare = func(ctx context.Context, request ComparisonRequest[I], state S) (commandkit.Acquired[[]configkit.Difference], error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return commandkit.Acquired[[]configkit.Difference]{}, fmt.Errorf("ConfigKit scope %q comparison before left load: %w", name, err)
|
||||
}
|
||||
|
||||
acquired := commandkit.Acquired[[]configkit.Difference]{}
|
||||
left, leftDiagnostics, leftExplanation, err := loadComparisonSide(ctx, name, "left", load, request.Left, state)
|
||||
acquired.Diagnostics = append(acquired.Diagnostics, leftDiagnostics...)
|
||||
acquired.Explanation = appendConfigKitExplanation(acquired.Explanation, leftExplanation)
|
||||
left, right, acquired, err := loadComparisonPair(ctx, name, load, request, state)
|
||||
if err != nil {
|
||||
return acquired, err
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
return acquired, fmt.Errorf("ConfigKit scope %q comparison after left load: %w", name, err)
|
||||
}
|
||||
|
||||
right, rightDiagnostics, rightExplanation, err := loadComparisonSide(ctx, name, "right", load, request.Right, state)
|
||||
acquired.Diagnostics = append(acquired.Diagnostics, rightDiagnostics...)
|
||||
acquired.Explanation = appendConfigKitExplanation(acquired.Explanation, rightExplanation)
|
||||
if err != nil {
|
||||
return acquired, err
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
return acquired, fmt.Errorf("ConfigKit scope %q comparison before native diff: %w", name, err)
|
||||
}
|
||||
|
||||
acquired.Value, err = configkit.Diff(left, right)
|
||||
if err != nil {
|
||||
return acquired, fmt.Errorf("ConfigKit scope %q native diff: %w", name, err)
|
||||
@@ -100,6 +55,126 @@ func FromConfigKit[I, S, T any](
|
||||
return scope
|
||||
}
|
||||
|
||||
// FromConfigKitSubtree adapts an application-owned typed loader to the same
|
||||
// operations as FromConfigKit, using native inspection at path. Loading and
|
||||
// validation cover the whole result; sources and findings retain that context.
|
||||
// Discovery and rendering use the focused inspection, while explicit paths and
|
||||
// comparison retain ConfigKit's native semantics. Construction and help do not
|
||||
// load configuration, and a nil loader is recorded as a construction issue.
|
||||
func FromConfigKitSubtree[I, S, T any](
|
||||
name, summary string,
|
||||
path configkit.Path,
|
||||
load func(context.Context, Selection, I, S) (*configkit.Result[T], error),
|
||||
) Scope[I, S] {
|
||||
scope := Scope[I, S]{
|
||||
Name: name, Summary: summary,
|
||||
Operations: []Operation{Validate, Show, Sources, Explain, Diff},
|
||||
Features: ScopeFeatures{PathDiscovery: true, PathShow: true},
|
||||
}
|
||||
if load == nil {
|
||||
return WithScopeError(scope, fmt.Errorf("ConfigKit scope %q loader must not be nil", name))
|
||||
}
|
||||
scope.Acquire = func(ctx context.Context, request Request, input I, state S) (commandkit.Acquired[View], error) {
|
||||
result, acquired, err := acquireConfigKit(ctx, name, load, request.Selection, input, state)
|
||||
if err != nil {
|
||||
return acquired, err
|
||||
}
|
||||
inspection, ok := result.InspectSubtree(path)
|
||||
if !ok {
|
||||
return acquired, fmt.Errorf("ConfigKit scope %q inspection at %q is unavailable", name, path.String())
|
||||
}
|
||||
metadata := inspection.Metadata()
|
||||
acquired.Value.Effective = inspection
|
||||
acquired.Value.Explanation = inspection
|
||||
acquired.Value.Paths = inspection
|
||||
acquired.Value.Metadata = &metadata
|
||||
acquired.Value.Sources = inspection
|
||||
return acquired, nil
|
||||
}
|
||||
scope.Compare = func(ctx context.Context, request ComparisonRequest[I], state S) (commandkit.Acquired[[]configkit.Difference], error) {
|
||||
left, right, acquired, err := loadComparisonPair(ctx, name, load, request, state)
|
||||
if err != nil {
|
||||
return acquired, err
|
||||
}
|
||||
leftView, ok := left.InspectSubtree(path)
|
||||
if !ok {
|
||||
return acquired, fmt.Errorf("ConfigKit scope %q left inspection at %q is unavailable", name, path.String())
|
||||
}
|
||||
rightView, ok := right.InspectSubtree(path)
|
||||
if !ok {
|
||||
return acquired, fmt.Errorf("ConfigKit scope %q right inspection at %q is unavailable", name, path.String())
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return acquired, fmt.Errorf("ConfigKit scope %q comparison before native diff: %w", name, err)
|
||||
}
|
||||
acquired.Value, err = leftView.Diff(rightView)
|
||||
if err != nil {
|
||||
return acquired, fmt.Errorf("ConfigKit scope %q native diff: %w", name, err)
|
||||
}
|
||||
return acquired, nil
|
||||
}
|
||||
return scope
|
||||
}
|
||||
|
||||
func acquireConfigKit[I, S, T any](ctx context.Context, name string,
|
||||
load func(context.Context, Selection, I, S) (*configkit.Result[T], error),
|
||||
selection Selection, input I, state S,
|
||||
) (*configkit.Result[T], commandkit.Acquired[View], error) {
|
||||
result, err := load(ctx, selection, input, state)
|
||||
acquired := commandkit.Acquired[View]{}
|
||||
if err != nil {
|
||||
var loadError *configkit.LoadError
|
||||
if errors.As(err, &loadError) && loadError != nil {
|
||||
records := loadError.Diagnostics()
|
||||
messages, explanation := configKitDiagnostics(name, loadError.Sources(), records)
|
||||
acquired.Value.Sources = loadError
|
||||
acquired.Value.Findings = &Findings{Records: records, Messages: messages}
|
||||
acquired.Explanation = explanation
|
||||
return result, acquired, err
|
||||
}
|
||||
}
|
||||
if result == nil {
|
||||
if err != nil {
|
||||
return nil, acquired, err
|
||||
}
|
||||
return nil, acquired, fmt.Errorf("ConfigKit scope %q returned a nil result", name)
|
||||
}
|
||||
records := result.Diagnostics()
|
||||
messages, explanation := configKitDiagnostics(name, result.Sources(), records)
|
||||
acquired.Value = View{Sources: result, Findings: &Findings{Records: records, Messages: messages}}
|
||||
acquired.Explanation = explanation
|
||||
return result, acquired, err
|
||||
}
|
||||
|
||||
func loadComparisonPair[I, S, T any](ctx context.Context, name string,
|
||||
load func(context.Context, Selection, I, S) (*configkit.Result[T], error),
|
||||
request ComparisonRequest[I], state S,
|
||||
) (*configkit.Result[T], *configkit.Result[T], commandkit.Acquired[[]configkit.Difference], error) {
|
||||
acquired := commandkit.Acquired[[]configkit.Difference]{}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, acquired, fmt.Errorf("ConfigKit scope %q comparison before left load: %w", name, err)
|
||||
}
|
||||
left, diagnostics, explanation, err := loadComparisonSide(ctx, name, "left", load, request.Left, state)
|
||||
acquired.Diagnostics = append(acquired.Diagnostics, diagnostics...)
|
||||
acquired.Explanation = appendConfigKitExplanation(acquired.Explanation, explanation)
|
||||
if err != nil {
|
||||
return nil, nil, acquired, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, acquired, fmt.Errorf("ConfigKit scope %q comparison after left load: %w", name, err)
|
||||
}
|
||||
right, diagnostics, explanation, err := loadComparisonSide(ctx, name, "right", load, request.Right, state)
|
||||
acquired.Diagnostics = append(acquired.Diagnostics, diagnostics...)
|
||||
acquired.Explanation = appendConfigKitExplanation(acquired.Explanation, explanation)
|
||||
if err != nil {
|
||||
return nil, nil, acquired, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, acquired, fmt.Errorf("ConfigKit scope %q comparison before native diff: %w", name, err)
|
||||
}
|
||||
return left, right, acquired, nil
|
||||
}
|
||||
|
||||
func loadComparisonSide[I, S, T any](
|
||||
ctx context.Context,
|
||||
scope, side string,
|
||||
|
||||
Reference in New Issue
Block a user