Preserve terminal values during result lookup

This commit is contained in:
2026-11-21 12:00:29 +00:00
parent bcee3f866f
commit ed5d57d061
4 changed files with 86 additions and 11 deletions

View File

@@ -7,11 +7,18 @@ Scope: the nine stages of [implementation.md](implementation.md), the settled
No implementation changes were made during these reviews. Findings 1–2 come No implementation changes were made during these reviews. Findings 1–2 come
from the correctness/architecture review; finding 3 comes from the subsequent from the correctness/architecture review; finding 3 comes from the subsequent
algorithmic review. All findings below remain open. algorithmic review. Completion notes below track remediation after those reviews.
## 1. Terminal availability still depends on recursive schema validity ## 1. Terminal availability still depends on recursive schema validity
**Priority:** P2. **Status:** Open. **Plan stages:** 1–2. **Priority:** P2. **Status:** Complete. **Plan stages:** 1–2.
**Completion:** lookup descriptors now describe only the containing shape and
necessary inline bindings; ordinary children remain opaque until navigation
continues. Added public regressions for map/slice/array terminals, post-load
dynamic children and siblings, and continued rejection of ambiguous navigation
and invalid load schemas. The regression failed before the fix. Focused tests,
`./scripts/check.sh`, and `go test -race ./...` passed with Go 1.27.4.
**Relevant code:** [ResultValue](../../internal/engine/result_value.go), especially **Relevant code:** [ResultValue](../../internal/engine/result_value.go), especially
its descriptor construction at line 33; the recursive map/slice/array and struct its descriptor construction at line 33; the recursive map/slice/array and struct

View File

@@ -130,3 +130,58 @@ func TestResultValueFrozenInspectionAndSecret(t *testing.T) {
t.Fatalf("frozen output = %s", output.String()) t.Fatalf("frozen output = %s", output.String())
} }
} }
func TestResultValueReturnsTerminalsWithoutSchemaValidation(t *testing.T) {
type terminal struct {
A int `yaml:"same"`
B int `yaml:"same"`
}
want := terminal{A: 1, B: 2}
cases := []struct {
name string
lookup func(configkit.Path) (any, bool)
path configkit.Path
}{
{"map", (&configkit.Result[map[string]terminal]{Config: map[string]terminal{"item": want}}).Value, configkit.Keys("item")},
{"slice", (&configkit.Result[[]terminal]{Config: []terminal{want}}).Value, configkit.Path{}.Index(0)},
{"array", (&configkit.Result[[1]terminal]{Config: [1]terminal{want}}).Value, configkit.Path{}.Index(0)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, ok := tc.lookup(tc.path)
if !ok || got != want {
t.Fatalf("terminal = %#v, %v", got, ok)
}
if _, ok := tc.lookup(tc.path.Key("same")); ok {
t.Fatal("ambiguous containing bindings accepted")
}
})
}
// A post-load replacement need not itself satisfy the load-time schema.
result, err := configkit.Load[map[string]any](configkit.Bytes("dynamic.yml", []byte("{}")))
if err != nil {
t.Fatal(err)
}
result.Config["dynamic"] = struct {
Child terminal `yaml:"child"`
Good int `yaml:"good"`
}{Child: want, Good: 3}
for _, tc := range []struct {
path configkit.Path
want any
}{
{configkit.Keys("dynamic", "child"), want},
{configkit.Keys("dynamic", "good"), 3},
} {
got, ok := result.Value(tc.path)
if !ok || got != tc.want {
t.Fatalf("%s = %#v, %v", tc.path.String(), got, ok)
}
}
if _, ok := result.Value(configkit.Keys("dynamic", "child", "same")); ok {
t.Fatal("ambiguous dynamic child accepted navigation")
}
if _, err := configkit.Load[map[string]terminal](configkit.NoSource()); err == nil {
t.Fatal("Load accepted an invalid element schema")
}
}

View File

