Documentation
¶
Index ¶
- Constants
- Variables
- func Edit(pn *Node, expr string) error
- func MarshalJSON(n Node) ([]byte, error)
- func MarshalYAML(n Node) ([]byte, error)
- func Walk(n Node, fn WalkFunc) error
- type And
- type Array
- func (n *Array) Append(v Node) error
- func (n Array) Array() Array
- func (n *Array) Delete(key interface{}) error
- func (n Array) Each(cb func(key interface{}, n Node) error) error
- func (n Array) Find(expr string) ([]Node, error)
- func (n Array) Get(key interface{}) Node
- func (n Array) Has(key interface{}) bool
- func (n Array) Map() Map
- func (n *Array) Set(key interface{}, v Node) error
- func (n Array) Type() Type
- func (n *Array) UnmarshalJSON(data []byte) error
- func (n *Array) UnmarshalYAML(unmarshal func(interface{}) error) 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 interface{}, n Node) error) error
- func (n BoolValue) Find(expr string) ([]Node, error)
- func (n BoolValue) Float64() float64
- func (n BoolValue) Get(key interface{}) Node
- func (n BoolValue) Has(key interface{}) bool
- func (n BoolValue) Int() int
- func (n BoolValue) Int64() int64
- func (n BoolValue) Map() Map
- func (n BoolValue) String() string
- func (n BoolValue) Type() Type
- func (n BoolValue) Value() Value
- type Comparator
- type EditorNode
- type EditorQuery
- type FilterQuery
- type Map
- func (n Map) Append(v Node) error
- func (n Map) Array() Array
- func (n Map) Delete(key interface{}) error
- func (n Map) Each(cb func(key interface{}, n Node) error) error
- func (n Map) Find(expr string) ([]Node, error)
- func (n Map) Get(key interface{}) Node
- func (n Map) Has(key interface{}) bool
- func (n Map) Keys() []string
- func (n Map) Map() Map
- func (n Map) Set(key interface{}, v Node) error
- func (n Map) Type() Type
- func (n *Map) UnmarshalJSON(data []byte) error
- func (n *Map) UnmarshalYAML(unmarshal func(interface{}) error) error
- func (n Map) Value() Value
- func (n Map) Values() []Node
- type MapQuery
- type Node
- func DecodeJSON(dec *json.Decoder) (Node, error)
- func DecodeYAML(dec *yaml.Decoder) (Node, error)
- func Find(n Node, expr string) ([]Node, error)
- func ToNode(v interface{}) Node
- func ToNodeValues(vs ...interface{}) []Node
- func ToValue(v interface{}) 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 interface{}, n Node) error) error
- func (n NumberValue) Find(expr string) ([]Node, error)
- func (n NumberValue) Float64() float64
- func (n NumberValue) Get(key interface{}) Node
- func (n NumberValue) Has(key interface{}) bool
- func (n NumberValue) Int() int
- func (n NumberValue) Int64() int64
- 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
- type SelectQuery
- type Selector
- type SlurpQuery
- 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 interface{}, n Node) error) error
- func (n StringValue) Find(expr string) ([]Node, error)
- func (n StringValue) Float64() float64
- func (n StringValue) Get(key interface{}) Node
- func (n StringValue) Has(key interface{}) bool
- func (n StringValue) Int() int
- func (n StringValue) Int64() int64
- func (n StringValue) Map() Map
- func (n StringValue) String() string
- func (n StringValue) Type() Type
- func (n StringValue) Value() Value
- type Type
- type Value
- type ValueQuery
- type WalkFunc
- type WalkQuery
Examples ¶
Constants ¶
const ( TypeArray Type = iota TypeMap TypeValue TypeStringValue = TypeValue | iota TypeBoolValue TypeNumberValue )
These variables are the Node types.
Variables ¶
var SkipWalk = errors.New("skip")
SkipWalk is used as a return value from WalkFunc to indicate that the node and that children in the call is to be skipped. It is not returned as an error by any function.
Functions ¶
func Edit ¶ added in v0.5.0
Example ¶
package main import ( "fmt" "log" "github.com/jarxorg/tree" ) func main() { var group tree.Node = tree.Map{ "ID": tree.ToValue(1), "Name": tree.ToValue("Reds"), "Colors": tree.ToArrayValues("Crimson", "Red", "Ruby", "Maroon"), } err := tree.Edit(&group, ".Colors += \"Pink\"") if err != nil { log.Fatal(err) } fmt.Printf("%+v\n", group) }
Output: map[Colors:[Crimson Red Ruby Maroon Pink] ID:1 Name:Reds]
func MarshalJSON ¶
MarshalJSON returns the JSON encoding of the specified node.
Example ¶
package main import ( "encoding/json" "fmt" "log" "github.com/jarxorg/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/jarxorg/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.
Example ¶
group := tree.Map{ "ID": tree.ToValue(1), "Name": tree.ToValue("Reds"), "Colors": tree.ToArrayValues("Crimson", "Red", "Ruby", "Maroon"), } b, err := yaml.Marshal(group) if err != nil { log.Fatal(err) } fmt.Println(string(b))
Output: Colors: - Crimson - Red - Ruby - Maroon ID: 1 Name: Reds
Types ¶
type And ¶ added in v0.4.0
type And []Selector
And represents selectors that combines each selector with and.
type Array ¶
type Array []Node
Array represents an array of Node.
func ToArrayValues ¶ added in v0.2.0
func ToArrayValues(vs ...interface{}) Array
ToArrayValues calss ToValues 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.
type ArrayQuery ¶ added in v0.2.0
type ArrayQuery int
ArrayQuery is an index of the Array that implements methods of the Query.
func (ArrayQuery) Delete ¶ added in v0.5.0
func (q ArrayQuery) Delete(pn *Node) error
func (ArrayQuery) String ¶ added in v0.5.0
func (q ArrayQuery) String() string
type ArrayRangeQuery ¶ added in v0.2.0
type ArrayRangeQuery []int
ArrayRangeQuery represents a range of the Array that implements methods of the Query.
func (ArrayRangeQuery) String ¶ added in v0.5.0
func (q ArrayRangeQuery) String() string
type BoolValue ¶
type BoolValue bool
A BoolValue represents a bool value.
type Comparator ¶ added in v0.2.0
Comparator represents a comparable selector.
func (Comparator) Matches ¶ added in v0.2.0
func (c Comparator) Matches(n Node) (bool, error)
Matches evaluates left and right using the operator. (eg. .id == 0)
func (Comparator) String ¶ added in v0.5.0
func (c Comparator) String() string
type EditorNode ¶ added in v0.5.0
type EditorNode interface { Node Append(v Node) error Set(key interface{}, v Node) error Delete(key interface{}) error }
EditorNode is an interface that defines the methods to edit this node.
type EditorQuery ¶ added in v0.5.0
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 FilterQuery ¶ added in v0.2.0
type FilterQuery []Query
FilterQuery consists of multiple queries that filter the nodes in order.
func (FilterQuery) String ¶ added in v0.5.0
func (qs FilterQuery) String() string
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.
type MapQuery ¶ added in v0.2.0
type MapQuery string
MapQuery is a key of the Map that implements methods of the Query.
type Node ¶
type Node interface { // Type returns this node type. Type() Type // Array returns this node as an Array. Array() Array // Map returns this node as a Map. Map() Map // Value returns this node as a Value. Value() Value // Has checks this node has key. Has(key interface{}) bool // Get returns array/map value that matched by the specified key. // The key type allows int or string. Get(key interface{}) 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 interface{}, 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 ¶ added in v0.2.0
DecodeJSON decodes JSON as a node using the provided decoder.
func DecodeYAML ¶ added in v0.2.0
DecodeYAML decodes YAML as a node using the provided decoder.
func Find ¶ added in v0.2.0
Find finds a node from n using the Query.
Example ¶
package main import ( "fmt" "log" "github.com/jarxorg/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]") if err != nil { log.Fatal(err) } for _, r := range rs { fmt.Printf("%#v\n", r) } }
Output: "Red"
func ToNodeValues ¶ added in v0.5.0
func ToNodeValues(vs ...interface{}) []Node
ToNodeValues calss ToValues for each provided vs and returns them as []Node.
func ToValue ¶
func ToValue(v interface{}) Node
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/jarxorg/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 (Combined) ¶
package main import ( "encoding/json" "fmt" "log" "github.com/jarxorg/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 ¶ added in v0.2.0
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 ¶ added in v0.2.0
func (n NumberValue) Compare(op Operator, v Value) bool
Compare compares n and v.
func (NumberValue) Each ¶ added in v0.3.0
func (n NumberValue) Each(cb func(key interface{}, n Node) error) error
Each calls cb(nil, n).
func (NumberValue) Find ¶ added in v0.5.0
func (n NumberValue) Find(expr string) ([]Node, error)
Find finds a node using the query expression.
func (NumberValue) Get ¶ added in v0.1.1
func (n NumberValue) Get(key interface{}) Node
Get returns nil.
func (NumberValue) Has ¶ added in v0.5.0
func (n NumberValue) Has(key interface{}) bool
Has returns false.
func (NumberValue) String ¶
func (n NumberValue) String() string
String returns this as string using strconv.FormatFloat(float64(n), 'f', -1, 64).
type Or ¶ added in v0.4.0
type Or []Selector
Or represents selectors that combines each selector with or.
type Query ¶ added in v0.2.0
Query is an interface that defines the methods to query a node.
func ParseQuery ¶ added in v0.2.0
ParseQuery parses the provided expr to a Query. See https://github.com/jarxorg/tree#Query
type SelectQuery ¶ added in v0.2.0
type SelectQuery struct {
Selector
}
SelectQuery returns nodes that matched by selectors.
func (SelectQuery) String ¶ added in v0.5.0
func (q SelectQuery) String() string
type SlurpQuery ¶ added in v0.5.0
type SlurpQuery struct{}
SlurpQuery is a special query that works in FilterQuery.
func (SlurpQuery) Exec ¶ added in v0.5.0
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 ¶ added in v0.5.0
func (q SlurpQuery) String() string
type StringValue ¶
type StringValue string
A StringValue represents a string value.
func (StringValue) Compare ¶ added in v0.2.0
func (n StringValue) Compare(op Operator, v Value) bool
Compare compares n and v.
func (StringValue) Each ¶ added in v0.3.0
func (n StringValue) Each(cb func(key interface{}, n Node) error) error
Each calls cb(nil, n).
func (StringValue) Find ¶ added in v0.5.0
func (n StringValue) Find(expr string) ([]Node, error)
Find finds a node using the query expression.
func (StringValue) Get ¶ added in v0.1.1
func (n StringValue) Get(key interface{}) Node
Get returns nil.
func (StringValue) Has ¶ added in v0.5.0
func (n StringValue) Has(key interface{}) bool
Has returns false.
type Type ¶
type Type int
Type represents the Node type.
func (Type) IsNumberValue ¶
IsNumberValue returns t == TypeNumberValue.
func (Type) IsStringValue ¶
IsStringValue returns t == TypeStringValue.
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 ¶ added in v0.2.0
type ValueQuery struct {
Node
}
ValueQuery is a query that returns the constant value.
func (ValueQuery) Exec ¶ added in v0.2.0
func (q ValueQuery) Exec(n Node) ([]Node, error)
Exec returns the constant value.
func (ValueQuery) String ¶ added in v0.5.0
func (q ValueQuery) String() string
type WalkFunc ¶ added in v0.2.0
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).