Compare focused inspection views
This commit is contained in:
6
diff.go
6
diff.go
@@ -40,6 +40,10 @@ func Diff[T any](left, right *Result[T]) ([]Difference, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fromEngineDifferences(values), nil
|
||||
}
|
||||
|
||||
func fromEngineDifferences(values []engine.Difference) []Difference {
|
||||
differences := make([]Difference, len(values))
|
||||
for index, value := range values {
|
||||
differences[index] = Difference{
|
||||
@@ -49,5 +53,5 @@ func Diff[T any](left, right *Result[T]) ([]Difference, error) {
|
||||
HiddenChanged: value.HiddenChanged,
|
||||
}
|
||||
}
|
||||
return differences, nil
|
||||
return differences
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Implementation plan: typed result values and focused inspection
|
||||
|
||||
Status: implementation in progress; Stages 1–4 complete.
|
||||
Status: implementation in progress; Stages 1–5 complete.
|
||||
|
||||
## Scope and working instructions
|
||||
|
||||
@@ -204,6 +204,8 @@ coverage rather than duplicating it comprehensively.
|
||||
|
||||
## Stage 5 — Compare focused views using the existing diff
|
||||
|
||||
**Complete.**
|
||||
|
||||
Implement `func (i Inspection) Diff(other Inspection) ([]Difference, error)`.
|
||||
Validate both initialized views and equality of normalized reporting locations
|
||||
before comparison. Errors return no records; do not add root-Go-type checks.
|
||||
|
||||
96
facade_inspection_diff_test.go
Normal file
96
facade_inspection_diff_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package configkit_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestInspectionViewDiff(t *testing.T) {
|
||||
type first struct {
|
||||
Component map[string]any `yaml:"component"`
|
||||
Sibling string `yaml:"sibling"`
|
||||
Items []string `yaml:"items"`
|
||||
}
|
||||
type second struct {
|
||||
Component map[string]any `yaml:"component"`
|
||||
Sibling string `yaml:"sibling"`
|
||||
Items []string `yaml:"items"`
|
||||
}
|
||||
left, err := configkit.Load[first](configkit.Bytes("left.yml", []byte("component: {name: before, gone: null}\nsibling: old\nitems: [a, b]\n")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
right, err := configkit.Load[second](configkit.Bytes("right.yml", []byte("component: {name: after, added: null}\nsibling: new\nitems: [a, c]\n")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lv, _ := left.InspectSubtree(configkit.Keys("component"))
|
||||
rv, _ := right.InspectSubtree(configkit.Keys("component"))
|
||||
diff, err := lv.Diff(rv)
|
||||
if err != nil || len(diff) != 3 || diff[0].Path.String() != "/component/added" || diff[1].Path.String() != "/component/gone" || diff[2].Path.String() != "/component/name" {
|
||||
t.Fatalf("component diff = %#v, %v", diff, err)
|
||||
}
|
||||
if diff[0].Left.Present || !diff[0].Right.Present || !diff[1].Left.Present || diff[1].Right.Present {
|
||||
t.Fatalf("null and absence = %#v", diff)
|
||||
}
|
||||
sequenceLeft, _ := left.InspectSubtree(configkit.Keys("items").Index(0))
|
||||
sequenceRight, _ := right.InspectSubtree(configkit.Keys("items", "0"))
|
||||
diff, err = sequenceLeft.Diff(sequenceRight)
|
||||
if err != nil || len(diff) != 1 || diff[0].Path.String() != "/items" {
|
||||
t.Fatalf("sequence ancestor = %#v, %v", diff, err)
|
||||
}
|
||||
missingLeft, _ := left.InspectSubtree(configkit.Keys("absent"))
|
||||
missingRight, _ := right.InspectSubtree(configkit.Keys("absent"))
|
||||
if diff, err = missingLeft.Diff(missingRight); err != nil || len(diff) != 0 {
|
||||
t.Fatalf("absent diff = %#v, %v", diff, err)
|
||||
}
|
||||
if diff, err = lv.Diff(missingRight); err == nil || diff != nil {
|
||||
t.Fatalf("mismatched diff = %#v, %v", diff, err)
|
||||
}
|
||||
if diff, err = (configkit.Inspection{}).Diff(lv); err == nil || diff != nil {
|
||||
t.Fatalf("zero diff = %#v, %v", diff, err)
|
||||
}
|
||||
rootLeft, _ := left.InspectSubtree(configkit.Path{})
|
||||
rootRight, _ := right.InspectSubtree(configkit.Path{})
|
||||
if diff, err = rootLeft.Diff(rootRight); err != nil || len(diff) != 5 {
|
||||
t.Fatalf("root diff = %#v, %v", diff, err)
|
||||
}
|
||||
if diff, err = lv.Diff(lv); err != nil || len(diff) != 0 {
|
||||
t.Fatalf("equal diff = %#v, %v", diff, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInspectionViewDiffRedactionAndShape(t *testing.T) {
|
||||
load := func(input string, sensitive bool) *configkit.Result[map[string]any] {
|
||||
t.Helper()
|
||||
result, err := configkit.Load(configkit.Bytes("view.yml", []byte(input)), configkit.Options[map[string]any]{Definition: configkit.Definition[map[string]any]{Sensitive: func(path configkit.Path) bool { return sensitive && path.String() == "/component" }}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
left := load("component: {token: disposable-canary-left}\n", true)
|
||||
right := load("component: {token: disposable-canary-right}\n", false)
|
||||
lv, _ := left.InspectSubtree(configkit.Keys("component", "token"))
|
||||
rv, _ := right.InspectSubtree(configkit.Keys("component", "token"))
|
||||
diff, err := lv.Diff(rv)
|
||||
if err != nil || len(diff) != 1 || !diff[0].Left.Redacted || !diff[0].Right.Redacted || !diff[0].HiddenChanged || strings.Contains(diff[0].Left.YAML+diff[0].Right.YAML, "disposable-canary") {
|
||||
t.Fatalf("sensitive diff = %#v, %v", diff, err)
|
||||
}
|
||||
removed := load("other: public\n", false)
|
||||
removedView, _ := removed.InspectSubtree(configkit.Keys("component", "token"))
|
||||
diff, err = lv.Diff(removedView)
|
||||
if err != nil || len(diff) != 1 || diff[0].Right.Present || !diff[0].Left.Redacted || strings.Contains(diff[0].Left.YAML, "disposable-canary") {
|
||||
t.Fatalf("sensitive removal = %#v, %v", diff, err)
|
||||
}
|
||||
left = load("component: {'0': old}\n", false)
|
||||
right = load("component: [new]\n", false)
|
||||
lv, _ = left.InspectSubtree(configkit.Keys("component", "0"))
|
||||
rv, _ = right.InspectSubtree(configkit.Keys("component").Index(0))
|
||||
diff, err = lv.Diff(rv)
|
||||
if err != nil || len(diff) != 1 || diff[0].Path.String() != "/component" {
|
||||
t.Fatalf("shape diff = %#v, %v", diff, err)
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,18 @@ func (i Inspection) HistoricalPaths() []Path {
|
||||
// writer errors can leave partial output.
|
||||
func (i Inspection) WriteEffective(output io.Writer) error { return i.view.WriteEffective(output) }
|
||||
|
||||
// Diff compares frozen views at the same canonical scope, including differences
|
||||
// at an ancestor when the existing comparison treats that ancestor atomically.
|
||||
// Views may come from different root Go types. Unavailable or mismatched views
|
||||
// return an error without records.
|
||||
func (i Inspection) Diff(other Inspection) ([]Difference, error) {
|
||||
values, err := i.view.Diff(other.view)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fromEngineDifferences(values), nil
|
||||
}
|
||||
|
||||
// Sources returns the full detached source catalog.
|
||||
func (i Inspection) Sources() []SourceInfo {
|
||||
values := i.view.Inspection().Sources()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
yamladapter "go.maximumdirect.net/configkit/internal/yaml"
|
||||
@@ -25,6 +26,23 @@ func NewInspectionView(inspection *Inspection, scope Path) (InspectionView, bool
|
||||
|
||||
func (v InspectionView) Inspection() *Inspection { return v.inspection }
|
||||
|
||||
func (v InspectionView) Diff(other InspectionView) ([]Difference, error) {
|
||||
if v.inspection == nil || other.inspection == nil || v.scope.String() != other.scope.String() {
|
||||
return nil, errors.New("configkit: diff inspection scope unavailable or mismatched")
|
||||
}
|
||||
values, err := Diff(v.inspection, other.inspection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result []Difference
|
||||
for _, value := range values {
|
||||
if pathContains(v.scope, value.Path) || pathContains(value.Path, v.scope) {
|
||||
result = append(result, value)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (v InspectionView) WriteEffective(output io.Writer) error {
|
||||
if v.inspection == nil || v.inspection.final == nil || output == nil {
|
||||
return errEffectiveUnavailable
|
||||
|
||||
Reference in New Issue
Block a user