Documentation
¶
Overview ¶
Package tree provides a simple structure for dealing with dynamic or unknown JSON/YAML structures.
Index ¶
- Variables
- func Edit(pn *Node, expr string) error
- func MarshalJSON(n Node) ([]byte, error)
- func MarshalYAML(n Node) ([]byte, error)
- func OutputColorJSON(out io.Writer, n Node) error
- func OutputColorYAML(out io.Writer, n Node) error
- func RegisterNewMethodQueryFunc(method string, fn NewMethodQueryFunc)
- func ToAny(n Node) any
- func UnmarshalViaJSON(n Node, v any) error
- func UnmarshalViaYAML(n Node, v any) error
- func Walk(n Node, fn WalkFunc) error
- type And
- type Any
- func (n Any) Array() Array
- func (n Any) Each(cb func(key any, n Node) error) error
- func (n Any) Find(expr string) ([]Node, error)
- func (n Any) Get(keys ...any) Node
- func (n Any) Has(keys ...any) bool
- func (n Any) IsNil() bool
- func (n Any) Map() Map
- func (n Any) Type() Type
- func (n *Any) UnmarshalJSON(data []byte) error
- func (n Any) Value() Value
- type Array
- func (n *Array) Append(v Node) error
- func (n Array) Array() Array
- func (n *Array) Delete(key any) error
- func (n Array) Each(cb func(key any, n Node) error) error
- func (n Array) Find(expr string) ([]Node, error)
- func (n Array) Get(keys ...any) Node
- func (n Array) Has(keys ...any) bool
- func (n Array) IsNil() bool
- func (n Array) Map() Map
- func (n *Array) Set(key any, v Node) error
- func (n Array) Type() Type
- func (n *Array) UnmarshalJSON(data []byte) error
- func (n *Array) UnmarshalYAML(value *yaml.Node) error
- func (n Array) Value() Value
- type ArrayQuery
- type ArrayRangeQuery
- type BoolValue
- func (n BoolValue) Array() Array
- func (n BoolValue) Bool() bool
- func (n BoolValue) Compare(op Operator, v Value) bool
- func (n BoolValue) Each(cb func(key any, n Node) error) error
- func (n BoolValue) Find(expr string) ([]Node, error)
- func (n BoolValue) Float64() float64
- func (n BoolValue) Get(keys ...any) Node
- func (n BoolValue) Has(keys ...any) bool
- func (n BoolValue) Int() int
- func (n BoolValue) Int64() int64
- func (n BoolValue) IsNil() bool
- func (n BoolValue) Map() Map
- func (n BoolValue) String() string
- func (n BoolValue) Type() Type
- func (n BoolValue) Value() Value
- type ColorEncoder
- type Comparator
- type ContainsQuery
- type CountQuery
- type EditorNode
- type EditorQuery
- type EmptyQuery
- type Evaluator
- type FilterQuery
- type FirstQuery
- type HasQuery
- type KeysQuery
- type LastQuery
- type Map
- func (n Map) Append(v Node) error
- func (n Map) Array() Array
- func (n Map) Delete(key any) error
- func (n Map) Each(cb func(key any, n Node) error) error
- func (n Map) Find(expr string) ([]Node, error)
- func (n Map) Get(keys ...any) Node
- func (n Map) Has(keys ...any) bool
- func (n Map) IsNil() bool
- func (n Map) Keys() []string
- func (n Map) Map() Map
- func (n Map) Set(key any, v Node) error
- func (n Map) Type() Type
- func (n *Map) UnmarshalJSON(data []byte) error
- func (n *Map) UnmarshalYAML(value *yaml.Node) error
- func (n Map) Value() Value
- func (n Map) Values() []Node
- type MapQuery
- type MergeOption
- type NewMethodQueryFunc
- type NilValue
- func (n NilValue) Array() Array
- func (n NilValue) Bool() bool
- func (n NilValue) Compare(op Operator, v Value) bool
- func (n NilValue) Each(cb func(key any, n Node) error) error
- func (n NilValue) Find(expr string) ([]Node, error)
- func (n NilValue) Float64() float64
- func (n NilValue) Get(keys ...any) Node
- func (n NilValue) Has(keys ...any) bool
- func (n NilValue) Int() int
- func (n NilValue) Int64() int64
- func (n NilValue) IsNil() bool
- func (n NilValue) Map() Map
- func (n NilValue) MarshalJSON() ([]byte, error)
- func (n NilValue) MarshalYAML() (any, error)
- func (n NilValue) String() string
- func (n NilValue) Type() Type
- func (n NilValue) Value() Value
- type Node
- func Clone(n Node) Node
- func CloneDeep(n Node) Node
- func DecodeJSON(dec *json.Decoder) (Node, error)
- func DecodeYAML(dec *yaml.Decoder) (Node, error)
- func Find(n Node, expr string) ([]Node, error)
- func MarshalViaJSON(v any) (Node, error)
- func MarshalViaYAML(v any) (Node, error)
- func Merge(a, b Node, opts MergeOption) Node
- func ToNode(v any) Node
- func ToNodeValues(vs ...any) []Node
- func ToValue(v any) Node
- func UnmarshalJSON(data []byte) (Node, error)
- func UnmarshalYAML(data []byte) (Node, error)
- type NopQuery
- type NumberValue
- func (n NumberValue) Array() Array
- func (n NumberValue) Bool() bool
- func (n NumberValue) Compare(op Operator, v Value) bool
- func (n NumberValue) Each(cb func(key any, n Node) error) error
- func (n NumberValue) Find(expr string) ([]Node, error)
- func (n NumberValue) Float64() float64
- func (n NumberValue) Get(keys ...any) Node
- func (n NumberValue) Has(keys ...any) bool
- func (n NumberValue) Int() int
- func (n NumberValue) Int64() int64
- func (n NumberValue) IsNil() bool
- func (n NumberValue) Map() Map
- func (n NumberValue) String() string
- func (n NumberValue) Type() Type
- func (n NumberValue) Value() Value
- type Operator
- type Or
- type Query
- func NewContainsQuery(args ...string) (Query, error)
- func NewCountQuery(args ...string) (Query, error)
- func NewEmptyQuery(args ...string) (Query, error)
- func NewFirstQuery(args ...string) (Query, error)
- func NewHasQuery(args ...string) (Query, error)
- func NewKeysQuery(args ...string) (Query, error)
- func NewLastQuery(args ...string) (Query, error)
- func NewMethodQuery(method string, args ...string) (Query, error)
- func NewRSortQuery(args ...string) (Query, error)
- func NewSortQuery(args ...string) (Query, error)
- func NewTypeQuery(args ...string) (Query, error)
- func NewValuesQuery(args ...string) (Query, error)
- func ParseQuery(expr string) (Query, error)
- type RSortQuery
- type SelectQuery
- type Selector
- type SlurpQuery
- type SortQuery
- type StringValue
- func (n StringValue) Array() Array
- func (n StringValue) Bool() bool
- func (n StringValue) Compare(op Operator, v Value) bool
- func (n StringValue) Each(cb func(key any, n Node) error) error
- func (n StringValue) Find(expr string) ([]Node, error)
- func (n StringValue) Float64() float64
- func (n StringValue) Get(keys ...any) Node
- func (n StringValue) Has(keys ...any) bool
- func (n StringValue) Int() int
- func (n StringValue) Int64() int64
- func (n StringValue) IsNil() bool
- func (n StringValue) Map() Map
- func (n StringValue) String() string
- func (n StringValue) Type() Type
- func (n StringValue) Value() Value
- type Type
- type TypeQuery
- type Value
- type ValueQuery
- type ValuesQuery
- type WalkFunc
- type WalkQuery
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
Nil = NilValue{}
)
var SkipWalk = errors.New("skip") //nolint:staticcheck // ST1012: sentinel value, see godoc
SkipWalk is used as a return value from WalkFunc to indicate that the node and its children in the call are to be skipped. It is not returned as an error by any function.
Like io/fs.SkipDir and io/fs.SkipAll, this is a sentinel control value rather than a failure, so it intentionally does not use the "Err" prefix recommended by ST1012.
var VERSION = "dev"
VERSION is the version number. The default "dev" value is overridden at release build time via -ldflags by goreleaser:
-X github.com/mojatter/tree.VERSION={{.Version}}
Functions ¶
func Edit ¶
Example ¶
package main
import (
"fmt"
"log"
"github.com/mojatter/tree"
)
func main() {
var group tree.Node = tree.Map{
"ID": tree.ToValue(1),
"Name": tree.ToValue("Reds"),
"Colors": tree.ToArrayValues("Crimson", "Red", "Ruby", "Maroon"),
}
if err := tree.Edit(&group, ".Colors += \"Pink\""); err != nil {
log.Fatal(err)
}
fmt.Printf("Append Pink to Colors:\n %+v\n", group)
if err := tree.Edit(&group, ".Name = \"Blue\""); err != nil {
log.Fatal(err)
}
fmt.Printf("Set Blue to Name:\n %+v\n", group)
if err := tree.Edit(&group, ".Colors ^?"); err != nil {
log.Fatal(err)
}
fmt.Printf("Delete Colors:\n %+v\n", group)
}
Output: Append Pink to Colors: map[Colors:[Crimson Red Ruby Maroon Pink] ID:1 Name:Reds] Set Blue to Name: map[Colors:[Crimson Red Ruby Maroon Pink] ID:1 Name:Blue] Delete Colors: map[ID:1 Name:Blue]
func MarshalJSON ¶
MarshalJSON returns the JSON encoding of the specified node.
Example ¶
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/mojatter/tree"
)
func main() {
group := tree.Map{
"ID": tree.ToValue(1),
"Name": tree.ToValue("Reds"),
"Colors": tree.ToArrayValues("Crimson", "Red", "Ruby", "Maroon"),
}
b, err := json.Marshal(group)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
Output: {"Colors":["Crimson","Red","Ruby","Maroon"],"ID":1,"Name":"Reds"}
Example (Combined) ¶
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/mojatter/tree"
)
func main() {
type ColorGroup struct {
ID int
Name string
Colors tree.Array
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: tree.ToArrayValues("Crimson", "Red", "Ruby", "Maroon"),
}
b, err := json.Marshal(group)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
Output: {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
func MarshalYAML ¶
MarshalYAML returns the YAML encoding of the specified node using tree's default 2-space indentation.
Example ¶
package main
import (
"fmt"
"log"
"github.com/mojatter/tree"
)
func main() {
group := tree.Map{
"ID": tree.ToValue(1),
"Name": tree.ToValue("Reds"),
"Colors": tree.ToArrayValues("Crimson", "Red", "Ruby", "Maroon"),
}
b, err := tree.MarshalYAML(group)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
Output: Colors: - Crimson - Red - Ruby - Maroon ID: 1 Name: Reds
func OutputColorJSON ¶
OutputColorJSON writes JSON values with color to out.
func OutputColorYAML ¶
OutputColorYAML writes YAML values with color to out.
func RegisterNewMethodQueryFunc ¶
func RegisterNewMethodQueryFunc(method string, fn NewMethodQueryFunc)
RegisterNewMethodQueryFunc registers a factory function for a method query. This allows dynamic registration of new method types at runtime.
func ToAny ¶
ToAny converts a Node back to a native Go any value. This is the reverse operation of ToNode.
func UnmarshalViaJSON ¶
UnmarshalViaJSON stores the node in the value pointed to by v via "encoding/json".
func UnmarshalViaYAML ¶
UnmarshalViaYAML stores the node in the value pointed to by v via "gopkg.in/yaml.v3".
Types ¶
type And ¶
type And []Selector
And represents selectors that combines each selector with and.
type Any ¶
type Any struct {
Node
}
Any is an interface that defines any node.
func (*Any) UnmarshalJSON ¶
UnmarshalJSON is an implementation of json.Unmarshaler.
type Array ¶
type Array []Node
Array represents an array of Node.
func ToArrayValues ¶
ToArrayValues calls ToValue for each provided vs and returns them as an Array.
func (*Array) UnmarshalJSON ¶
UnmarshalJSON is an implementation of json.Unmarshaler.
func (*Array) UnmarshalYAML ¶
UnmarshalYAML is an implementation of yaml.Unmarshaler (yaml.v3).
type ArrayQuery ¶
type ArrayQuery int
ArrayQuery is an index of the Array that implements methods of the Query.
func (ArrayQuery) Delete ¶
func (q ArrayQuery) Delete(pn *Node) error
func (ArrayQuery) String ¶
func (q ArrayQuery) String() string
type ArrayRangeQuery ¶
type ArrayRangeQuery []int
ArrayRangeQuery represents a range of the Array that implements methods of the Query.
func (ArrayRangeQuery) String ¶
func (q ArrayRangeQuery) String() string
type BoolValue ¶
type BoolValue bool
A BoolValue represents a bool value.
type ColorEncoder ¶
type ColorEncoder struct {
Out io.Writer
IndentSize int
NoColor bool
// contains filtered or unexported fields
}
ColorEncoder writes JSON or YAML values with color to an output stream.
func (*ColorEncoder) EncodeJSON ¶
func (e *ColorEncoder) EncodeJSON(n Node) error
EncodeJSON writes JSON values with color to an output stream.
func (*ColorEncoder) EncodeYAML ¶
func (e *ColorEncoder) EncodeYAML(n Node) error
EncodeJSON writes JSON values with color to an output stream.
type Comparator ¶
Comparator represents a comparable selector.
func (Comparator) Matches ¶
func (c Comparator) Matches(n Node) (bool, error)
Matches evaluates left and right using the operator. (eg. .id == 0)
func (Comparator) String ¶
func (c Comparator) String() string
type ContainsQuery ¶
type ContainsQuery struct {
Value string
}
ContainsQuery checks if the node contains the specified value. Works with arrays, maps, and string values.
func (*ContainsQuery) Exec ¶
func (q *ContainsQuery) Exec(n Node) ([]Node, error)
Exec returns whether the node contains the specified value. For arrays, checks if any element equals the value. For maps, checks if any value equals the value. For strings, checks if the string contains the substring.
func (*ContainsQuery) String ¶
func (q *ContainsQuery) String() string
type CountQuery ¶
type CountQuery struct{}
CountQuery returns the count of elements in arrays or maps. For other node types, returns 0.
func (*CountQuery) Exec ¶
func (q *CountQuery) Exec(n Node) ([]Node, error)
Exec returns the count of elements in the node. For arrays and maps, returns their length. For other types, returns 0.
func (*CountQuery) String ¶
func (q *CountQuery) String() string
type EditorNode ¶
type EditorNode interface {
Node
Append(v Node) error
Set(key any, v Node) error
Delete(key any) error
}
EditorNode is an interface that defines the methods to edit this node.
type EditorQuery ¶
type EditorQuery interface {
Query
Set(pn *Node, v Node) error
Append(pn *Node, v Node) error
Delete(pn *Node) error
}
EditorQuery is an interface that defines the methods to edit a node.
type EmptyQuery ¶
type EmptyQuery struct{}
EmptyQuery checks if the node is empty. Returns true for empty arrays, empty maps, null values, and empty strings.
func (*EmptyQuery) Exec ¶
func (q *EmptyQuery) Exec(n Node) ([]Node, error)
Exec returns whether the node is empty. Returns true for empty arrays, empty maps, null values, and empty strings.
func (*EmptyQuery) String ¶
func (q *EmptyQuery) String() string
type FilterQuery ¶
type FilterQuery []Query
FilterQuery consists of multiple queries that filter the nodes in order.
func (FilterQuery) String ¶
func (qs FilterQuery) String() string
type FirstQuery ¶
type FirstQuery struct{}
FirstQuery returns the first element of an array. Returns null for empty arrays or non-array types.
func (*FirstQuery) Exec ¶
func (q *FirstQuery) Exec(n Node) ([]Node, error)
Exec returns the first element of an array. Returns null for empty arrays or non-array types.
func (*FirstQuery) String ¶
func (q *FirstQuery) String() string
type HasQuery ¶
type HasQuery struct {
Key string
}
HasQuery checks if the node has the specified key. Works with both arrays (numeric keys) and maps (string keys).
type KeysQuery ¶
type KeysQuery struct{}
KeysQuery returns the keys of arrays (as indices) or maps. For arrays, returns numeric indices. For maps, returns string keys.
type LastQuery ¶
type LastQuery struct{}
LastQuery returns the last element of an array. Returns null for empty arrays or non-array types.
type Map ¶
Map represents a map of Node.
func (*Map) UnmarshalJSON ¶
UnmarshalJSON is an implementation of json.Unmarshaler.
func (*Map) UnmarshalYAML ¶
UnmarshalYAML is an implementation of yaml.Unmarshaler (yaml.v3).
type MapQuery ¶
type MapQuery string
MapQuery is a key of the Map that implements methods of the Query.
type MergeOption ¶
type MergeOption int
MergeOption represents different merge strategies for combining nodes.
var ( // MergeOptionDefault merges with the following default rules. // For examples: // - {"a": 1, "b": 2} and {"a": 3, "c": 4} merges to {"a": 1, "b": 2, "c": 4} // - [1, 2] and [3, 4, 5] merges to [1, 2, 5] // - "a" and "b" merges to "a" MergeOptionDefault MergeOption = 0 // MergeOptionOverrideMap overrides duplicate map keys. // For examples: // - {"a": 1, "b": 2} and {"a": 3} merges to {"a": 3, "b": 2} // - "a" and "b" merges to "b MergeOptionOverrideMap MergeOption = 0b000001 // MergeOptionOverrideArray overrides duplicate array indexes. // For examples: // - [1, 2, 3] and [4, 5] merges to [4, 5, 3] // - "a" and "b" merges to "b MergeOptionOverrideArray MergeOption = 0b000010 // MergeOptionOverride overrides duplicate map keys and array indexes. // For examples: // - {"a": 1, "b": 2} and {"a": 3} merges to {"a": 3, "b": 2} // - [1, 2, 3] and [4, 5] merges to [4, 5, 3] // - "a" and "b" merges to "b" MergeOptionOverride MergeOption = MergeOptionOverrideMap | MergeOptionOverrideArray // MergeOptionReplaceMap merges with replace map. // For examples: // - {"a": 1, "b": 2} and {"a": 3} merges to {"a": 3} // - "a" and "b" merges to "b" MergeOptionReplaceMap MergeOption = 0b000100 // MergeOptionReplaceArray merges with replace array. // For examples: // - [1, 2, 3] and [4, 5] merges to [4, 5] // - "a" and "b" merges to "b" MergeOptionReplaceArray MergeOption = 0b001000 // MergeOptionReplace merges with replace. // For examples: // - {"a": 1, "b": 2} and {"a": 3} merges to {"a": 3} // - [1, 2, 3] and [4, 5] merges to [4, 5] // - "a" and "b" merges to "b" MergeOptionReplace MergeOption = MergeOptionReplaceMap | MergeOptionReplaceArray // MergeOptionAppend acts when both are arrays and append them. // It takes precedence over MergeOptionOverride and MergeOptionReplace. // For examples: // - [1, 2, 3] and [4, 5] merges to [1, 2, 3, 4, 5] MergeOptionAppend MergeOption = 0b010000 // MergeOptionSlurp acts on an array or value and converts it to an array and // merges it, even if the value is not an array. // It takes precedence over MergeOptionOverride and MergeOptionReplace. // For examples: // - [1, 2, 3] and [4, 5] merges to [1, 2, 3, 4, 5] // - [1, 2, 3] and 4 merges to [1, 2, 3, 4] // - 1 and 2 merges to [1, 2] MergeOptionSlurp MergeOption = 0b100000 )
type NewMethodQueryFunc ¶
NewMethodQueryFunc is a factory function type for creating method queries. It takes string arguments and returns a Query implementation.
type NilValue ¶
type NilValue struct{}
func (NilValue) MarshalJSON ¶
MarshalJSON is an implementation of json.Marshaler.
func (NilValue) MarshalYAML ¶
MarshalYAML is an implementation of yaml.Marshaler.
type Node ¶
type Node interface {
// IsNil returns true if this node is nil.
IsNil() bool
// Type returns this node type.
Type() Type
// Array returns this node as an Array.
// If this type is not Array, returns a Array(nil).
Array() Array
// Map returns this node as a Map.
// If this type is not Map, returns a Map(nil).
Map() Map
// Value returns this node as a Value.
// If this type is not Value, returns a NilValue.
Value() Value
// Has checks this node has key.
Has(keys ...any) bool
// Get returns array/map value that matched by the specified key.
// The key type allows int or string.
// If the specified keys does not match, returns NilValue.
Get(keys ...any) Node
// Each calls the callback function for each Array|Map values.
// If the node type is not Array|Map then the callback called once with nil key and self as value.
Each(cb func(key any, v Node) error) error
// Find finds a node using the query expression.
Find(expr string) ([]Node, error)
}
A Node is an element on the tree.
func DecodeJSON ¶
DecodeJSON decodes JSON as a node using the provided decoder.
func DecodeYAML ¶
DecodeYAML decodes YAML as a node using the provided decoder.
func Find ¶
Find finds a node from n using the Query.
Example ¶
package main
import (
"fmt"
"log"
"github.com/mojatter/tree"
)
func main() {
group := tree.Map{
"ID": tree.ToValue(1),
"Name": tree.ToValue("Reds"),
"Colors": tree.ToArrayValues("Crimson", "Red", "Ruby", "Maroon"),
}
rs, err := group.Find(".Colors[1:3]")
if err != nil {
log.Fatal(err)
}
for _, r := range rs {
fmt.Println(r)
}
}
Output: Red Ruby
func MarshalViaJSON ¶
MarshalViaJSON returns the node encoding of v via "encoding/json".
func MarshalViaYAML ¶
MarshalViaYAML returns the node encoding of v via "gopkg.in/yaml.v3".
func Merge ¶
func Merge(a, b Node, opts MergeOption) Node
Merge merges two nodes with MergeOption. If you do not want to change the state of the node given as an argument, use CloneDeep. ex: merged := Merge(CloneDeep(a), CloneDeep(b), opts)
func ToNodeValues ¶
ToNodeValues calls ToValue for each provided vs and returns them as []Node.
func ToValue ¶
ToValue converts the specified v to a Value as Node. Node.Value() returns converted value.
func UnmarshalJSON ¶
UnmarshalJSON parses the JSON-encoded data to a Node.
Example ¶
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/mojatter/tree"
)
func main() {
data := []byte(`[
{"Name": "Platypus", "Order": "Monotremata"},
{"Name": "Quoll", "Order": "Dasyuromorphia"}
]`)
var animals tree.Array
err := json.Unmarshal(data, &animals)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", animals)
}
Output: [map[Name:Platypus Order:Monotremata] map[Name:Quoll Order:Dasyuromorphia]]
Example (Any) ¶
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/mojatter/tree"
)
func main() {
data := []byte(`[
{"Name": "Platypus", "Order": "Monotremata"},
{"Name": "Quoll", "Order": "Dasyuromorphia"}
]`)
var animals tree.Any
err := json.Unmarshal(data, &animals)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", animals.Type().IsArray())
fmt.Printf("%v\n", animals.Array())
}
Output: true [map[Name:Platypus Order:Monotremata] map[Name:Quoll Order:Dasyuromorphia]]
Example (Combined) ¶
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/mojatter/tree"
)
func main() {
data := []byte(`[
{"Name": "Platypus", "Order": "Monotremata"},
{"Name": "Quoll", "Order": "Dasyuromorphia"}
]`)
type Animal struct {
Name string
Order tree.StringValue
}
var animals []Animal
err := json.Unmarshal(data, &animals)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", animals)
}
Output: [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
func UnmarshalYAML ¶
UnmarshalYAML returns the YAML encoding of the specified node.
Example ¶
data := []byte(`---
Colors:
- Crimson
- Red
- Ruby
- Maroon
ID: 1
Name: Reds
`)
var group tree.Map
if err := yaml.Unmarshal(data, &group); err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", group)
Output: map[Colors:[Crimson Red Ruby Maroon] ID:1 Name:Reds]
type NopQuery ¶
type NopQuery struct{}
NopQuery is a query that implements no-op Exec method.
type NumberValue ¶
type NumberValue float64
A NumberValue represents an number value.
func (NumberValue) Compare ¶
func (n NumberValue) Compare(op Operator, v Value) bool
Compare compares n and v.
func (NumberValue) Each ¶
func (n NumberValue) Each(cb func(key any, n Node) error) error
Each calls cb(nil, n).
func (NumberValue) Find ¶
func (n NumberValue) Find(expr string) ([]Node, error)
Find finds a node using the query expression.
func (NumberValue) IsNil ¶
func (n NumberValue) IsNil() bool
IsNil returns true if this node is nil.
func (NumberValue) String ¶
func (n NumberValue) String() string
String returns this as string using strconv.FormatFloat(float64(n), 'f', -1, 64).
type Or ¶
type Or []Selector
Or represents selectors that combines each selector with or.
type Query ¶
Query is an interface that defines the methods to query a node.
func NewContainsQuery ¶
NewContainsQuery creates a new ContainsQuery instance. Requires exactly one argument specifying the value to check.
func NewCountQuery ¶
NewCountQuery creates a new CountQuery instance. Arguments are ignored for this query type.
func NewEmptyQuery ¶
NewEmptyQuery creates a new EmptyQuery instance. Arguments are ignored for this query type.
func NewFirstQuery ¶
NewFirstQuery creates a new FirstQuery instance. Arguments are ignored for this query type.
func NewHasQuery ¶
NewHasQuery creates a new HasQuery instance. Requires exactly one argument specifying the key to check.
func NewKeysQuery ¶
NewKeysQuery creates a new KeysQuery instance. Arguments are ignored for this query type.
func NewLastQuery ¶
NewLastQuery creates a new LastQuery instance. Arguments are ignored for this query type.
func NewMethodQuery ¶
NewMethodQuery creates a method query for the specified method name. Returns an error if the method is not registered.
func NewRSortQuery ¶
NewRSortQuery creates a new RSortQuery instance. Arguments:
- args[0] (optional): the query expression to sort by.
func NewSortQuery ¶
NewSortQuery creates a new SortQuery instance. Arguments:
- args[0] (optional): the query expression to sort by.
func NewTypeQuery ¶
NewTypeQuery creates a new TypeQuery instance. Arguments are ignored for this query type.
func NewValuesQuery ¶
NewValuesQuery creates a new ValuesQuery instance. Arguments are ignored for this query type.
func ParseQuery ¶
ParseQuery parses the provided expr to a Query. See https://github.com/mojatter/tree#Query
type RSortQuery ¶
RSortQuery sorts elements in an array in reverse order. For arrays of objects, it can optionally sort by a query expression.
func (*RSortQuery) Exec ¶
func (q *RSortQuery) Exec(n Node) ([]Node, error)
Exec sorts the elements of the node in reverse order if it's an array. For non-array types, returns the node as is in a slice.
func (*RSortQuery) String ¶
func (q *RSortQuery) String() string
type SelectQuery ¶
type SelectQuery struct {
Selector
}
SelectQuery returns nodes that matched by selectors.
func (SelectQuery) String ¶
func (q SelectQuery) String() string
type SlurpQuery ¶
type SlurpQuery struct{}
SlurpQuery is a special query that works in FilterQuery.
func (SlurpQuery) Exec ¶
func (q SlurpQuery) Exec(n Node) ([]Node, error)
Exec returns the provided node into a single node array. FilterQuery calls q.Exec(Array(results)), which has the effect of to slurp all the results into a single node array.
func (SlurpQuery) String ¶
func (q SlurpQuery) String() string
type SortQuery ¶
SortQuery sorts elements in an array. For arrays of objects, it can optionally sort by a query expression.
type StringValue ¶
type StringValue string
A StringValue represents a string value.
func (StringValue) Compare ¶
func (n StringValue) Compare(op Operator, v Value) bool
Compare compares n and v.
func (StringValue) Each ¶
func (n StringValue) Each(cb func(key any, n Node) error) error
Each calls cb(nil, n).
func (StringValue) Find ¶
func (n StringValue) Find(expr string) ([]Node, error)
Find finds a node using the query expression.
func (StringValue) IsNil ¶
func (n StringValue) IsNil() bool
IsNil returns true if this node is nil.
type Type ¶
type Type int
Type represents the Node type.
const ( TypeArray Type = 0b0001 TypeMap Type = 0b0010 TypeValue Type = 0b1000 TypeNilValue Type = 0b1001 TypeStringValue Type = 0b1010 TypeBoolValue Type = 0b1011 TypeNumberValue Type = 0b1100 )
These variables are the Node types.
func (Type) IsNumberValue ¶
IsNumberValue returns t == TypeNumberValue.
func (Type) IsStringValue ¶
IsStringValue returns t == TypeStringValue.
type TypeQuery ¶
type TypeQuery struct{}
TypeQuery returns the type name of the node. Returns "array", "object", "string", "number", "boolean", or "null".
type Value ¶
type Value interface {
Node
String() string
Bool() bool
Int() int
Int64() int64
Float64() float64
Compare(op Operator, v Value) bool
}
Value provides the accessor of primitive value.
type ValueQuery ¶
type ValueQuery struct {
Node
}
ValueQuery is a query that returns the constant value.
func (ValueQuery) Exec ¶
func (q ValueQuery) Exec(n Node) ([]Node, error)
Exec returns the constant value.
func (ValueQuery) String ¶
func (q ValueQuery) String() string
type ValuesQuery ¶
type ValuesQuery struct{}
ValuesQuery returns the values of arrays or maps as an array. For arrays, returns the array itself. For maps, returns values in key order.
func (*ValuesQuery) Exec ¶
func (q *ValuesQuery) Exec(n Node) ([]Node, error)
Exec returns the values of the node as an array. For arrays, returns the array itself. For maps, returns values in key order. Returns nil for other node types.
func (*ValuesQuery) String ¶
func (q *ValuesQuery) String() string
type WalkFunc ¶
WalkFunc is the type of the function called by Walk to visit each nodes.
The keys argument contains that parent keys and the node key that type is int (array index) or string (map key).