Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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"
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"))
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"),
)