Documentation
¶
Overview ¶
SYS-REQ-015, SYS-REQ-058, SYS-REQ-059, SYS-REQ-064: integer parsing internals
SYS-REQ-001, SYS-REQ-013, SYS-REQ-014: unsafe build-tag byte utilities
SYS-REQ-014, SYS-REQ-060, SYS-REQ-061, SYS-REQ-062, SYS-REQ-063: string escape and Unicode handling
Index ¶
- Constants
- Variables
- func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), ...) (offset int, err error)
- func ArrayEachErr(data []byte, ...) (count int, err error)
- func ArrayEachWildcard(data []byte, ...) (int, error)
- func Delete(data []byte, keys ...string) []byte
- func DeleteFound(data []byte, keys ...string) (result []byte, found bool)
- func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int
- func EachKeyErr(data []byte, cb func(idx int, value []byte, vt ValueType, err error) error, ...) error
- func EachKeyWildcard(data []byte, cb func(idx int, value []byte, vt ValueType, err error), ...) error
- func Escape(in string) []byte
- func FuzzDelete(data []byte) int
- func FuzzEachKey(data []byte) int
- func FuzzGetBoolean(data []byte) int
- func FuzzGetFloat(data []byte) int
- func FuzzGetInt(data []byte) int
- func FuzzGetString(data []byte) int
- func FuzzGetUnsafeString(data []byte) int
- func FuzzObjectEach(data []byte) int
- func FuzzParseBool(data []byte) int
- func FuzzParseFloat(data []byte) int
- func FuzzParseInt(data []byte) int
- func FuzzParseString(data []byte) int
- func FuzzSet(data []byte) int
- func FuzzTokenStart(data []byte) int
- func GetArrayLen(data []byte, keys ...string) (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 GetObjectLen(data []byte, keys ...string) (int, error)
- func GetString(data []byte, keys ...string) (val string, err error)
- func GetUint64(data []byte, keys ...string) (uint64, error)
- func GetUnsafeString(data []byte, keys ...string) (val string, err error)
- func ObjectEach(data []byte, ...) (err error)
- func ParseBoolean(b []byte) (bool, error)
- func ParseFloat(b []byte) (float64, error)
- func ParseInt(b []byte) (int64, error)
- func ParsePath(jsonPath string) ([]string, error)
- func ParseString(b []byte) (string, error)
- func Set(data []byte, setValue []byte, keys ...string) (value []byte, err error)
- func SetString(data []byte, val string, keys ...string) ([]byte, error)
- func SetWildcard(data []byte, setValue []byte, keys ...string) ([]byte, error)
- func StringToBytes(s string) []byte
- func Unescape(in, out []byte) ([]byte, error)
- func WriteToBuffer(buffer []byte, str string) int
- type CompiledPath
- func (c CompiledPath) ArrayEach(data []byte, cb func([]byte, ValueType, int, error)) (int, error)
- func (c CompiledPath) Delete(data []byte) []byte
- func (c CompiledPath) EachKey(data []byte, cb func(int, []byte, ValueType, error)) error
- func (c CompiledPath) Get(data []byte) ([]byte, ValueType, int, error)
- func (c CompiledPath) GetInt(data []byte) (int64, error)
- func (c CompiledPath) GetString(data []byte) (string, error)
- func (c CompiledPath) Parts() []string
- func (c CompiledPath) Set(data []byte, value []byte) ([]byte, error)
- type ValueType
Constants ¶
const ( NotExist = ValueType(iota) String Number Object Array Boolean Null Unknown )
Variables ¶
var ( KeyPathNotFoundError = errors.New("Key path not found") 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") MalformedStringEscapeError = errors.New("Encountered an invalid escape sequence in a string") NullValueError = errors.New("Value is null") )
Errors
Functions ¶
func ArrayEach ¶
func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (offset int, err error)
SYS-REQ-006, SYS-REQ-028, SYS-REQ-029, SYS-REQ-052, SYS-REQ-053, SYS-REQ-055, SYS-REQ-083 ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
func ArrayEachErr ¶ added in v1.4.0
func ArrayEachErr(data []byte, cb func(value []byte, dataType ValueType, offset int, err error) error, keys ...string) (count int, err error)
ArrayEachErr is used when iterating arrays and allows the callback to stop iteration by returning an error. io.EOF stops iteration without returning an error. The returned count includes the element whose callback stopped the iteration. SYS-REQ-004
func ArrayEachWildcard ¶ added in v1.4.0
func ArrayEachWildcard(data []byte, cb func(idx int, value []byte, vt ValueType, offset int, err error) error, keys ...string) (int, error)
ArrayEachWildcard iterates over an array addressed by keys. A terminal [*] component is accepted as an explicit request for all elements; without it, the function behaves like ArrayEachErr. SYS-REQ-113
func Delete ¶
Del - Receives existing data structure, path to delete.
Returns: `data` - return modified data
SYS-REQ-010, SYS-REQ-033, SYS-REQ-034, SYS-REQ-035, SYS-REQ-048, SYS-REQ-049, SYS-REQ-050, SYS-REQ-056
func DeleteFound ¶ added in v1.4.0
DeleteFound removes the value addressed by keys and reports whether the value was found and removed. When found is false, result is data unchanged. SYS-REQ-010
func EachKeyErr ¶ added in v1.4.0
func EachKeyErr(data []byte, cb func(idx int, value []byte, vt ValueType, err error) error, paths ...[]string) error
EachKeyErr finds the requested paths and allows the callback to stop iteration by returning an error. io.EOF stops iteration gracefully. SYS-REQ-008
func EachKeyWildcard ¶ added in v1.4.0
func EachKeyWildcard(data []byte, cb func(idx int, value []byte, vt ValueType, err error), path ...string) error
EachKeyWildcard resolves a path that may contain [*] components. A wildcard requires an array at that position and fans out over its elements before resolving the rest of the path. Multiple wildcards produce one callback for every matching combination, in document order.
idx is the zero-based order in which matches are reported.
SYS-REQ-113
func Escape ¶ added in v1.4.0
Escape returns in as a JSON string literal, including the surrounding quotation marks. SYS-REQ-014
func GetArrayLen ¶ added in v1.4.0
GetArrayLen returns the number of elements in the addressed JSON array. It scans the array without invoking a callback. SYS-REQ-112
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. SYS-REQ-005, SYS-REQ-079
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. SYS-REQ-004
func GetInt ¶
GetInt returns the value retrieved by `Get`, cast to a int64 if possible. If key data type do not match, it will return an error. SYS-REQ-003, SYS-REQ-075, SYS-REQ-076, SYS-REQ-077, SYS-REQ-078
func GetObjectLen ¶ added in v1.4.0
GetObjectLen returns the number of key-value pairs in the addressed JSON object. It scans the object without invoking a callback. SYS-REQ-112
func GetString ¶
SYS-REQ-002, SYS-REQ-071, SYS-REQ-072, SYS-REQ-073, SYS-REQ-074 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 GetUint64 ¶ added in v1.4.0
GetUint64 returns the value retrieved by internalGet, cast to a uint64 if possible. Negative values and malformed integers return MalformedValueError; values larger than uint64 return OverflowIntegerError. SYS-REQ-003
func GetUnsafeString ¶
SYS-REQ-011, SYS-REQ-080, SYS-REQ-081, SYS-REQ-082 GetUnsafeString returns the value retrieved by `Get`, use creates string without memory allocation by mapping string to slice memory. It does not handle escape symbols.
func ObjectEach ¶
func ObjectEach(data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string) (err error)
SYS-REQ-007, SYS-REQ-030, SYS-REQ-031, SYS-REQ-032, SYS-REQ-054, SYS-REQ-084 ObjectEach iterates over the key-value pairs of a JSON object, invoking a given callback for each such entry
func ParseBoolean ¶
ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness) SYS-REQ-012, SYS-REQ-036, SYS-REQ-057, SYS-REQ-066
func ParseFloat ¶
ParseNumber parses a Number ValueType into a Go float64 SYS-REQ-013, SYS-REQ-037, SYS-REQ-065
func ParseInt ¶
ParseInt parses a Number ValueType into a Go int64 SYS-REQ-015, SYS-REQ-039, SYS-REQ-040, SYS-REQ-058, SYS-REQ-059, SYS-REQ-064
func ParsePath ¶ added in v1.4.0
ParsePath converts a JSONPath-style path into the path components accepted by Get, Set, Delete, ArrayEach, and EachKey. SYS-REQ-114
func ParseString ¶
ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string) SYS-REQ-014, SYS-REQ-038, SYS-REQ-060, SYS-REQ-063, SYS-REQ-067
func Set ¶
Set - Receives existing data structure, path to set, and data to set at that key.
Returns: `value` - modified byte array `err` - On any parsing error
SYS-REQ-009, SYS-REQ-051, SYS-REQ-068, SYS-REQ-069, SYS-REQ-070, SYS-REQ-110
func SetString ¶ added in v1.4.0
SetString replaces the value at keys with val encoded as a JSON string. SYS-REQ-009
func SetWildcard ¶ added in v1.4.0
SetWildcard applies Set to every concrete path matched by [*]. Wildcards may appear at any array position and may be repeated. If a wildcard matches an empty array, data is returned unchanged.
SYS-REQ-113
func StringToBytes ¶
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 CompiledPath ¶ added in v1.4.0
type CompiledPath struct {
// contains filtered or unexported fields
}
CompiledPath stores a parsed path for repeated operations. SYS-REQ-114
func CompilePath ¶ added in v1.4.0
func CompilePath(jsonPath string) (CompiledPath, error)
CompilePath parses jsonPath once for reuse. SYS-REQ-114
func (CompiledPath) ArrayEach ¶ added in v1.4.0
ArrayEach iterates over the array at the compiled path. SYS-REQ-114
func (CompiledPath) Delete ¶ added in v1.4.0
func (c CompiledPath) Delete(data []byte) []byte
Delete removes the value at the compiled path. SYS-REQ-114
func (CompiledPath) EachKey ¶ added in v1.4.0
EachKey resolves the compiled path using EachKey. SYS-REQ-114
func (CompiledPath) Get ¶ added in v1.4.0
Get resolves the compiled path in data. Verifies: SYS-REQ-001 (Get) SYS-REQ-114
func (CompiledPath) GetInt ¶ added in v1.4.0
func (c CompiledPath) GetInt(data []byte) (int64, error)
GetInt resolves the compiled path and returns its integer value. SYS-REQ-114
func (CompiledPath) GetString ¶ added in v1.4.0
func (c CompiledPath) GetString(data []byte) (string, error)
GetString resolves the compiled path and returns its string value. SYS-REQ-114
func (CompiledPath) Parts ¶ added in v1.4.0
func (c CompiledPath) Parts() []string
Parts returns a copy of the compiled path components. SYS-REQ-114
type ValueType ¶
type ValueType int
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.
SYS-REQ-001, SYS-REQ-016, SYS-REQ-017, SYS-REQ-018, SYS-REQ-019, SYS-REQ-025, SYS-REQ-026, SYS-REQ-041, SYS-REQ-042, SYS-REQ-043