Categorygithub.com/ldclabs/cbor-patch
modulepackage
1.2.0-beta2
Repository: https://github.com/ldclabs/cbor-patch.git
Documentation: pkg.go.dev

# README

CBOR-Patch

CI Codecov CodeQL License Go Reference

cborpatch is a library which provides functionality for applying RFC6902 JSON patches on CBOR

cborpatch supports positive integer, negative integer, byte string and UTF-8 text string as map key.

Import

// package cborpatch
import "github.com/ldclabs/cbor-patch"

Examples

Create and apply a CBOR Patch

package main

import (
	"fmt"

	cborpatch "github.com/ldclabs/cbor-patch"
)

func main() {
	original := cborpatch.MustFromJSON(`{"name": "John", "age": 24, "height": 3.21}`)
	fmt.Printf("%x\n", original)
	// a3636167651818646e616d65644a6f686e66686569676874fb4009ae147ae147ae

	patch, err := cborpatch.PatchFromJSON(`[
		{"op": "replace", "path": "/name", "value": "Jane"},
		{"op": "remove", "path": "/height"}
	]`)
	if err != nil {
		panic(err)
	}
	modified, err := patch.Apply(original)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%x\n", modified)
	// a2636167651818646e616d65644a616e65
	fmt.Printf("%s\n", cborpatch.MustToJSON(modified))
	// {"age":24,"name":"Jane"}
}

Create a Node and apply more Patchs

package main

import (
	"fmt"

	cborpatch "github.com/ldclabs/cbor-patch"
)

func main() {
	original := cborpatch.MustFromJSON(`{"name": "John", "age": 24, "height": 3.21}`)
	fmt.Printf("%x\n", original)
	// a3636167651818646e616d65644a6f686e66686569676874fb4009ae147ae147ae

	node := cborpatch.NewNode(original)
	patch, err := cborpatch.PatchFromJSON(`[
		{"op": "replace", "path": "/name", "value": "Jane"},
		{"op": "remove", "path": "/height"}
	]`)
	if err != nil {
		panic(err)
	}
	err = node.Patch(patch, nil)
	if err != nil {
		panic(err)
	}
	modified, err := node.MarshalCBOR()
	if err != nil {
		panic(err)
	}
	fmt.Printf("%x\n", modified)
	// a2636167651818646e616d65644a616e65
	modified, err = node.MarshalJSON()
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", string(modified))
	// {"age":24,"name":"Jane"}

	patch, err = cborpatch.PatchFromJSON(`[
		{"op": "replace", "path": "/age", "value": 25}
	]`)
	if err != nil {
		panic(err)
	}
	err = node.Patch(patch, nil)
	if err != nil {
		panic(err)
	}
	modified, err = node.MarshalCBOR()
	if err != nil {
		panic(err)
	}
	fmt.Printf("%x\n", modified)
	// a2636167651819646e616d65644a616e65
	modified, err = node.MarshalJSON()
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", string(modified))
	// {"age":25,"name":"Jane"}
}

Get value by path

package main

import (
	"fmt"

	cborpatch "github.com/ldclabs/cbor-patch"
)

func main() {
	doc := cborpatch.MustFromJSON(`{
		"baz": "qux",
		"foo": [ "a", 2, "c" ]
	}`)
	node := cborpatch.NewNode(doc)
	path, err := cborpatch.PathFromJSON("/foo/0")
	if err != nil {
		panic(err)
	}

	value, err := node.GetValue(path, nil)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%x\n", value)
	// 6161
	fmt.Printf("%s\n", cborpatch.MustToJSON(value))
	// "a"
}

Find children by test operations

package main

import (
	"fmt"

	cborpatch "github.com/ldclabs/cbor-patch"
)

func main() {
	doc := cborpatch.MustFromJSON(`["root", ["p",
		["span", {"data-type": "text"},
			["span", {"data-type": "leaf"}, "Hello 1"],
			["span", {"data-type": "leaf"}, "Hello 2"],
			["span", {"data-type": "leaf"}, "Hello 3"],
			["span", {"data-type": null}, "Hello 4"]
		]
	]]`)

	node := cborpatch.NewNode(doc)
	tests := cborpatch.PVs{
		{cborpatch.PathMustFromJSON("/0"), cborpatch.MustFromJSON(`"span"`)},
		{cborpatch.PathMustFromJSON("/1/data-type"), cborpatch.MustFromJSON(`"leaf"`)},
	}

	result, err := node.FindChildren(tests, nil)
	if err != nil {
		panic(err)
	}
	for _, r := range result {
		fmt.Printf("Path: %s, Value: %x, JSON: %s\n", r.Path, r.Value, cborpatch.MustToJSON(r.Value))
	}

	// Output:
	// Path: [1, 1, 2], Value: 83647370616ea169646174612d74797065646c6561666748656c6c6f2031, JSON: ["span",{"data-type":"leaf"},"Hello 1"]
	// Path: [1, 1, 3], Value: 83647370616ea169646174612d74797065646c6561666748656c6c6f2032, JSON: ["span",{"data-type":"leaf"},"Hello 2"]
	// Path: [1, 1, 4], Value: 83647370616ea169646174612d74797065646c6561666748656c6c6f2033, JSON: ["span",{"data-type":"leaf"},"Hello 3"]
}

# Functions

Diagify returns the doc as CBOR diagnostic notation.
Equal indicates if 2 CBOR documents have the same structural equality.
FromJSON converts a JSON-encoded data to a CBOR-encoded data with a optional value as struct container.
GetValueByPath returns the value of a given path in a raw encoded CBOR document.
MustFromJSON converts a JSON-encoded string to a CBOR-encoded data.
No description provided by the author
MustToJSON converts a CBOR-encoded data to a JSON-encoded string.
NewAccumulatedCopySizeError returns an AccumulatedCopySizeError.
NewNode returns a new Node with the given raw encoded CBOR document.
NewOptions creates a default set of options for calls to ApplyWithOptions.
NewPatch decodes the passed CBOR document as an RFC 6902 patch.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ReadCBORType returns the type of a raw encoded CBOR value.
SetCBOR set the underlying global CBOR Marshal and Unmarshal functions.
ToJSON converts a CBOR-encoded data to a JSON-encoded data with a optional value as struct container.

# Constants

Predefined CBORTypes.
Predefined CBORTypes.
Predefined CBORTypes.
Predefined CBORTypes.
Predefined CBORTypes.
Predefined CBORTypes.
Predefined CBORTypes.
Predefined CBORTypes.
Predefined CBORTypes.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

AccumulatedCopySizeLimit limits the total size increase in bytes caused by "copy" operations in a patch.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SupportNegativeIndices decides whether to support non-standard practice of allowing negative indices to mean indices starting at the end of an array.

# Structs

AccumulatedCopySizeError is an error type returned when the accumulated size increase caused by copy operations in a patch operation has exceeded the limit.
Node represents a lazy parsing CBOR document.
Operation is a single CBOR-Patch step, such as a single 'add' operation.
Options specifies options for calls to ApplyWithOptions.
PV represents a node with a path and a raw encoded CBOR value.

# Type aliases

No description provided by the author
CBORType is the type of a raw encoded CBOR value.
No description provided by the author
Patch is an ordered collection of Operations.
No description provided by the author
PVs represents a list of PV.
rawKey is a raw encoded CBOR value for map key.
RawMessage is a raw encoded CBOR value.