diff

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildEnvelope added in v1.1.0

func BuildEnvelope(statusCode int, headers http.Header, bodyParsed jsontree.Tree) map[string]any

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

func FormatOps(ops []PatchOp) string

FormatOps returns a human-readable string representation of a list of patch operations. This is useful for logging and CLI output.

Types

type Differ

type Differ[T any] interface {
	Diff(a, b T) ([]PatchOp, error)
}

Differ is an interface for comparing two values and returning a list of RFC 6902 JSON Patch operations describing the differences.

type DifferFunc

type DifferFunc[T any] func(a, b T) ([]PatchOp, error)

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

func WithFloatTolerance(tolerance float64) Option

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

func WithIgnoredFields(fields ...string) Option

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

func WithIncludedFields(fields ...string) Option

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"))

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

func JSON(a, b string, opts ...Option) ([]PatchOp, error)

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

func Parsed(a, b jsontree.Tree, opts ...Option) ([]PatchOp, error)

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"),
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL