Use focused native configuration inspection
This commit is contained in:
@@ -53,6 +53,50 @@ func TestCaptureUsesEffectiveSourceContexts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCapturedInspectionUsesNativeView(t *testing.T) {
|
||||
type anotherApplication struct {
|
||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||
Sibling int `yaml:"sibling"`
|
||||
}
|
||||
definition := promptkit.Configure(configkit.Definition[application]{}, func(value *application) *promptkit.Settings { return &value.PromptKit })
|
||||
first, err := configkit.Load(configkit.Bytes("first", []byte("promptkit: {application_field: first}\n")), configkit.Options[application]{Definition: definition})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secondDefinition := promptkit.Configure(configkit.Definition[anotherApplication]{}, func(value *anotherApplication) *promptkit.Settings { return &value.PromptKit })
|
||||
second, err := configkit.Load(configkit.Bytes("second", []byte("promptkit: {application_field: second}\n")), configkit.Options[anotherApplication]{Definition: secondDefinition})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
leftView, _ := first.InspectSubtree(configkit.Keys("promptkit"))
|
||||
rightView, _ := second.InspectSubtree(configkit.Keys("promptkit"))
|
||||
diagnostics := []configkit.Diagnostic{{Message: "warning", Primary: &configkit.Location{SourceID: "first"}, Related: []configkit.Location{{SourceID: "related"}}}}
|
||||
left, err := configuration.Capture(&first.Config.PromptKit, leftView, diagnostics)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
right, err := configuration.Capture(&second.Config.PromptKit, rightView, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
diagnostics[0].Primary.SourceID = "changed"
|
||||
diagnostics[0].Related[0].SourceID = "changed"
|
||||
got := left.Inspection().Diagnostics()
|
||||
if got[0].Primary.SourceID != "first" || got[0].Related[0].SourceID != "related" {
|
||||
t.Fatalf("captured diagnostics = %+v", got)
|
||||
}
|
||||
got[0].Primary.SourceID = "again"
|
||||
got[0].Related[0].SourceID = "again"
|
||||
got = left.Inspection().Diagnostics()
|
||||
if got[0].Primary.SourceID != "first" || got[0].Related[0].SourceID != "related" {
|
||||
t.Fatalf("returned diagnostics aliased: %+v", got)
|
||||
}
|
||||
differences, err := configuration.Diff(left.Inspection(), right.Inspection())
|
||||
if err != nil || len(differences) == 0 || len(left.Inspection().HistoricalPaths()) == 0 {
|
||||
t.Fatalf("diff = %+v, %v", differences, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaptureOwnsEffectiveResources(t *testing.T) {
|
||||
definition := promptkit.Configure(configkit.Definition[application]{}, func(value *application) *promptkit.Settings { return &value.PromptKit })
|
||||
result, err := configkit.Load(configkit.Bytes("application", []byte(`other: application
|
||||
|
||||
@@ -8,24 +8,30 @@ import (
|
||||
"go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
// Inspection is the read-only subset of the retained ConfigKit result used by
|
||||
// Inspection is the read-only subset of the native ConfigKit view used by
|
||||
// the facade. It does not expose typed configuration or resource contexts.
|
||||
type Inspection interface {
|
||||
Metadata() configkit.Metadata
|
||||
Sources() []configkit.SourceInfo
|
||||
Diagnostics() []configkit.Diagnostic
|
||||
HistoricalPaths() []configkit.Path
|
||||
Explain(configkit.Path) (configkit.Explanation, bool)
|
||||
WriteEffective(io.Writer) error
|
||||
WriteExplanation(io.Writer, configkit.Path) error
|
||||
capturedResult() *configkit.Result[document]
|
||||
nativeView() configkit.Inspection
|
||||
}
|
||||
|
||||
type inspectionResult struct {
|
||||
*configkit.Result[document]
|
||||
configkit.Inspection
|
||||
diagnostics []configkit.Diagnostic
|
||||
}
|
||||
|
||||
func (inspection inspectionResult) capturedResult() *configkit.Result[document] {
|
||||
return inspection.Result
|
||||
func (inspection inspectionResult) nativeView() configkit.Inspection {
|
||||
return inspection.Inspection
|
||||
}
|
||||
|
||||
func (inspection inspectionResult) Diagnostics() []configkit.Diagnostic {
|
||||
return ownDiagnostics(inspection.diagnostics)
|
||||
}
|
||||
|
||||
// Inspection returns only the retained result's inspection operations.
|
||||
@@ -33,19 +39,12 @@ func (state *loaded) Inspection() Inspection {
|
||||
if state == nil {
|
||||
return nil
|
||||
}
|
||||
return inspectionResult{Result: state.result}
|
||||
return inspectionResult{Inspection: state.view, diagnostics: state.diagnostics}
|
||||
}
|
||||
|
||||
// Diff compares two captured ConfigKit results without reconstructing them.
|
||||
func Diff(left, right Inspection) ([]configkit.Difference, error) {
|
||||
var leftResult, rightResult *configkit.Result[document]
|
||||
if left != nil {
|
||||
leftResult = left.capturedResult()
|
||||
}
|
||||
if right != nil {
|
||||
rightResult = right.capturedResult()
|
||||
}
|
||||
return configkit.Diff(leftResult, rightResult)
|
||||
return left.nativeView().Diff(right.nativeView())
|
||||
}
|
||||
|
||||
// Sensitive marks credential-shaped provider values beneath PromptKit settings.
|
||||
|
||||
@@ -41,7 +41,6 @@ const (
|
||||
)
|
||||
|
||||
type loaded struct {
|
||||
result *configkit.Result[document]
|
||||
view configkit.Inspection
|
||||
diagnostics []configkit.Diagnostic
|
||||
promptMetadata map[string][]promptInputPresence
|
||||
@@ -204,7 +203,6 @@ func load(main configkit.Input, underlays []configkit.Underlay) (*loaded, error)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load configuration: %w", err)
|
||||
}
|
||||
state.result = result
|
||||
return state, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user