jd

package module
v2.0.0-...-8e742b1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SET      = setOption{}
	MULTISET = multisetOption{}
)
View Source
var (
	COLOR = colorOption{}
	MERGE = mergeOption{}
)

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

func ReadMergeFile(filename string) (Diff, error)

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

func ReadMergeString

func ReadMergeString(s string) (Diff, error)

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

func ReadPatchFile

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

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 ...Option) string

func (Diff) RenderMerge

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

func (Diff) RenderPatch

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

type DiffElement

type DiffElement struct {

	// Metadata describes how this DiffElement should be
	// interpretted. It is also inherited by following
	// DiffElements until another Metadata is encountered.
	Metadata Metadata

	// 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 Path

	// Before are the required context which should appear before
	// new and old values of a diff element. They are only used
	// for diffs in a list element.
	Before []JsonNode

	// Remove 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).
	Remove []JsonNode

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

	// After are the required context which should appear after
	// new and old values of a diff element. They are only used
	// for diffs in a list element.
	After []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 ...Option) string

type JsonNode

type JsonNode interface {

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

	// Yaml renders a JsonNode as a YAML string in block format.
	Yaml(renderOptions ...Option) 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, options ...Option) 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, options ...Option) 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 struct {
	Merge bool
}

func (Metadata) Options

func (m Metadata) Options() []Option

func (Metadata) Render

func (m Metadata) Render() string

type Option

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

func SetKeys

func SetKeys(keys ...string) Option

type Path

type Path []PathElement

func NewPath

func NewPath(n JsonNode) (Path, error)

func (Path) JsonNode

func (p Path) JsonNode() JsonNode

type PathElement

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

type PathIndex

type PathIndex int

type PathKey

type PathKey string

type PathMultiset

type PathMultiset struct{}

type PathMultisetKeys

type PathMultisetKeys map[string]JsonNode

type PathSet

type PathSet struct{}

type PathSetKeys

type PathSetKeys map[string]JsonNode

Jump to

Keyboard shortcuts

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