config: Merge with per-value origins; configcmd: UnknownKeysNode
config.Merge(sources...) overlays explicit, ordered sources with the LoadMerged rules (LoadMerged now uses the same code) and returns the origin of every leaf, for a "config show" that says where each value is set. config.ReadFile reads one file as a mapping, Resolver.Paths() exposes the core/paths resolver (e.g. DataDir for defaults). configcmd.UnknownKeysNode checks one section of a parsed document. Needed by the yamase family's layered configuration (yamase ADR-0074). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b7f7ba6b36
commit
185211e304
7 files changed
+262
-13
No files matched your search
@@ -179,3 +179,9 @@ func (r *Resolver) Application() core.Application { return r.app }
|
||||
// Filename returns the configuration file name this Resolver searches
|
||||
// for in every layer.
|
||||
func (r *Resolver) Filename() string { return r.filename }
|
||||
|
||||
// Paths returns the core/paths Resolver this Resolver locates its
|
||||
// directories with (same application, environment and options), for a
|
||||
// caller that also needs another category, e.g. DataDir for defaults
|
||||
// derived from it.
|
||||
func (r *Resolver) Paths() *paths.Resolver { return r.paths }
|
||||
+1
-13
@@ -25,19 +25,7 @@ func mergeMaps(base, over map[string]any) map[string]any {
|
||||
if base == nil {
|
||||
base = map[string]any{}
|
||||
}
|
||||
for k, v := range over {
|
||||
if v == nil {
|
||||
delete(base, k)
|
||||
continue
|
||||
}
|
||||
if overMap, ok := v.(map[string]any); ok {
|
||||
if baseMap, ok := base[k].(map[string]any); ok {
|
||||
base[k] = mergeMaps(baseMap, overMap)
|
||||
continue
|
||||
}
|
||||
}
|
||||
base[k] = v
|
||||
}
|
||||
mergeTracked(base, over, "", "", nil)
|
||||
return base
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Source is one input of Merge: a parsed YAML mapping and the name of
|
||||
// where it came from (a file path, "path [section]", "default", ...),
|
||||
// which Merge records as the origin of every value it contributes.
|
||||
type Source struct {
|
||||
Name string
|
||||
Data map[string]any
|
||||
}
|
||||
|
||||
// Origins maps the dotted path of every leaf of a merged configuration
|
||||
// ("storage.inbound.root") to the Name of the Source that set it. A
|
||||
// leaf is any value that is not a mapping: scalars and lists alike,
|
||||
// since a list is replaced as a whole, never merged element by element.
|
||||
type Origins map[string]string
|
||||
|
||||
// Merge overlays sources lowest priority first, with the same semantics
|
||||
// as LoadMerged (see mergeMaps: maps merge key by key, scalars and lists
|
||||
// are replaced, an explicit null removes the key), and returns the
|
||||
// merged mapping together with the origin of each of its leaves. The
|
||||
// sources' maps are not modified.
|
||||
func Merge(sources ...Source) (map[string]any, Origins) {
|
||||
merged := map[string]any{}
|
||||
origins := Origins{}
|
||||
for _, s := range sources {
|
||||
mergeTracked(merged, s.Data, "", s.Name, origins)
|
||||
}
|
||||
return merged, origins
|
||||
}
|
||||
|
||||
// ReadFile reads path and parses it as a YAML mapping (an empty file is
|
||||
// an empty mapping). Unlike LoadMerged's layers, a missing file is an
|
||||
// error: the caller asked for this file.
|
||||
func ReadFile(path string) (map[string]any, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("config: read %s: %w", path, err)
|
||||
}
|
||||
var m map[string]any
|
||||
if err := yaml.Unmarshal(data, &m); err != nil {
|
||||
return nil, fmt.Errorf("config: parse %s: %w", path, err)
|
||||
}
|
||||
if m == nil {
|
||||
m = map[string]any{}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// mergeTracked overlays over onto base in place, recording the origin
|
||||
// of every leaf it sets in origins (nil: not tracked).
|
||||
func mergeTracked(base, over map[string]any, prefix, name string, origins Origins) {
|
||||
for k, v := range over {
|
||||
key := joinKey(prefix, k)
|
||||
if v == nil {
|
||||
delete(base, k)
|
||||
origins.drop(key)
|
||||
continue
|
||||
}
|
||||
if overMap, ok := v.(map[string]any); ok {
|
||||
baseMap, ok := base[k].(map[string]any)
|
||||
if !ok {
|
||||
origins.drop(key)
|
||||
baseMap = map[string]any{}
|
||||
base[k] = baseMap
|
||||
}
|
||||
mergeTracked(baseMap, overMap, key, name, origins)
|
||||
continue
|
||||
}
|
||||
origins.drop(key)
|
||||
base[k] = v
|
||||
if origins != nil {
|
||||
origins[key] = name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// drop forgets the origin of key and of everything below it.
|
||||
func (o Origins) drop(key string) {
|
||||
delete(o, key)
|
||||
prefix := key + "."
|
||||
for k := range o {
|
||||
if strings.HasPrefix(k, prefix) {
|
||||
delete(o, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func joinKey(prefix, k string) string {
|
||||
if prefix == "" {
|
||||
return k
|
||||
}
|
||||
return prefix + "." + k
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMergeTracksOrigins(t *testing.T) {
|
||||
low := Source{Name: "system", Data: map[string]any{
|
||||
"hostname": "mail.example.org",
|
||||
"tls": map[string]any{"cert-file": "a.crt", "key-file": "a.key"},
|
||||
"secrets": []any{"one", "two"},
|
||||
"limits": map[string]any{"max": 10},
|
||||
"drop": "me",
|
||||
}}
|
||||
high := Source{Name: "program", Data: map[string]any{
|
||||
"tls": map[string]any{"cert-file": "b.crt"},
|
||||
"secrets": []any{"three"},
|
||||
"limits": "flat",
|
||||
"drop": nil,
|
||||
}}
|
||||
merged, origins := Merge(low, high)
|
||||
|
||||
want := map[string]any{
|
||||
"hostname": "mail.example.org",
|
||||
"tls": map[string]any{"cert-file": "b.crt", "key-file": "a.key"},
|
||||
"secrets": []any{"three"},
|
||||
"limits": "flat",
|
||||
}
|
||||
if !reflect.DeepEqual(merged, want) {
|
||||
t.Errorf("merged = %#v\nwant %#v", merged, want)
|
||||
}
|
||||
wantOrigins := Origins{
|
||||
"hostname": "system",
|
||||
"tls.cert-file": "program",
|
||||
"tls.key-file": "system",
|
||||
"secrets": "program",
|
||||
"limits": "program",
|
||||
}
|
||||
if !reflect.DeepEqual(origins, wantOrigins) {
|
||||
t.Errorf("origins = %#v\nwant %#v", origins, wantOrigins)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeMapReplacesScalarAndBack(t *testing.T) {
|
||||
merged, origins := Merge(
|
||||
Source{Name: "a", Data: map[string]any{"x": "scalar"}},
|
||||
Source{Name: "b", Data: map[string]any{"x": map[string]any{"y": 1}}},
|
||||
)
|
||||
if !reflect.DeepEqual(merged, map[string]any{"x": map[string]any{"y": 1}}) {
|
||||
t.Errorf("merged = %#v", merged)
|
||||
}
|
||||
if !reflect.DeepEqual(origins, Origins{"x.y": "b"}) {
|
||||
t.Errorf("origins = %#v", origins)
|
||||
}
|
||||
|
||||
merged, origins = Merge(
|
||||
Source{Name: "a", Data: map[string]any{"x": map[string]any{"y": 1, "z": 2}}},
|
||||
Source{Name: "b", Data: map[string]any{"x": "scalar"}},
|
||||
)
|
||||
if !reflect.DeepEqual(merged, map[string]any{"x": "scalar"}) {
|
||||
t.Errorf("merged = %#v", merged)
|
||||
}
|
||||
if !reflect.DeepEqual(origins, Origins{"x": "b"}) {
|
||||
t.Errorf("origins = %#v", origins)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeNullRemovesSubtree(t *testing.T) {
|
||||
merged, origins := Merge(
|
||||
Source{Name: "a", Data: map[string]any{"x": map[string]any{"y": 1, "z": 2}, "w": 3}},
|
||||
Source{Name: "b", Data: map[string]any{"x": nil}},
|
||||
)
|
||||
if !reflect.DeepEqual(merged, map[string]any{"w": 3}) {
|
||||
t.Errorf("merged = %#v", merged)
|
||||
}
|
||||
if !reflect.DeepEqual(origins, Origins{"w": "a"}) {
|
||||
t.Errorf("origins = %#v", origins)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeDoesNotModifySources(t *testing.T) {
|
||||
inner := map[string]any{"y": 1}
|
||||
low := Source{Name: "a", Data: map[string]any{"x": inner}}
|
||||
Merge(low, Source{Name: "b", Data: map[string]any{"x": map[string]any{"z": 2}}})
|
||||
if !reflect.DeepEqual(inner, map[string]any{"y": 1}) {
|
||||
t.Errorf("source map modified: %#v", inner)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if _, err := ReadFile(filepath.Join(dir, "missing.yaml")); err == nil {
|
||||
t.Error("ReadFile of a missing file succeeded")
|
||||
}
|
||||
empty := filepath.Join(dir, "empty.yaml")
|
||||
if err := os.WriteFile(empty, nil, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m, err := ReadFile(empty)
|
||||
if err != nil || m == nil || len(m) != 0 {
|
||||
t.Errorf("ReadFile(empty) = %#v, %v; want empty map", m, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user