jd

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2024 License: MIT Imports: 12 Imported by: 25

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Diff

type Diff []DiffElement

Diff describes how two JsonNodes differ from each other. A Diff is composed of DiffElements (hunks) which describe a difference at a given Path. Each hunk stands alone with all necessary Metadata embedded in the Path, so a Diff rendered in native jd format can easily be edited by hand. The elements of a Diff can be applied to a JsonNode by the Patch method.

func ReadDiffFile

func ReadDiffFile(filename string) (Diff, error)

ReadDiffFile reads a file in native jd format.

func ReadDiffString

func ReadDiffString(s string) (Diff, error)

ReadDiffString reads a string in native jd format.

func ReadMergeFile added in v1.6.0

func ReadMergeFile(filename string) (Diff, error)

ReadMergeFile reads a JSON Merge Patch (RFC 7386) from a file.

func ReadMergeString added in v1.6.0

func ReadMergeString(s string) (Diff, error)

ReadMergeString reads a JSON Merge Patch (RFC 7386) from a string.

func ReadPatchFile added in v1.5.0

func ReadPatchFile(filename string) (Diff, error)

ReadPatchFile reads a JSON Patch (RFC 6902) from a file. It is subject to the same restrictions as ReadPatchString.

func ReadPatchString added in v1.5.0

func ReadPatchString(s string) (Diff, error)

ReadPatchString reads a JSON Patch (RFC 6902) from a string. ReadPatchString supports a subset of the specification and requires a sequence of "test", "remove", "add" operations which mimics the strict patching strategy of a native jd patch.

For example:

[
  {"op":"test","path":"/foo","value":"bar"},
  {"op":"remove","path":"/foo","value":"bar"},
  {"op":"add","path":"/foo","value":"baz"}
]

func (Diff) Render

func (d Diff) Render(opts ...RenderOption) string

func (Diff) RenderMerge added in v1.6.0

func (d Diff) RenderMerge() (string, error)

func (Diff) RenderPatch added in v1.5.0

func (d Diff) RenderPatch() (string, error)

type DiffElement

type DiffElement struct {

	// Path elements can be strings to index Objects, numbers to
	// index Lists and objects to index Sets and Multisets. Path
	// elements can be preceeded by an Array of Metadata strings to
	// change how the structure is interpretted. Metadata in lower
	// case applies to the following path element. Metadata in upper
	// case applies to the rest of the path.
	//
	// For example:
	//   ["foo","bar"]               // indexes to 1 in {"foo":{"bar":1}}
	//   ["foo",0]                   // indexes to 1 in {"foo":[1]}
	//   ["foo",{}]                  // indexes a set under "foo" in {"foo":[1]}
	//   ["foo",["multiset"],{}]     // indexes a multiset under "foo" in {"foo":[1,1]}
	//   ["foo",{"id":"bar"},"baz"]  // indexes to 1 in {"foo":[{"id":"bar","baz":1}]}
	//   [["MERGE"],"foo","bar"]     // indexes to 1 in {"foo":{"bar":1}} with merge semantics
	Path []JsonNode

	// OldValues are removed from the JsonNode at the Path. Usually
	// only one old value is provided unless removing entries from a
	// Set or Multiset. When using merge semantics no old values are
	// provided (new values stomp old ones).
	OldValues []JsonNode

	// NewValues are added to the JsonNode at the Path. Usually only
	// one new value is provided unless adding entries to a Set or
	// Multiset.
	NewValues []JsonNode
}

DiffElement (hunk) is a way in which two JsonNodes differ at a given Path. OldValues can be removed and NewValues can be added. The exact Path and how to interpret the intervening structure is determined by a list of JsonNodes (path elements).

func (DiffElement) Render

func (d DiffElement) Render(opts ...RenderOption) string

type JsonNode

