Documentation
¶
Index ¶
- Constants
- func MarshalJSON(n Node) ([]byte, error)
- func MarshalYAML(n Node) ([]byte, error)
- type Array
- type BoolValue
- func (n BoolValue) Array() Array
- func (n BoolValue) Bool() bool
- func (n BoolValue) Float64() float64
- func (n BoolValue) Get(key interface{}) Node
- 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 Map
- type Node
- type NumberValue
- func (n NumberValue) Array() Array
- func (n NumberValue) Bool() bool
- func (n NumberValue) Float64() float64
- func (n NumberValue) Get(key interface{}) Node
- 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 StringValue
- func (n StringValue) Array() Array
- func (n StringValue) Bool() bool
- func (n StringValue) Float64() float64
- func (n StringValue) Get(key interface{}) Node
- 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
Examples ¶
Constants ¶
const ( TypeArray Type = 1 << (32 - 1 - iota) TypeMap TypeValue TypeStringValue = TypeValue | iota TypeBoolValue TypeNumberValue )
These variables are the Node types.
Variables ¶
This section is empty.
Functions ¶
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.ToArray("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.ToArray("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.ToArray("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 Array ¶
type Array []Node
Array represents an array of Node.
func (*Array) UnmarshalJSON ¶
UnmarshalJSON is an implementation of json.Unmarshaler.
func (*Array) UnmarshalYAML ¶
UnmarshalYAML is an implementation of yaml.Unmarshaler.
type BoolValue ¶
type BoolValue bool
A BoolValue represents a bool value.
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 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
// Get returns array/map value that matched by the specified key.
// The key type allows int or string.
Get(key interface{}) Node
}
A Node is an element on the tree.
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 NumberValue ¶
type NumberValue float64
A NumberValue represents an number value.
func (NumberValue) Get ¶ added in v0.1.1
func (n NumberValue) Get(key interface{}) Node
Get returns nil.
func (NumberValue) String ¶
func (n NumberValue) String() string
String returns this as string using strconv.FormatFloat(float64(n), 'f', -1, 64).
type StringValue ¶
type StringValue string
A StringValue represents a string value.
func (StringValue) Get ¶ added in v0.1.1
func (n StringValue) Get(key interface{}) Node
Get returns nil.
type Type ¶
type Type int
Type represents the Node type.
func (Type) IsNumberValue ¶
IsNumberValue returns t == TypeNumberValue.
func (Type) IsStringValue ¶
IsStringValue returns t == TypeStringValue.