diff

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package diff implements "Diff" that compares two JSON objects and generates Deltas that describes differences between them. The package also provides "Patch" that apply Deltas to a JSON object.

Updated copy from https://github.com/yudai/gojsondiff.

Index

Constants

View Source
const (
	// ASCIISame represents the ASCII string " " used to indicate unchanged or identical elements in a comparison.
	ASCIISame = " "

	// ASCIIAdded represents the ASCII string "+" used to indicate newly added items or elements in comparisons.
	ASCIIAdded = "+"

	// ASCIIDeleted represents the ASCII string "-" used to indicate deleted items or removed elements.
	ASCIIDeleted = "-"
)

Variables

View Source
var ACSIIStyles = map[string]string{
	ASCIIAdded:   "30;42",
	ASCIIDeleted: "30;41",
}

ACSIIStyles is a map defining ANSI color styles for different ASCII markers used in formatting output.

Functions

This section is empty.

Types

type ASCIIFormatter

type ASCIIFormatter struct {
	// contains filtered or unexported fields
}

ASCIIFormatter is used to generate ASCII representations of differences between JSON-like data structures.

func NewASCIIFormatter

func NewASCIIFormatter(left interface{}, config ASCIIFormatterConfig) *ASCIIFormatter

NewASCIIFormatter creates a new ASCIIFormatter instance with the specified left data and configuration settings.

func (*ASCIIFormatter) Format

func (f *ASCIIFormatter) Format(diff Diff) (result string, err error)

Format formats the differences between two JSON objects into an ASCII representation using the provided Diff.

type ASCIIFormatterConfig

type ASCIIFormatterConfig struct {
	ShowArrayIndex bool
	Coloring       bool
}

ASCIIFormatterConfig specifies configuration options for formatting ASCII representations of data structures. ShowArrayIndex determines if array indices should be displayed in the formatted output. Coloring enables or disables colored output in the formatted result.

type ASCIILine

type ASCIILine struct {
	// contains filtered or unexported fields
}

ASCIILine represents a line in an ASCII-formatted output with a marker, indentation, and a buffer containing content.

type Added

type Added struct {

	// Values holds the added value
	Value interface{}
	// contains filtered or unexported fields
}

An Added represents a new added field of an object or an array.

func NewAdded

func NewAdded(position Position, value interface{}) *Added

NewAdded returns a new Added.

func (*Added) PostApply

func (d *Added) PostApply(object interface{}) interface{}

PostApply applies the added value to the given object at the position specified by the PostPosition method.

func (Added) PostPosition

func (i Added) PostPosition() Position

func (Added) Similarity

func (cache Added) Similarity() (similarity float64)

type Array

type Array struct {

	// Deltas holds internal Deltas
	Deltas []Delta
	// contains filtered or unexported fields
}

An Array is a Delta that represents an array of JSON.

func NewArray

func NewArray(position Position, deltas []Delta) *Array

NewArray returns an Array.

func (*Array) PostApply

func (d *Array) PostApply(object interface{}) interface{}

PostApply applies the stored deltas to the provided object based on their positions, modifying and returning the object.

func (Array) PostPosition

func (i Array) PostPosition() Position

func (Array) Similarity

func (cache Array) Similarity() (similarity float64)

type Deleted

type Deleted struct {

	// The value deleted
	Value interface{}
	// contains filtered or unexported fields
}

Deleted represents a change where an element is removed from a map or slice at a specific position. It embeds preDelta to store positional metadata and includes the Value field to reference the deleted element.

func NewDeleted

func NewDeleted(position Position, value interface{}) *Deleted

NewDeleted returns a Deleted.

func (Deleted) PreApply

func (d Deleted) PreApply(object interface{}) interface{}

PreApply removes an element from a map or slice based on the position specified in the Deleted instance.

func (Deleted) PrePosition

func (i Deleted) PrePosition() Position

func (Deleted) Similarity

func (d Deleted) Similarity() (similarity float64)

Similarity calculates and returns the similarity for the Deleted delta type as a floating-point value.

type Delta

type Delta interface {
	// Similarity calculates the similarity of the Delta values.
	// The return value is normalized from 0 to 1,
	// 0 is completely different and 1 is they are same
	Similarity() (similarity float64)
}

A Delta represents an atomic difference between two JSON objects.

type Diff

type Diff interface {
	// Deltas returns Deltas that describe differences between two JSON objects
	Deltas() []Delta
	// Modified returns true if Diff has at least one Delta.
	Modified() bool
}

A Diff holds deltas generated by a Differ.

type Differ

type Differ struct {
	// contains filtered or unexported fields
}

A Differ compares JSON objects and apply patches.

func New

func New() *Differ

New returns new Differ with default configuration.

func (*Differ) ApplyPatch

func (differ *Differ) ApplyPatch(json map[string]interface{}, patch Diff)

ApplyPatch applies a Diff to an JSON object. This method is destructive.

func (*Differ) Compare

func (differ *Differ) Compare(
	left []byte,
	right []byte,
) (Diff, error)

Compare compares two JSON strings as []bytes and return a Diff object.

func (*Differ) CompareArrays

func (differ *Differ) CompareArrays(
	left []interface{},
	right []interface{},
) Diff

