Expose inspection views on load errors

This commit is contained in:
2026-12-05 16:07:09 +00:00
parent 40c1d94b35
commit a7053e5aaa
3 changed files with 139 additions and 0 deletions

View File

@@ -250,6 +250,8 @@ optional failure inspection must not execute hooks skipped by the original error
## Stage 7 — Expose the shared public inspection interface
**Complete.**
Add LoadError.InspectSubtree and the root InspectionStage declarations/Stage method
specified above. Convert stage values explicitly across the private boundary.
Reuse engine.NewInspectionView and existing root conversions; do not embed an

View File

@@ -0,0 +1,87 @@
package configkit_test
import (
"bytes"
"errors"
"reflect"
"testing"
configkit "go.maximumdirect.net/configkit"
)
type inspectionProvider interface {
InspectSubtree(configkit.Path) (configkit.Inspection, bool)
}
func TestLoadErrorInspectionUsesSharedView(t *testing.T) {
type settings struct {
Items []string `yaml:"items"`
Other string `yaml:"other"`
}
input := configkit.Bytes("input.yml", []byte("items: [first, second]\nother: evidence\n"))
options := configkit.Options[settings]{Definition: configkit.Definition[settings]{
Validate: func(_ *settings, v *configkit.Validation) { v.Error(configkit.Keys("other"), "rejected") },
}}
result, err := configkit.Load(input, options)
var failure *configkit.LoadError
if result != nil || !errors.As(err, &failure) || len(failure.Diagnostics()) == 0 {
t.Fatalf("failed load = %v, %v", result, err)
}
var provider inspectionProvider = failure
root, ok := provider.InspectSubtree(configkit.Path{})
if !ok || root.Stage() != configkit.InspectionStageResolved {
t.Fatalf("failed root checkpoint = %q, %v", root.Stage(), ok)
}
if !reflect.DeepEqual(root.Sources(), failure.Sources()) {
t.Fatal("failure and view source catalogs differ")
}
key, ok := provider.InspectSubtree(configkit.Keys("items", "0"))
if !ok {
t.Fatal("numeric key scope unavailable")
}
index, ok := provider.InspectSubtree(configkit.Keys("items").Index(0))
if !ok || !reflect.DeepEqual(key.HistoricalPaths(), index.HistoricalPaths()) {
t.Fatal("equivalent numeric scopes disagree")
}
if _, ok := key.Explain(configkit.Keys("other")); !ok {
t.Fatal("outside native query unavailable")
}
if _, ok := provider.InspectSubtree(configkit.Keys("absent")); !ok {
t.Fatal("absent scope rejected")
}
if _, ok := provider.InspectSubtree(configkit.Path{}.Index(-1)); ok {
t.Fatal("invalid scope accepted")
}
var out bytes.Buffer
if err := key.WriteEffective(&out); err != nil || out.String() != "first\n" {
t.Fatalf("failed view output = %q, %v", out.String(), err)
}
success, err := configkit.Load[settings](input)
if err != nil {
t.Fatal(err)
}
provider = success
successView, ok := provider.InspectSubtree(configkit.Path{})
if !ok || successView.Stage() != configkit.InspectionStageValidated {
t.Fatalf("successful checkpoint = %q, %v", successView.Stage(), ok)
}
}
func TestLoadErrorInspectionUnavailable(t *testing.T) {
for _, failure := range []*configkit.LoadError{nil, {}} {
if _, ok := failure.InspectSubtree(configkit.Path{}); ok {
t.Fatal("unavailable error accepted")
}
}
_, err := configkit.Load[map[string]any](configkit.Bytes("bad.yml", []byte("[\n")))
var failure *configkit.LoadError
if !errors.As(err, &failure) {
t.Fatal(err)
}
if _, ok := failure.InspectSubtree(configkit.Path{}); ok {
t.Fatal("malformed input produced inspection")
}
if (configkit.Inspection{}).Stage() != "" {
t.Fatal("zero view has checkpoint")
}
}

View File

@@ -10,6 +10,45 @@ import (
// enumeration; explicit queries and source records retain their native paths.
type Inspection struct{ view engine.InspectionView }
// InspectionStage identifies the completed processing represented by a view.
// It does not indicate the error location or whether a load succeeded.
type InspectionStage string
// Inspection checkpoints describe completed source assembly, typed processing,
// resolution, and validation. A resolved view does not imply successful secret
// lookup, identity checking, or validation.
const (
InspectionStageMain InspectionStage = "main"
InspectionStageComposed InspectionStage = "composed"
InspectionStageInherited InspectionStage = "inherited"
InspectionStageDecoded InspectionStage = "decoded"
InspectionStageOverridden InspectionStage = "overridden"
InspectionStageResolved InspectionStage = "resolved"
InspectionStageValidated InspectionStage = "validated"
)
// Stage returns the represented checkpoint, or an empty string for a zero view.
func (i Inspection) Stage() InspectionStage {
switch i.view.Stage() {
case engine.InspectionStageMain:
return InspectionStageMain
case engine.InspectionStageComposed:
return InspectionStageComposed
case engine.InspectionStageInherited:
return InspectionStageInherited
case engine.InspectionStageDecoded:
return InspectionStageDecoded
case engine.InspectionStageOverridden:
return InspectionStageOverridden
case engine.InspectionStageResolved:
return InspectionStageResolved
case engine.InspectionStageValidated:
return InspectionStageValidated
default:
return ""
}
}
// InspectSubtree selects a canonical JSON Pointer reporting location, including
// an absent location. Equivalent key and index spellings select the same scope.
// Nil, manually constructed results and invalid paths cannot produce a view.
@@ -21,6 +60,17 @@ func (r *Result[T]) InspectSubtree(path Path) (Inspection, bool) {
return Inspection{view: view}, ok
}
// InspectSubtree selects a canonical reporting location from a failed load's
// available frozen snapshot. An absent location is valid. Nil or zero errors,
// unavailable snapshots, and invalid paths cannot produce a view.
func (e *LoadError) InspectSubtree(path Path) (Inspection, bool) {
if e == nil || e.value == nil {
return Inspection{}, false
}
view, ok := engine.NewInspectionView(e.value.Inspection(), toEnginePath(path))
return Inspection{view: view}, ok
}
// Metadata returns the unfiltered metadata of the completed load.
func (i Inspection) Metadata() Metadata { return Metadata{value: i.view.Inspection().Metadata()} }