jsondiff

package module
Version: v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2022 License: MIT Imports: 9 Imported by: 0

README

status PkgGoDev

jsondiff

jsnodiff provides functions to calculate JSON objects differences with gojq filter.

Synopsis

See examples on pkg.go.dev.

Installation

go get github.com/aereal/jsondiff

CLI

go install github.com/aereal/jsondiff/cmd/jsondiff@latest
jsondiff -only '.d' ./testdata/from.json ./testdata/to.json
# --- from.json
# +++ to.json
# @@ -1,2 +1,2 @@
# -4
# +3

See also

  • gojq
    • Thanks to this library, it is able to filter JSON structures

License

See LICENSE file.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrEitherOnlyOneOption = errors.New("either of only one of Ignore() or Only() must be specified")

Functions

func Diff

func Diff(from, to *Input, opts ...Option) (string, error)

Diff calculates differences with inputs.

func DiffFromFiles added in v0.1.0

func DiffFromFiles(from, to fs.File, opts ...Option) (string, error)

DiffFromFiles calculates differences with flies' contents.

Example
query, err := gojq.Parse(".d")
if err != nil {
	panic(err)
}
from, err := os.Open("./testdata/from.json")
if err != nil {
	panic(err)
}
to, err := os.Open("./testdata/to.json")
if err != nil {
	panic(err)
}
diff, err := DiffFromFiles(from, to, Only(query))
if err != nil {
	panic(err)
}
fmt.Print(diff)
Output:

--- from.json
+++ to.json
@@ -1,2 +1,2 @@
-4
+3

func DiffFromObjects added in v0.1.0

func DiffFromObjects(from, to interface{}, opts ...Option) (string, error)

DiffFromObjects calculates differences with from and to.

Example (Ignore)
query, err := gojq.Parse(".b, .c")
if err != nil {
	panic(err)
}
diff, err := DiffFromObjects(lhs, rhs, Ignore(query))
if err != nil {
	panic(err)
}
fmt.Println(diff)
Output:

--- from
+++ to
@@ -2,6 +2,6 @@
   "a": 1,
   "b": null,
   "c": null,
-  "d": 4
+  "d": 3
 }
Example (Only)
query, err := gojq.Parse(".d")
if err != nil {
	panic(err)
}
diff, err := DiffFromObjects(lhs, rhs, Only(query))
if err != nil {
	panic(err)
}
fmt.Println(diff)
Output:

--- from
+++ to
@@ -1,2 +1,2 @@
-4
+3

Types

type Input added in v0.2.0

type Input struct {
	// Name is Input's name.
	//
	// It'll be used as patch's file name.
	Name string

	// X is an object decoded from JSON.
	X interface{}
}

Input represents a pair of the object that decoded from JSON and its name.

func NewInputFromFile added in v0.2.0

func NewInputFromFile(f fs.File) (*Input, error)

NewInputFromFile returns a new Input from file's name and contents.

type Option

type Option func(*opt)

Option is a function to modify Diff's behavior.

func Ignore

func Ignore(query *gojq.Query) Option

Ignore returns Option function that indicates the function to ignore structures pointed by the query.

func Only added in v0.1.0

func Only(query *gojq.Query) Option

Only returns Option function that indicates the function to calculate differences based on the structure pointed by the query.

Source Files

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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