Make explicit metadata queries lightweight

This commit is contained in:
2026-10-09 03:18:50 +00:00
parent b59f002314
commit a2084df11a
3 changed files with 93 additions and 7 deletions

View File

@@ -285,6 +285,75 @@ func TestResolutionInputKeepsDetachedEntryMetadata(t *testing.T) {
}
}
func TestMetadataIsExplicitAgreesWithLookup(t *testing.T) {
t.Parallel()
type entry struct {
Kept string `yaml:"kept"`
Set string `yaml:"set"`
Enabled bool `yaml:"enabled"`
Count int `yaml:"count"`
}
type config struct {
Nested entry `yaml:"nested"`
Nullable *string `yaml:"nullable"`
Nodes map[string]entry `yaml:"nodes"`
}
input := []byte(`nested: {set: configured, enabled: false, count: 0}
nullable: null
nodes:
base: {enabled: false, count: 0}
copy: {extends: base}
`)
result, err := testLoad(Bytes("metadata-presence.yml", input), Options[config]{Definition: Definition[config]{
Defaults: func() config {
return config{
Nested: entry{Kept: "default", Set: "default", Enabled: true, Count: 7},
Nodes: map[string]entry{"copy": {Kept: "node default"}},
}
},
}})
if err != nil {
t.Fatal(err)
}
metadata := result.Metadata()
tests := []struct {
name string
path Path
wantExplicit bool
wantNull bool
wantInherited bool
}{
{name: "default", path: Keys("nested", "kept")},
{name: "direct input", path: Keys("nested", "set"), wantExplicit: true},
{name: "explicit false", path: Keys("nested", "enabled"), wantExplicit: true},
{name: "explicit zero", path: Keys("nested", "count"), wantExplicit: true},
{name: "explicit null", path: Keys("nullable"), wantExplicit: true, wantNull: true},
{name: "inherited input", path: Keys("nodes", "copy", "enabled"), wantExplicit: true, wantInherited: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
info, found := metadata.Lookup(test.path)
if !found {
t.Fatalf("Lookup(%s) did not find an effective path", test.path)
}
if got := metadata.IsExplicit(test.path); got != info.Explicit || got != test.wantExplicit {
t.Fatalf("IsExplicit(%s) = %v, Lookup explicit = %v, want %v", test.path, got, info.Explicit, test.wantExplicit)
}
if info.Null != test.wantNull {
t.Fatalf("Lookup(%s).Null = %v, want %v", test.path, info.Null, test.wantNull)
}
if inherited := len(info.Origin.Inheritance) != 0; inherited != test.wantInherited {
t.Fatalf("Lookup(%s) inherited = %v, want %v", test.path, inherited, test.wantInherited)
}
})
}
if result.Config.Nested.Enabled || result.Config.Nested.Count != 0 || result.Config.Nullable != nil {
t.Fatalf("explicit values changed during loading: %#v", result.Config)
}
}
func TestQueriesRespectActualHistoricalAndEffectiveContainers(t *testing.T) {
result, err := testLoad[map[string]any](Bytes("base", []byte("items: [1]\nm: {'0': 2}\nx: 3\nretired: {child: 4}\n")), Options[map[string]any]{Overrides: []Override{{Path: Keys("retired"), Value: "removed", Flag: "--retired"}}})
if err != nil {