ZDDC/zddc/internal/zddc/defaults.go
ZDDC 21f6883157 feat(zddc): embed default tree + assemble into cascade (migration phases 3-4)
Phase 3 — //go:embed all:defaults bakes the per-depth default tree into the
binary; EmbeddedPolicyTree() loads it (LoadPolicyTreeFromFS, generalized to any
fs.FS — embed, disk, or zip).

Phase 4 — PolicyTree.Assemble() folds the flat per-depth tree into the single
nested paths:-bearing ZddcFile the cascade walker already consumes, so the
walker is UNCHANGED. EmbeddedDefaults() now sources from the tree via Assemble()
instead of parsing defaults.zddc.yaml.

Proven behavior-preserving: TestEmbeddedTreeMatchesYAML asserts Assemble(tree)
deep-equals the legacy parsed defaults.zddc.yaml, and the Layer-2 matrix +
full suite stay green. defaults.zddc.yaml is kept only as that test's oracle
(deleted in phase 6). This same Assemble path is what an operator .zddc.zip
mounted at any level will use next (phase 5).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-05 11:22:59 -05:00

68 lines
2 KiB
Go

package zddc
import (
"embed"
"sync"
)
// defaultsBytes is the legacy single-file embedded baseline. Retained only so
// TestEmbeddedTreeMatchesYAML can prove the per-depth tree (the new source of
// truth) assembles to exactly the same ZddcFile. Removed once that guarantee
// is locked. (Still surfaced by EmbeddedDefaultsBytes / show-defaults for now.)
//
//go:embed defaults.zddc.yaml
var defaultsBytes []byte
// defaultsTreeFS is the embedded per-depth default policy tree — the source of
// truth. `all:` includes the `.zddc` (dot) files and `_any_` (underscore)
// directories that a bare //go:embed would skip.
//
//go:embed all:defaults
var defaultsTreeFS embed.FS
// EmbeddedDefaultsBytes returns the raw embedded defaults YAML. Surface: the
// show-defaults CLI dumps these to stdout.
func EmbeddedDefaultsBytes() []byte {
out := make([]byte, len(defaultsBytes))
copy(out, defaultsBytes)
return out
}
var (
embeddedTreeOnce sync.Once
embeddedTree PolicyTree
embeddedTreeErr error
)
// EmbeddedPolicyTree returns the baked-in per-depth default policy tree,
// memoised. This is the embedded form of the .zddc.zip mounted at the
// deployment root (the bottom of every cascade).
func EmbeddedPolicyTree() (PolicyTree, error) {
embeddedTreeOnce.Do(func() {
embeddedTree, embeddedTreeErr = LoadPolicyTreeFromFS(defaultsTreeFS, "defaults")
})
return embeddedTree, embeddedTreeErr
}
var (
embeddedDefaultsOnce sync.Once
embeddedDefaults ZddcFile
embeddedDefaultsErr error
)
// EmbeddedDefaults returns the embedded defaults assembled from the per-depth
// tree into the single nested ZddcFile the cascade walker consumes, memoised.
//
// The cascade walker (EffectivePolicy) consults this as the bottom-most level
// unless an on-disk .zddc up the chain sets `inherit: false`.
func EmbeddedDefaults() (ZddcFile, error) {
embeddedDefaultsOnce.Do(func() {
tree, err := EmbeddedPolicyTree()
if err != nil {
embeddedDefaultsErr = err
return
}
embeddedDefaults = tree.Assemble()
})
return embeddedDefaults, embeddedDefaultsErr
}