From cb313242436e53ba145e5e26cbe6c35169a1e5ab Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 5 Dec 2026 16:11:40 +0000 Subject: [PATCH] Demonstrate reporting failed configuration loads --- docs/roadmap/implementation.md | 2 + examples/consumers/failure_inspection.go | 102 ++++++++++++++++++ examples/consumers/failure_inspection_test.go | 60 +++++++++++ 3 files changed, 164 insertions(+) create mode 100644 examples/consumers/failure_inspection.go create mode 100644 examples/consumers/failure_inspection_test.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 976f52d..8fff7ab 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -297,6 +297,8 @@ new security policy under the guise of tests. ## Stage 9 — Demonstrate one-invocation diagnostic reporting +**Complete.** + Add a maintained example under examples/consumers and a small behavioral test. Let an application validator reject an unsupported backend from an imported or overlaid source. The example handles the error, obtains root/focused inspection, diff --git a/examples/consumers/failure_inspection.go b/examples/consumers/failure_inspection.go new file mode 100644 index 0000000..fc4c856 --- /dev/null +++ b/examples/consumers/failure_inspection.go @@ -0,0 +1,102 @@ +package consumers + +import ( + "errors" + "fmt" + "io" + + configkit "go.maximumdirect.net/configkit" +) + +// BackendConfiguration is an application-owned configuration. +type BackendConfiguration struct { + Storage struct { + Backend string `yaml:"backend"` + } `yaml:"storage"` +} + +// ConfigurationInspector is the reporting interface shared by results and errors. +type ConfigurationInspector interface { + InspectSubtree(configkit.Path) (configkit.Inspection, bool) +} + +// BackendReport retains reporting evidence and diagnostics from one load. +type BackendReport struct { + Inspection configkit.Inspection + Available bool + Diagnostics []configkit.Diagnostic + storage configkit.Inspection + hasStorage bool +} + +// InspectBackend loads once and preserves the original error along with any +// available inspection. Imports are enabled for the application's source. +func InspectBackend(source configkit.Source) (BackendReport, error) { + result, err := configkit.Load(source, configkit.Options[BackendConfiguration]{ + Imports: true, + Definition: configkit.Definition[BackendConfiguration]{Validate: func(value *BackendConfiguration, validation *configkit.Validation) { + if value.Storage.Backend != "local" && value.Storage.Backend != "memory" { + validation.Error(configkit.Keys("storage", "backend"), "unsupported storage backend") + } + }}, + }) + var provider ConfigurationInspector = result + var diagnostics []configkit.Diagnostic + if err != nil { + var failure *configkit.LoadError + if !errors.As(err, &failure) { + return BackendReport{}, err + } + provider, diagnostics = failure, failure.Diagnostics() + } + view, ok := provider.InspectSubtree(configkit.Path{}) + storage, hasStorage := provider.InspectSubtree(configkit.Keys("storage")) + return BackendReport{Inspection: view, Available: ok, Diagnostics: diagnostics, storage: storage, hasStorage: hasStorage}, err +} + +// WriteConfigurationReport uses the same show and explain operations for a +// successful result or a failed load. Paths in explanations remain native. +func WriteConfigurationReport(output io.Writer, provider ConfigurationInspector) error { + root, ok := provider.InspectSubtree(configkit.Path{}) + if !ok { + _, err := fmt.Fprintln(output, "Configuration inspection unavailable") + return err + } + if _, err := fmt.Fprintf(output, "Configuration checkpoint: %s\n", root.Stage()); err != nil { + return err + } + focus, ok := provider.InspectSubtree(configkit.Keys("storage")) + if !ok { + return errors.New("storage inspection unavailable") + } + if err := focus.WriteEffective(output); err != nil { + return err + } + return focus.WriteExplanation(output, configkit.Keys("storage", "backend")) +} + +// WriteBackendReport renders retained evidence and any original diagnostics. +func WriteBackendReport(output io.Writer, report BackendReport) error { + if err := configkit.WriteDiagnostics(output, report.Diagnostics); err != nil { + return err + } + return WriteConfigurationReport(output, savedInspection{report.Inspection, report.Available, report.storage, report.hasStorage}) +} + +type savedInspection struct { + view configkit.Inspection + available bool + storage configkit.Inspection + hasStorage bool +} + +func (saved savedInspection) InspectSubtree(path configkit.Path) (configkit.Inspection, bool) { + switch path.String() { + case "": + return saved.view, saved.available + case "/storage": + return saved.storage, saved.hasStorage + default: + return configkit.Inspection{}, false + } +} diff --git a/examples/consumers/failure_inspection_test.go b/examples/consumers/failure_inspection_test.go new file mode 100644 index 0000000..25bb86f --- /dev/null +++ b/examples/consumers/failure_inspection_test.go @@ -0,0 +1,60 @@ +package consumers + +import ( + "bytes" + "errors" + "strings" + "testing" + "testing/fstest" + + configkit "go.maximumdirect.net/configkit" +) + +func TestBackendReportRetainsFailedLoadEvidence(t *testing.T) { + files := fstest.MapFS{ + "main.yml": &fstest.MapFile{Data: []byte("config: {imports: [{path: storage.yml}]}\n")}, + "storage.yml": &fstest.MapFile{Data: []byte("storage: {backend: unsupported}\n")}, + } + filesystem := configkit.NewFilesystem("application", files) + report, err := InspectBackend(filesystem.File("main.yml")) + var failure *configkit.LoadError + if !errors.As(err, &failure) || !report.Available || len(report.Diagnostics) == 0 { + t.Fatalf("report = %#v, error = %v", report, err) + } + info, found := report.Inspection.Metadata().Lookup(configkit.Keys("storage", "backend")) + if !found || len(info.Origin.Locations) == 0 { + t.Fatalf("origin missing: %#v", info) + } + source, found := report.Inspection.Source(info.Origin.Locations[0].SourceID) + if !found || !strings.HasSuffix(source.Label, "storage.yml") { + t.Fatalf("source = %#v", source) + } + files["storage.yml"].Data = []byte("storage: {backend: local}\n") + var output bytes.Buffer + if err := WriteBackendReport(&output, report); err != nil { + t.Fatal(err) + } + if !strings.Contains(output.String(), "unsupported") || !strings.Contains(output.String(), "resolved") || !strings.Contains(output.String(), "storage.yml") { + t.Fatalf("report lost original evidence: %q", output.String()) + } + + success, err := configkit.Load[BackendConfiguration](configkit.Bytes("ok.yml", []byte("storage: {backend: local}\n"))) + if err != nil { + t.Fatal(err) + } + output.Reset() + if err := WriteConfigurationReport(&output, success); err != nil || !strings.Contains(output.String(), "validated") { + t.Fatalf("successful report = %q, %v", output.String(), err) + } +} + +func TestBackendReportExplainsUnavailableInspection(t *testing.T) { + report, err := InspectBackend(configkit.Bytes("bad.yml", []byte("[\n"))) + if err == nil || report.Available || len(report.Diagnostics) == 0 { + t.Fatalf("report = %#v, error = %v", report, err) + } + var output bytes.Buffer + if err := WriteBackendReport(&output, report); err != nil || !strings.Contains(output.String(), "inspection unavailable") { + t.Fatalf("early failure report = %q, %v", output.String(), err) + } +}