From f514936c9c00425115c9f82f569bae76e18ad236 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 21 Nov 2026 04:05:27 +0000 Subject: [PATCH] Expose current configuration values --- docs/roadmap/implementation.md | 4 +- facade_result_value_test.go | 123 +++++++++++++++++++++++++++++++++ result_value.go | 15 ++++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 facade_result_value_test.go create mode 100644 result_value.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index e085a79..fac97f7 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -1,6 +1,6 @@ # Implementation plan: typed result values and focused inspection -Status: implementation in progress; Stage 1 complete. +Status: implementation in progress; Stages 1–2 complete. ## Scope and working instructions @@ -95,6 +95,8 @@ reachable schema types; lookup should not depend on total runtime collection siz ## Stage 2 — Expose Value and verify its public contract +**Complete.** + Add `func (r *Result[T]) Value(path Path) (any, bool)` in a focused root file, forwarding current `r.Config`, the converted path, and the existing Secret binding. A nil receiver returns `(nil, false)`. Any non-nil result works independently of diff --git a/facade_result_value_test.go b/facade_result_value_test.go new file mode 100644 index 0000000..fb2ab35 --- /dev/null +++ b/facade_result_value_test.go @@ -0,0 +1,123 @@ +package configkit_test + +import ( + "bytes" + "reflect" + "strings" + "testing" + + configkit "go.maximumdirect.net/configkit" +) + +type valueName string +type valueKey string +type valueEmbedded struct{ Label valueName } +type valueCodec struct{ Text string } + +func (valueCodec) MarshalText() ([]byte, error) { panic("codec called during lookup") } +func (*valueCodec) UnmarshalText([]byte) error { panic("codec called during lookup") } + +type valueConfig struct { + Embedded *valueEmbedded `yaml:",inline"` + Extra map[valueKey]any `yaml:",inline"` + List []string `yaml:"list"` + Array [2]int `yaml:"array"` + Dynamic any `yaml:"dynamic"` + Codec valueCodec `yaml:"codec"` + Ignored string `yaml:"-"` + private string +} + +func TestResultValueCurrentNavigation(t *testing.T) { + var missing *configkit.Result[valueConfig] + if _, ok := missing.Value(configkit.Path{}); ok { + t.Fatal("nil result") + } + var nilMap map[string]int + result := &configkit.Result[valueConfig]{Config: valueConfig{ + Embedded: &valueEmbedded{Label: "named"}, Extra: map[valueKey]any{"a/b": nil, "": nilMap, "0": 7}, + List: []string{"first"}, Array: [2]int{1, 2}, Dynamic: map[string]any{"list": []any{valueEmbedded{Label: "dynamic"}}}, + }} + parsed, _ := configkit.ParsePath("/dynamic/list/0/label") + parsedKey, _ := configkit.ParsePath("/0") + if got, ok := result.Value(configkit.Path{}); !ok || !reflect.DeepEqual(got, result.Config) { + t.Fatalf("root = %#v, %v", got, ok) + } + pointer, ok := result.Value(configkit.Keys("dynamic", "list").Index(0)) + if !ok || pointer.(valueEmbedded).Label != "dynamic" { + t.Fatalf("struct terminal = %#v, %v", pointer, ok) + } + for _, tc := range []struct { + path configkit.Path + want any + ok bool + }{ + {configkit.Keys("label"), valueName("named"), true}, + {configkit.Keys("a/b"), nil, true}, {configkit.Keys(""), nilMap, true}, + {parsedKey, 7, true}, {configkit.Path{}.Index(0), nil, false}, + {configkit.Keys("list").Index(0), "first", true}, {configkit.Keys("list", "0"), nil, false}, + {configkit.Keys("list").Index(1), nil, false}, {configkit.Keys("list").Index(-1), nil, false}, + {parsed, valueName("dynamic"), true}, {configkit.Keys("ignored"), nil, false}, {configkit.Keys("private"), nil, false}, + {configkit.Keys("codec"), valueCodec{}, true}, {configkit.Keys("codec", "text"), nil, false}, + } { + got, ok := result.Value(tc.path) + if ok != tc.ok || !reflect.DeepEqual(got, tc.want) { + t.Errorf("%s: got %#v, %v; want %#v, %v", tc.path.String(), got, ok, tc.want, tc.ok) + } + } + copyValue, _ := result.Value(configkit.Keys("array")) + array := copyValue.([2]int) + array[0] = 9 + listValue, _ := result.Value(configkit.Keys("list")) + listValue.([]string)[0] = "changed" + mapValue, _ := result.Value(configkit.Keys("dynamic")) + mapValue.(map[string]any)["added"] = true + if result.Config.Array[0] != 1 || result.Config.List[0] != "changed" { + t.Fatal("assignment ownership differs") + } + if result.Config.Dynamic.(map[string]any)["added"] != true { + t.Fatal("map assignment did not preserve alias") + } + result.Config.Embedded = nil + if _, ok := result.Value(configkit.Keys("label")); ok { + t.Fatal("nil inline pointer") + } + result.Config.Dynamic = make(chan int) + if _, ok := result.Value(configkit.Keys("dynamic")); !ok { + t.Fatal("unsupported terminal unavailable") + } + if _, ok := result.Value(configkit.Keys("dynamic", "x")); ok { + t.Fatal("unsupported value traversed") + } +} + +func TestResultValueFrozenInspectionAndSecret(t *testing.T) { + t.Setenv("CONFIGKIT_VALUE_TEST_SECRET", "disposable-value-canary") + type settings struct { + Name string `yaml:"name"` + Count int `yaml:"count"` + Token configkit.Secret `yaml:"token"` + } + result, err := configkit.Load[settings](configkit.Bytes("settings.yml", []byte("name: before\ntoken: CONFIGKIT_VALUE_TEST_SECRET\n"))) + if err != nil { + t.Fatal(err) + } + if got, ok := result.Value(configkit.Keys("count")); !ok || got != 0 || result.Metadata().IsExplicit(configkit.Keys("count")) { + t.Fatalf("default = %#v, %v", got, ok) + } + secret, ok := result.Value(configkit.Keys("token")) + if !ok || secret.(configkit.Secret).Value() != "disposable-value-canary" { + t.Fatal("resolved secret unavailable") + } + result.Config.Name = "after" + if got, _ := result.Value(configkit.Keys("name")); got != "after" { + t.Fatalf("current value = %v", got) + } + var output bytes.Buffer + if err := result.WriteEffective(&output); err != nil { + t.Fatal(err) + } + if !strings.Contains(output.String(), "before") || strings.Contains(output.String(), "after") || strings.Contains(output.String(), "disposable-value-canary") { + t.Fatalf("frozen output = %s", output.String()) + } +} diff --git a/result_value.go b/result_value.go new file mode 100644 index 0000000..590c67c --- /dev/null +++ b/result_value.go @@ -0,0 +1,15 @@ +package configkit + +import "go.maximumdirect.net/configkit/internal/engine" + +// Value returns the current Config value at path, including values absent from +// input. It returns false for a nil result, invalid path, or missing value. +// Values are trusted application data and may include resolved secrets. Ordinary +// Go assignment applies: maps, slices, and pointers can alias Config. Inspection +// methods continue to report the frozen completed load, not later mutations. +func (r *Result[T]) Value(path Path) (any, bool) { + if r == nil { + return nil, false + } + return engine.ResultValue(r.Config, toEnginePath(path), secretBinding()) +}