Documentation
¶
Index ¶
- type P
- func (p P) At(i int) interface{}
- func (p P) Components() []interface{}
- func (p P) Int(i ...int) P
- func (p P) Len() int
- func (p P) MarshalJSON() ([]byte, error)
- func (p P) Path(paths ...P) P
- func (p P) Str(s ...string) P
- func (p P) String() string
- func (p P) Sub(idxs ...int) P
- func (p P) ToJS() string
- func (p *P) UnmarshalJSON(b []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type P ¶
type P struct {
// contains filtered or unexported fields
}
P is a slice-based representation of a JSON path. Each slice element represents a single path component: either an object key (if a string) or an array index (if an integer).
Unlike JSONPath (https://goessner.net/articles/JsonPath/) and JSON pointers (https://datatracker.ietf.org/doc/html/rfc6901), paths are serialized as slices rather than strings. This helps avoid a class of errors around improperly (de-)escaping path components.
Unlike []interface{}, paths are type-safe via the Str/Int/Path methods.
The zero value is safe to use.
func FromJS ¶
FromJS parses a JSONPath-style path string and returns an error if it is unable to parse the string.
For example:
"foo[0].bar" -> ["foo", 0, "bar"]
func FromJSPartial ¶
FromJSPartial parses a JSONPath-style path string from the start greedily, and returns the index of how far it managed to parse.
For example:
"foo[0].bar asdf" -> ["foo", 0, "bar"], index for " asdf"
func (P) Components ¶
func (p P) Components() []interface{}
func (P) Int ¶
Int appends one or more ints onto the provided path.
It has the same semantics as the built-in append: the returned path may re-allocate the underlying array as needed.
func (P) MarshalJSON ¶
func (P) Path ¶
Path appends one or more paths onto the provided path.
It has the same semantics as the built-in append: the returned path may re-allocate the underlying array as needed.
func (P) Str ¶
Str appends one or more strings onto the provided path.
It has the same semantics as the built-in append: the returned path may re-allocate the underlying array as needed.
func (P) Sub ¶
Sub(start, end) returns a subpath from [start, end).
If not provided, end defaults to len(p). If not provided, start defaults to 0.
For example, ["foo", "bar"].Sub(1) would return ["bar"].
func (P) ToJS ¶
ToJS converts a path to a JSONPath-style string representation.
For example:
["foo", 0, "bar"] -> "foo[0].bar"
The produced value can be parsed by FromJS and will produce an identical path.