CompareArrays compares two JSON arrays as []interface{} and return a Diff object.

func (*Differ) CompareObjects

func (differ *Differ) CompareObjects(
	left map[string]interface{},
	right map[string]interface{},
) Diff

CompareObjects compares two JSON object as map[string]interface{} and return a Diff object.

type Index

type Index int

Index is a Position with an int value, which means the Delta is in an Array.

func (Index) CompareTo

func (i Index) CompareTo(another Position) bool

CompareTo compares the current Index with another Position and returns true if the current Index is smaller.

func (Index) String

func (i Index) String() (name string)

String converts the Index value to its string representation and returns it.

type Modified

type Modified struct {

	// The value before modification
	OldValue interface{}

	// The value after modification
	NewValue interface{}
	// contains filtered or unexported fields
}

A Modified represents a field whose value is changed.

func NewModified

func NewModified(position Position, oldValue, newValue interface{}) *Modified

NewModified returns a Modified.

func (*Modified) PostApply

func (d *Modified) PostApply(object interface{}) interface{}

PostApply updates a map or slice at a specific position with a new value and returns the modified object.

func (Modified) PostPosition

func (i Modified) PostPosition() Position

func (Modified) Similarity

func (cache Modified) Similarity() (similarity float64)

type Moved

type Moved struct {

	// The value before moving
	Value interface{}
	// The delta applied after moving (for compatibility)
	Delta interface{}
	// contains filtered or unexported fields
}

A Moved represents field that is moved, which means the index or name is changed. Note that, in this library, assigning a Moved and a Modified to a single position is not allowed. For the compatibility with jsondiffpatch, the Moved in this library can hold the old and new value in it.

func NewMoved

func NewMoved(oldPosition Position, newPosition Position, value interface{}, delta Delta) *Moved

NewMoved creates and returns a new Moved instance representing a field that has been relocated with old and new positions.

func (*Moved) PostApply

func (d *Moved) PostApply(object interface{}) interface{}

PostApply applies the stored delta after a move operation and adjusts the position of a value in the object.

func (Moved) PostPosition

func (i Moved) PostPosition() Position

func (*Moved) PreApply

func (d *Moved) PreApply(object interface{}) interface{}

PreApply modifies the given object by removing the element at the pre-move index and storing its value in the Moved instance.

func (Moved) PrePosition

func (i Moved) PrePosition() Position

func (Moved) Similarity

func (cache Moved) Similarity() (similarity float64)

type Name

type Name string

A Name is a Postition with a string, which means the delta is in an object.

func (Name) CompareTo

func (n Name) CompareTo(another Position) bool

CompareTo returns true if the Name is lexicographically less than the given Position, which must be of type Name.

func (Name) String

func (n Name) String() (name string)

String returns the string representation of the Name.

type Object

type Object struct {

	// Deltas holds internal Deltas
	Deltas []Delta
	// contains filtered or unexported fields
}

An Object is a Delta that represents an object of JSON.

func NewObject

func NewObject(position Position, deltas []Delta) *Object

NewObject returns an Object.

func (*Object) PostApply

func (d *Object) PostApply(object interface{}) interface{}

PostApply processes the given object by applying deltas at positions determined by the object's type (map or slice).

func (Object) PostPosition

func (i Object) PostPosition() Position

func (Object) Similarity

func (cache Object) Similarity() (similarity float64)

type Position

type Position interface {
	// String returns the position as a string
	String() (name string)

	// CompareTo returns a true if the Position is smaller than another Position.
	// This function is used to sort Positions by the sort package.
	CompareTo(another Position) bool
}

A Position represents the position of a Delta in an object or an array.

type PostDelta

type PostDelta interface {
	// PostPosition returns the Position.
	PostPosition() Position

	// PostApply applies the delta to object.
	PostApply(object interface{}) interface{}
}

PostDelta represents an interface for deltas applied post object modification. It requires methods to retrieve the position and apply the delta to an object.

type PreDelta

type PreDelta interface {
	// PrePosition returns the Position.
	PrePosition() Position

	// PreApply applies the delta to object.
	PreApply(object interface{}) interface{}
}

A PreDelta is a Delta that has a position of the left side JSON object. Deltas implements this interface should be applies before PostDeltas.

type TextDiff

type TextDiff struct {
	Modified

	// Diff string
	Diff []dmp.Patch
}

A TextDiff represents a Modified with TextDiff between the old and the new values.

func NewTextDiff

func NewTextDiff(position Position, diff []dmp.Patch, oldValue, newValue interface{}) *TextDiff

NewTextDiff creates a new TextDiff instance with the provided position, diff, oldValue, and newValue.

func (*TextDiff) DiffString

func (d *TextDiff) DiffString() string

DiffString returns the textual representation of the diff stored in the TextDiff instance.

func (*TextDiff) PostApply

func (d *TextDiff) PostApply(object interface{}) interface{}

PostApply updates the provided object with the changes specified in the TextDiff and returns the modified object.

func (TextDiff) PostPosition

func (i TextDiff) PostPosition() Position

func (TextDiff) Similarity

func (cache TextDiff) Similarity() (similarity float64)

Jump to

Keyboard shortcuts

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