Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildEnvelope ¶ added in v1.1.0
BuildEnvelope constructs the synthetic diff envelope as a Go value tree:
{"statusCode": N, "headers": {...}, "body": ...}
Type conventions (must match json.Unmarshal output for cmp.Equal compatibility):
- statusCode: float64 (not int) — json.Unmarshal decodes numbers as float64
- headers: map[string]any where values are []any{string, ...} — matches the shape produced by json.Marshal(http.Header) → json.Unmarshal. nil headers produce nil (matching json.Marshal(nil) → "null").
- body: as-is from json.Unmarshal (map[string]any, []any, string, float64, etc.) Mutable types (map/slice) are aliased, not copied. Callers must not mutate.
func FormatOps ¶
FormatOps returns a human-readable string representation of a list of patch operations. This is useful for logging and CLI output.
func SortArraysInTree ¶ added in v1.4.0
SortArraysInTree recursively sorts all []any slices in v using the same deterministic key function used by cmpopts.SortSlices. Maps and other values are traversed but not reordered. The value is sorted in-place and also returned for convenience.
Types ¶
type Differ ¶
Differ is an interface for comparing two values and returning a list of RFC 6902 JSON Patch operations describing the differences.
type DifferFunc ¶
DifferFunc is a function type that implements the Differ interface.
func (DifferFunc[T]) Diff ¶
func (f DifferFunc[T]) Diff(a, b T) ([]PatchOp, error)
Diff implements the Differ interface for DifferFunc.
type FieldNormalizer ¶
type FieldNormalizer struct {
// contains filtered or unexported fields
}
FieldNormalizer handles field filtering for JSON comparison. It always uses a hybrid approach: 1. If IncludedFields is set, extract only those fields (whitelist) 2. If IgnoredFields is set, remove those fields from the result (blacklist) 3. If both are set, include first, then exclude from included 4. If neither is set, return data unchanged
func NewFieldNormalizer ¶
func NewFieldNormalizer(includedFields, ignoredFields []string) *FieldNormalizer
NewFieldNormalizer creates a normalizer with the specified field filters.
func (*FieldNormalizer) NormalizeBytes ¶
func (fn *FieldNormalizer) NormalizeBytes(data []byte) ([]byte, error)
NormalizeBytes filters JSON fields using a hybrid approach: 1. Apply whitelist if includedFields is set (keep only these fields) 2. Apply blacklist if ignoredFields is set (remove these fields) This allows for flexible combinations like "include user object but exclude user.ssn"
func (*FieldNormalizer) NormalizeTree ¶ added in v1.1.0
func (fn *FieldNormalizer) NormalizeTree(tree jsontree.Tree) jsontree.Tree
NormalizeTree filters fields on an in-memory JSON tree (map[string]any / []any). Same semantics as NormalizeBytes but operates on Go values directly, avoiding gjson/sjson byte-level operations.
Whitelist: builds a new tree keeping only the included paths. Blacklist: deletes keys in-place from the tree.
The returned tree is always a fresh copy and safe to mutate by callers.
type Option ¶
type Option func(*options)
Option is a functional option for configuring JSON comparison.
func WithFloatTolerance ¶
WithFloatTolerance sets the acceptable difference for floating-point comparisons. Values closer than this tolerance are considered equal.
Example:
diff.JSON(a, b, diff.WithFloatTolerance(0.0001))
func WithIgnoredFields ¶
WithIgnoredFields sets a blacklist of fields to exclude from comparison. Uses gjson path syntax (e.g., "timestamp", "users.#.created_at"). Can be combined with WithIncludedFields for hybrid filtering.
Example:
diff.JSON(a, b, diff.WithIgnoredFields("timestamp", "request_id"))
func WithIncludedFields ¶
WithIncludedFields sets a whitelist of fields to include in comparison. Uses gjson path syntax (e.g., "name", "users.#.email"). Can be combined with WithIgnoredFields for hybrid filtering.
Example:
diff.JSON(a, b, diff.WithIncludedFields("name", "email"))
func WithSortArrays ¶ added in v1.4.0
WithSortArrays controls whether arrays are sorted before comparison. When true, arrays are sorted using a deterministic key function so that element ordering does not produce false-positive diffs. Default is false (positional comparison).
Example:
diff.JSON(a, b, diff.WithSortArrays(true))
type PatchOp ¶
type PatchOp struct {
Op string `json:"op"`
Path string `json:"path"`
Value any `json:"value,omitempty"`
}
PatchOp represents a single RFC 6902 JSON Patch operation.
func JSON ¶
JSON compares two JSON strings with configurable options and returns a list of RFC 6902 JSON Patch operations describing the differences.
The comparison process: 1. Normalize JSON (filter fields based on options) 2. Parse normalized JSON into Go values 3. Compare using go-cmp with a patch reporter 4. Return []PatchOp (empty slice if inputs are equal)
Example usage:
// No filtering (compare everything)
ops, err := diff.JSON(a, b)
// Ignore timestamp fields (blacklist)
ops, err := diff.JSON(a, b,
diff.WithIgnoredFields("timestamp", "users.#.created_at"),
)
// Only compare specific fields (whitelist - faster)
ops, err := diff.JSON(a, b,
diff.WithIncludedFields("name", "email"),
)
// Hybrid: include + exclude (with multiple options)
ops, err := diff.JSON(a, b,
diff.WithIncludedFields("user", "metadata"),
diff.WithIgnoredFields("user.ssn", "metadata.internal_id"),
)
func Parsed ¶ added in v1.1.0
Parsed compares two pre-parsed value trees and returns a list of RFC 6902 JSON Patch operations describing the differences.
This is the optimized entry point that skips JSON parsing/validation. Trees should be the shape produced by json.Unmarshal: map[string]any, []any, string, float64, bool, or nil.
Use diff.JSON() for raw JSON strings. Use diff.Parsed() when you already have in-memory Go values (e.g., from the redactor's BodyParsed).
Example usage:
liveEnvelope := diff.BuildEnvelope(200, liveHeaders, liveBodyParsed)
shadowEnvelope := diff.BuildEnvelope(200, shadowHeaders, shadowBodyParsed)
ops, err := diff.Parsed(liveEnvelope, shadowEnvelope,
diff.WithIgnoredFields("headers.Date"),
)