@@ -115,6 +115,8 @@ type schemaBuilder struct {
building map[reflect.Type]bool building map[reflect.Type]bool
journal []reflect.Type journal []reflect.Type
secrets SecretBinding secrets SecretBinding
// lookup describes only the current container and its flattened inline fields.
// Ordinary descendants are described only when navigation continues into them.
lookup bool lookup bool
} }
@@ -129,17 +131,15 @@ func (b *schemaBuilder) describe(current reflect.Type, path Path) (*typeDescript
defer delete(b.building, current) defer delete(b.building, current)
fail := func(code Code, message string) (*typeDescriptor, *schemaIssue) { fail := func(code Code, message string) (*typeDescriptor, *schemaIssue) {
// Lookup may reach unsupported values as terminals; they remain leaves.
if b.lookup && (code == CodeSchemaUnsupportedType || code == CodeSchemaTextCodec) {
descriptor.kind = descriptorInvalid
return descriptor, nil
}
delete(b.cache, current) delete(b.cache, current)
return nil, &schemaIssue{code: code, path: path.clone(), message: message} return nil, &schemaIssue{code: code, path: path.clone(), message: message}
} }
if current.Kind() == reflect.Pointer { if current.Kind() == reflect.Pointer {
descriptor.kind = descriptorPointer descriptor.kind = descriptorPointer
if b.lookup {
return descriptor, nil
}
element, issue := b.describe(current.Elem(), path) element, issue := b.describe(current.Elem(), path)
if issue != nil { if issue != nil {
return nil, issue return nil, issue
@@ -177,6 +177,9 @@ func (b *schemaBuilder) describe(current reflect.Type, path Path) (*typeDescript
return fail(CodeSchemaUnsupportedType, "configuration maps must have string keys") return fail(CodeSchemaUnsupportedType, "configuration maps must have string keys")
} }
descriptor.kind = descriptorMap descriptor.kind = descriptorMap
if b.lookup {
return descriptor, nil
}
element, issue := b.describe(current.Elem(), path) element, issue := b.describe(current.Elem(), path)
if issue != nil { if issue != nil {
return nil, issue return nil, issue
@@ -184,6 +187,9 @@ func (b *schemaBuilder) describe(current reflect.Type, path Path) (*typeDescript
descriptor.element = element descriptor.element = element
case reflect.Slice: case reflect.Slice:
descriptor.kind = descriptorSlice descriptor.kind = descriptorSlice
if b.lookup {
return descriptor, nil
}
element, issue := b.describe(current.Elem(), path.Index(0)) element, issue := b.describe(current.Elem(), path.Index(0))
if issue != nil { if issue != nil {
return nil, issue return nil, issue
@@ -192,6 +198,9 @@ func (b *schemaBuilder) describe(current reflect.Type, path Path) (*typeDescript
case reflect.Array: case reflect.Array:
descriptor.kind = descriptorArray descriptor.kind = descriptorArray
descriptor.length = current.Len() descriptor.length = current.Len()
if b.lookup {
return descriptor, nil
}
element, issue := b.describe(current.Elem(), path.Index(0)) element, issue := b.describe(current.Elem(), path.Index(0))
if issue != nil { if issue != nil {
return nil, issue return nil, issue

View File

@@ -4,10 +4,14 @@ import "reflect"
func (b *schemaBuilder) describeOrdinaryField(owner *typeDescriptor, field reflect.StructField, index int, name string, secret bool, path Path) *schemaIssue { func (b *schemaBuilder) describeOrdinaryField(owner *typeDescriptor, field reflect.StructField, index int, name string, secret bool, path Path) *schemaIssue {
fieldPath := path.Key(name) fieldPath := path.Key(name)
fieldType, issue := b.describe(field.Type, fieldPath) var fieldType *typeDescriptor
if !b.lookup {
var issue *schemaIssue
fieldType, issue = b.describe(field.Type, fieldPath)
if issue != nil { if issue != nil {
return issue return issue
} }
}
if issue := addField(owner, fieldDescriptor{name: name, index: []int{index}, descriptor: fieldType, secret: secret}, path); issue != nil { if issue := addField(owner, fieldDescriptor{name: name, index: []int{index}, descriptor: fieldType, secret: secret}, path); issue != nil {
return issue return issue
} }