Preserve terminal values during result lookup
This commit is contained in:
@@ -130,3 +130,58 @@ func TestResultValueFrozenInspectionAndSecret(t *testing.T) {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user