type JsonNode interface {

	// Json renders a JsonNode as a JSON string.
	Json(renderOptions ...Metadata) string

	// Yaml renders a JsonNode as a YAML string in block format.
	Yaml(renderOptions ...Metadata) string

	// Equals returns true if the JsonNodes are equal according to
	// the provided Metadata. The default behavior (no Metadata) is
	// to compare the entire structure down to scalar values treating
	// Arrays as orders Lists. The SET and MULTISET Metadata will
	// treat Arrays as sets or multisets (bags) respectively. To deep
	// compare objects in an array irrespective of order, the SetKeys
	// function will construct Metadata to compare objects by a set
	// of keys. If two JsonNodes are equal, then Diff with the same
	// Metadata will produce an empty Diff. And vice versa.
	Equals(n JsonNode, metadata ...Metadata) bool

	// Diff produces a list of differences (Diff) between two
	// JsonNodes such that if the output Diff were applied to the
	// first JsonNode (Patch) then the two JsonNodes would be
	// Equal. The necessary Metadata is embeded in the Diff itself so
	// only the Diff is required to Patch a JsonNode.
	Diff(n JsonNode, metadata ...Metadata) Diff

	// Patch applies a Diff to a JsonNode. No Metadata is provided
	// because the original interpretation of the structure is
	// embedded in the Diff itself.
	Patch(d Diff) (JsonNode, error)
	// contains filtered or unexported methods
}

JsonNode is a JSON value, collection of values, or a void representing the absense of a value. JSON values can be a Number, String, Boolean or Null. Collections can be an Object, native JSON array, ordered List, unordered Set or Multiset. JsonNodes are created with the NewJsonNode function or ReadJson* and ReadYaml* functions.

func NewJsonNode

func NewJsonNode(n interface{}) (JsonNode, error)

NewJsonNode constructs a JsonNode from native Golang objects. See the function source for supported types and conversions. Slices are always placed into native JSON Arrays and interpretated as Lists, Sets or Multisets based on Metadata provided during Equals and Diff operations.

func ReadJsonFile

func ReadJsonFile(filename string) (JsonNode, error)

ReadJsonFile reads a file as JSON and constructs a JsonNode.

func ReadJsonString

func ReadJsonString(s string) (JsonNode, error)

ReadJsonString reads a string as JSON and constructs a JsonNode.

func ReadYamlFile

func ReadYamlFile(filename string) (JsonNode, error)

ReadYamlFile reads a file as YAML and constructs a JsonNode.

func ReadYamlString

func ReadYamlString(s string) (JsonNode, error)

ReadJsonString reads a string as YAML and constructs a JsonNode.

type Metadata

type Metadata interface {
	// contains filtered or unexported methods
}

Metadata is a closed set of values which modify Diff and Equals semantics.

var (
	// MULTISET interprets all Arrays as Multisets (bags) during Diff
	// and Equals operations.
	MULTISET Metadata = multisetMetadata{}
	// SET interprets all Arrays as Sets during Diff and Equals
	// operations.
	SET Metadata = setMetadata{}
	// MERGE produces a Diff with merge semantics (RFC 7386).
	MERGE Metadata = mergeMetadata{}
)

func SetPrecision added in v1.8.0

func SetPrecision(precision float64) Metadata

func Setkeys

func Setkeys(keys ...string) Metadata

SetKeys constructs Metadata to identify unique objects in an Array for deeper Diff and Patch operations.

type RenderOption added in v1.6.0

type RenderOption interface {

	// RenderOptions are included in the closed Metadata set for
	// backward compatability. Previously Json and Yaml methods
	// accepted Metadata instead of RenderOptions. Passing
	// Metadata to Json and Yaml methods did nothing but it remains
	// an option to not break any existing code.
	Metadata
	// contains filtered or unexported methods
}

RenderOption is a closed set of values which modify the output appearance of Render*, Json and Yaml methods.

var (
	COLOR RenderOption = colorRenderOption{}
)

Jump to

Keyboard shortcuts

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