diff

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 8 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 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"

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

Jump to

Keyboard shortcuts

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