Documentation
¶
Index ¶
- Constants
- Variables
- func ArrayEach(data []byte, ...) error
- func Equal(a, b []byte) bool
- func GetBigInt(data []byte, keys ...string) (*big.Int, error)
- func GetBoolean(data []byte, keys ...string) (val bool, err error)
- func GetFloat(data []byte, keys ...string) (val float64, err error)
- func GetInt(data []byte, keys ...string) (val int64, err error)
- func GetNull(data []byte, keys ...string) error
- func GetString(data []byte, keys ...string) (val string, err error)
- func IsNull(b []byte) bool
- func ObjectEach(data []byte, callback func(key []byte, value []byte, dataType ValueType) error, ...) (err error)
- func ParseBigInt(b []byte) (res *big.Int, err error)
- func ParseBoolean(b []byte) (bool, error)
- func ParseFloat(b []byte) (float64, error)
- func ParseInt(b []byte) (int64, error)
- func ParseString(b []byte) (string, error)
- func Unescape(in, out []byte) ([]byte, error)
- type Index
- type Op
- type Patch
- type PatchOpts
- type Patcher
- func (p *Patcher) Add(path Pointer, value interface{})
- func (p *Patcher) Append(other Patch)
- func (p *Patcher) Copy(path, from Pointer)
- func (p *Patcher) Missing(path Pointer)
- func (p *Patcher) Move(path, from Pointer)
- func (p *Patcher) Op(op string, path, from Pointer, value interface{})
- func (p *Patcher) Patch() (Patch, error)
- func (p *Patcher) Remove(path Pointer)
- func (p *Patcher) Replace(path Pointer, value interface{})
- func (p *Patcher) Test(path Pointer, val interface{})
- type Pointer
- func (p Pointer) Append(frag string) Pointer
- func (p Pointer) Chop() (string, Pointer)
- func (p Pointer) Contains(q Pointer) bool
- func (p Pointer) Equal(other Pointer) bool
- func (p Pointer) MarshalJSON() ([]byte, error)
- func (p Pointer) Path() []string
- func (p Pointer) Shift() (string, Pointer)
- func (p Pointer) String() string
- func (p *Pointer) UnmarshalJSON(buf []byte) error
- func (p Pointer) Valid() bool
- type ValueType
Constants ¶
const ( NotExist = ValueType(iota) String Number Object Array Boolean Null Unknown = ValueType(255) )
const ContentType = "application/json-patch+json"
Variables ¶
var ( StopIteration = errors.New("stop iteration") KeyPathNotFoundError = errors.New("Key path not found") KeyPathFoundError = errors.New("Key path exists") UnknownValueTypeError = errors.New("Unknown value type") MalformedJsonError = errors.New("Malformed JSON error") MalformedStringError = errors.New("Value is string, but can't find closing '\"' symbol") MalformedArrayError = errors.New("Value is array, but can't find closing ']' symbol") MalformedObjectError = errors.New("Value looks like object, but can't find closing '}' symbol") MalformedValueError = errors.New("Value looks like Number/Boolean/None, but can't find its end: ',' or '}' symbol") OverflowIntegerError = errors.New("Value is number, but overflowed while parsing") MalformedNumberError = errors.New("Value is number, but starts '0' or '-0' or is an invalid float") MalformedStringEscapeError = errors.New("Encountered an invalid escape sequence in a string") NotStringError = errors.New("Value is not a string") NotFloatError = errors.New("Value is not a float") NotIntegerError = errors.New("Value is not an integer") NotBooleanError = errors.New("Value is not a boolean") NotNullError = errors.New("Value is not null") NotEqualError = errors.New("Values are not equal") BadMoveError = errors.New("Cannot move a value into itself") BadPatchOpError = errors.New("Invalid patch Op") NotArrayError = errors.New("Value is not an array") NotObjectError = errors.New("Value is not an object") )
Errors
var ( ErrMissingOp = errors.New("missing op") ErrMissingPath = errors.New("missing path") )
var IllegalPointerError = fmt.Errorf("illegal pointer")
Functions ¶
func ArrayEach ¶
func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error) error, keys ...string) error
ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
func Equal ¶
Equal tests the passed-in JSON blobs to see if they are equal. It first compares the byte arrays directly. If they are not equal, it will call MakeIndex on them and then see if the JSON documents are structurally equal.
func GetBigInt ¶ added in v0.9.5
GetBigInt returns the value retrieved by `Get`, cast to a *big.Int if possible. If key data type does not match, it will return an error. You shold use it if GetInt returns OverflowIntegerError.
func GetBoolean ¶
GetBoolean returns the value retrieved by `Get`, cast to a bool if possible. The offset is the same as in `Get`. If key data type do not match, it will return error.
func GetFloat ¶
GetFloat returns the value retrieved by `Get`, cast to a float64 if possible. The offset is the same as in `Get`. If key data type do not match, it will return an error.
func GetInt ¶
GetInt returns the value retrieved by `Get`, cast to an int64 if possible. If key data type does not match, it will return an error.
func GetString ¶
GetString returns the value retrieved by `Get`, cast to a string if possible, trying to properly handle escape and utf8 symbols If key data type do not match, it will return an error.
func ObjectEach ¶
func ObjectEach(data []byte, callback func(key []byte, value []byte, dataType ValueType) error, keys ...string) (err error)
ObjectEach iterates over the key-value pairs of a JSON object, invoking a given callback for each such entry
func ParseBigInt ¶ added in v0.9.5
ParseBigInt parses an integer of indefinite size and returns it. You should use it if ParseInt returns OverflowIntegerError, as that indicates that the value is out of the bounds of what an int64 can handle.
func ParseBoolean ¶
ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness)
func ParseFloat ¶
ParseNumber parses a Number ValueType into a Go float64
func ParseString ¶
ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string)
func Unescape ¶
Unescape unescapes the string contained in 'in' and returns it as a slice. If 'in' contains no escaped characters:
Returns 'in'.
Else, if 'out' is of sufficient capacity (guaranteed if cap(out) >= len(in)):
'out' is used to build the unescaped string and is returned with no extra allocation
Else:
A new slice is allocated and returned.
Types ¶
type Index ¶
type Index interface {
// WriteTo allows for low copy marshalling of an Index to something that
// wants the raw JSON. WriteTo always returns JSON in minified canonical form.
// The returned byte array will contain a single line of JSON with no line ending, and all
// object keys will be sorted according to their unescaped UTF-8 encoding.
WriteTo(w io.Writer) (int64, error)
// MarshalJSON does the same, except it returns just the raw bytes. It only returns
// an error if you run out of memory, more or less.
MarshalJSON() ([]byte, error)
// Type returns the type of the object the Index references.
Type() ValueType
// Equal returns whether this Index is equal to another Index
Equal(other Index) bool
// Get retrieves the sub-value at keys, if the Index can do that. If it cannot or there
// is no such sub-value, it returns nil and KeyPathNotFoundError. If keys is zero-length, it returns itself.
Get(keys ...string) (Index, error)
// Delete deletes the sub-value at keys. If it does not exist, it is an error.
Delete(keys ...string) error
// Replace replaces the existing value at keys to value. There must be a value at the target.
Replace(value []byte, keys ...string) error
// Add inserts the value at keys.
// If the target is an array, the values in the array will be shifted to accommodate the new object.
// If the target is an array, and the final key in the path is `-`, the value will be appended to the array.
Add(value []byte, keys ...string) error
// All returns an iterator over the Index, if the value is amenable to iteration. It does not iterate into sub-objects
// for arrays and objects.
All() iter.Seq2[string, Index]
}
Index abstracts out the structure of a JSON value and provides methods for searching and updating it without needing to reparse the underlying byte buffer over and over again.
func IndexValue ¶
IndexValue creates an Index for the first JSON value in buf. If IndexValue returns without an error, then idx points to the first position in buf after the JSON value that was parsed.
If IndexValue returns with an error, idx points to the start of the JSON value that caused the parse error.
func MakeIndex ¶
MakeIndex takes a buffer full of JSON, parses it, and returns an Index that can be used for fast access to arbitrary parts of the JSON, memory-efficient updates, and fast remarshalling. The returned object references the passed-in buf for all of its data until you start modifying things, and is not safe for concurrent access from multiple goroutines.
If there are any errors during json parsing (due to bad JSON, usually), an error will be returned. This includes trailing non-whitespace in the passed-in byte array.
type Op ¶ added in v0.9.3
type Op []byte
Op represents a valid JSON Patch operation as defined by RFC 6902
func (Op) From ¶ added in v0.9.3
From returns the "from" pointer from the Op. Not all operations require a From pointer, so this will return nil if it is missing.
func (Op) Op ¶ added in v0.9.3
Op returns the "op" field for this Op.
All Ops must have an operation, this method will panic if it is missing.
func (Op) Path ¶ added in v0.9.3
Path returns the JSON pointer for this Op.
All Ops must have a Path, this method will panic if it is missing. It assumes the pointer in the Op is well-formed.
func (Op) Valid ¶ added in v0.9.3
Valid returns true if the op has all required keys for the operation it encodes and the values are valid.
func (Op) Value ¶ added in v0.9.3
func (o Op) Value() json.RawMessage
Value returns the JSON encoded value for this Op, or nil if there is no value.
type Patch ¶
type Patch []byte
Patch is an array of individual JSON Patch operations. This patch supports the standard JSON patch operators, plus a `missing` operator that will error out if the JSON value contains anything at the specified `path`. This allows us to provide truly atomic and granular support for adding a value at a specified path.
func Generate ¶
Generate generates a JSON Patch that will modify base into target. If paranoid is true, then the generated patch with have test checks for changed item.
base and target must be byte arrays containing valid JSON
func GenerateFull ¶
Generate generates a JSON Patch that will modify base into target. If paranoid is true, then the generated patch with have test checks for changed item. If pretest is true, then the generated patch with have test ALL parts of the base.
base and target must be byte arrays containing valid JSON
func (Patch) Apply ¶
ApplyJSON does the same thing as Apply, except the inputs should be JSON-containing byte arrays instead of unmarshalled JSON
func (Patch) MarshalJSON ¶ added in v0.9.3
func (Patch) Ops ¶ added in v0.9.3
Ops returns the individual patch operations.
This is mostly present for debugging purposes.
func (*Patch) UnmarshalJSON ¶ added in v0.9.3
type PatchOpts ¶ added in v0.9.2
type PatchOpts struct {
// TestIndividualChanges generates a `test` op for every `remove` or
// `replace` op generated while testing differences in a json object.
// `add` does not get a `test` by default because we only generate them
// for objects on truly new fields, and testing the entire parent
// object beforehand conflicts with the intent to make just the changes
// we are protecting atomic. Truly granular testing would either need to
// change the semantics of `add` on an object to fail if th
TestIndividualChanges bool
// SupportMissingOp will remedy the above issue with JSONPatch by
// generating a non-standard `missing` op that passes if the path being
// tested does not exist.
SupportMissingOp bool
// TestFullBase skips all other tests and just includes a `test` Op
// against the full object at the beginning. Only use when you really
// want to guard the entire object instead of just the changes you want to make,
// as the resulting patch may be too strict.
TestFullBase bool
}
PatchOpts control how the patch will be generated.
type Patcher ¶ added in v0.9.3
type Patcher struct {
// contains filtered or unexported fields
}
Patcher is a helper object for generating JSON patches programatically without diffing two objects. An empty Patcher is ready for use.
func (*Patcher) Patch ¶ added in v0.9.3
Patch finalizes the JSON patch using the buffered Ops and returns it. If there was an error during patch generation, or the resulting patch was invalid, an error is returned indicating where along with the accumulated patch.
type Pointer ¶
type Pointer []byte
Pointer is a JSON Pointer that conforms to RFC 6901, plus extensions that allow for indexing json arrays starting from the end using negative numbers.
func NewPointer ¶
NewPointer takes a string that conforms to RFC6901 and turns it into a JSON pointer. It returns an error if the passed-in string does not describe a valid JSON pointer.
func Ptr ¶ added in v0.9.3
Ptr does the same as NewPointer, except it will panic if the pointer is invalid.
func PtrTo ¶ added in v0.9.3
PtrTo constructs a new Pointer out of the passed in Path fragments. It is the inverse operation to Pointer.Path()
func (Pointer) Chop ¶ added in v0.9.1
Chop extracts the last element in the pointer, returning it and the rest of the pointer.
func (Pointer) MarshalJSON ¶
MarshalJSON marshals a pointer into JSON.
func (Pointer) Path ¶
Path returns a string array that is suitable to use as a parameter to the `keys ...string` arguments that many other methods and functions in this package take.
func (Pointer) Shift ¶ added in v0.9.1
Shift extracts the first element in the pointer, returning it and the rest of the pointer.
func (*Pointer) UnmarshalJSON ¶
UnmarshalJSON unmarshals a pointer from JSON.
type ValueType ¶
type ValueType byte
Data types available in valid JSON data.
func Get ¶
Get - Receives data structure, and key path to extract value from.
Returns: `value` - Pointer to original data structure containing key value, or just empty slice if nothing found or error `dataType` - Can be: `NotExist`, `String`, `Number`, `Object`, `Array`, `Boolean` or `Null` `offset` - Offset from provided data structure where key value ends. Used mostly internally, for example for `ArrayEach` helper. `err` - If key not found or any other parsing issue it should return error. If key not found it also sets `dataType` to `NotExist`
Accept multiple keys to specify path to JSON value (in case of quering nested structures). If no keys provided it will try to extract closest JSON value (simple ones or object/array), useful for reading streams or arrays, see `ArrayEach` implementation.