54 lines
2.1 KiB
Go
54 lines
2.1 KiB
Go
package configkit
|
|
|
|
import "go.maximumdirect.net/configkit/internal/engine"
|
|
|
|
// Difference describes one changed value or subtree between two successful loads.
|
|
// Left is the earlier value and Right is the later value. Snapshots are copies;
|
|
// Present distinguishes absence from explicit null. Redacted reports hiding
|
|
// based on sensitivity from either load. Mappings compare by key; a changed
|
|
// sequence is reported as a whole value. Records are ordered by canonical JSON
|
|
// pointer. HiddenChanged reports an actual hidden change, without identifying
|
|
// the hidden member. Redaction alone does not set it.
|
|
type Difference struct {
|
|
Path Path
|
|
Left ValueSnapshot
|
|
Right ValueSnapshot
|
|
HiddenChanged bool
|
|
}
|
|
|
|
// Diff compares the saved final values of two successful loads of the same
|
|
// configuration type, independently of later changes to Result.Config.
|
|
// It does not reread sources, environment variables,
|
|
// or rerun defaults or resolvers. Equal values produce no records even when
|
|
// their sources, defaults, or resolution histories differ. Credential contents
|
|
// are not compared: typed secret references compare by reference name only.
|
|
//
|
|
// Both results must be non-nil successful loads with inspection state; nil or
|
|
// zero results return an error and no records. Load errors are returned by
|
|
// Load, so callers must handle those before calling Diff. Snapshots are safe
|
|
// for presentation but are not a complete reloadable configuration.
|
|
func Diff[T any](left, right *Result[T]) ([]Difference, error) {
|
|
var leftInspection, rightInspection *engine.Inspection
|
|
if left != nil {
|
|
leftInspection = left.inspection
|
|
}
|
|
if right != nil {
|
|
rightInspection = right.inspection
|
|
}
|
|
|
|
values, err := engine.Diff(leftInspection, rightInspection)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
differences := make([]Difference, len(values))
|
|
for index, value := range values {
|
|
differences[index] = Difference{
|
|
Path: fromEnginePath(value.Path),
|
|
Left: fromEngineValueSnapshot(value.Left),
|
|
Right: fromEngineValueSnapshot(value.Right),
|
|
HiddenChanged: value.HiddenChanged,
|
|
}
|
|
}
|
|
return differences, nil
|
|
}
|