tree

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2022 License: MIT Imports: 5 Imported by: 4

README

Tree structure for the Go language

PkgGoDev Report Card

The tree nodes can marshal/unmarshal JSON and YAML that is instead of interface{}.

Examples

Go
tree.Map{
	"ID":     tree.ToValue(1),
	"Name":   tree.ToValue("Reds"),
	"Colors": tree.ToArray("Crimson", "Red", "Ruby", "Maroon"),
}
JSON
{
	"ID": 1,
	"Name": "Reds",
	"Colors": ["Crimson", "Red", "Ruby", "Maroon"]
}
YAML
ID: 1
Name: Reds
Colors:
- Crimson
- Red
- Ruby
- Maroon
Marshal and Unmarshal
package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/jarxorg/tree"
	"gopkg.in/yaml.v2"
)

func main() {
	group := tree.Map{
		"ID":     tree.ToValue(1),
		"Name":   tree.ToValue("Reds"),
		"Colors": tree.ToArray("Crimson", "Red", "Ruby", "Maroon"),
	}
	j, err := json.Marshal(group)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println()
	fmt.Println(string(j))
	fmt.Println()

	y, err := yaml.Marshal(group)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(y))
	fmt.Println()

	var n tree.Map
	if err := json.Unmarshal(j, &n); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%#v\n", n)

	// Output:
	//
	// {"Colors":["Crimson","Red","Ruby","Maroon"],"ID":1,"Name":"Reds"}
	//
	// Colors:
	// - Crimson
	// - Red
	// - Ruby
	// - Maroon
	// ID: 1
	// Name: Reds
	//
	// tree.Map{"Colors":tree.Array{"Crimson", "Red", "Ruby", "Maroon"}, "ID":1, "Name":"Reds"}
}

Documentation

Index

Examples

Constants

View Source
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

func MarshalJSON(n Node) ([]byte, error)

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

func MarshalYAML(n Node) ([]byte, error)

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 ToArray

func ToArray(v ...interface{}) Array

ToArray converts the specified v to an Array.

func (Array) Array

func (n Array) Array() Array

Array returns this node as an Array.

func (Array) Get added in v0.1.1

func (n Array) Get(key interface{}) Node

Get returns an array value as Node.

func (Array) Map

func (n Array) Map() Map

Map returns nil.

func (Array) Type

func (n Array) Type() Type

Type returns TypeArray.

func (*Array) UnmarshalJSON

func (n *Array) UnmarshalJSON(data []byte) error

UnmarshalJSON is an implementation of json.Unmarshaler.

func (*Array) UnmarshalYAML

func (n *Array) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML is an implementation of yaml.Unmarshaler.

func (Array) Value

func (n Array) Value() Value

Value returns nil.

type BoolValue

type BoolValue bool

A BoolValue represents a bool value.

func (BoolValue) Array

func (n BoolValue) Array() Array

Array returns nil.

func (BoolValue) Bool

func (n BoolValue) Bool() bool

Bool returns this.

func (BoolValue) Float64

func (n BoolValue) Float64() float64

Float64 returns 0.

func (BoolValue) Get added in v0.1.1

func (n BoolValue) Get(key interface{}) Node

Get returns nil.

func (BoolValue) Int

func (n BoolValue) Int() int

Int returns 0.

func (BoolValue) Int64

func (n BoolValue) Int64() int64

Int64 returns 0.

func (BoolValue) Map

func (n BoolValue) Map() Map

Map returns nil.

func (BoolValue) String

func (n BoolValue) String() string

String returns this as string.

func (BoolValue) Type

func (n BoolValue) Type() Type

Type returns TypeValue.

func (BoolValue) Value

func (n BoolValue) Value() Value

Value returns this.

type Map

type Map map[string]Node

Map represents a map of Node.

func (Map) Array

func (n Map) Array() Array

Array returns nil.

func (Map) Get added in v0.1.1

func (n Map) Get(key interface{}) Node

Get returns an array value as Node.

func (Map) Map

func (n Map) Map() Map

Map returns this node as a Map.

func (Map) Type

func (n Map) Type() Type

Type returns TypeMap.

func (*Map) UnmarshalJSON

func (n *Map) UnmarshalJSON(data []byte) error

UnmarshalJSON is an implementation of json.Unmarshaler.

func (*Map) UnmarshalYAML

func (n *Map) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML is an implementation of yaml.Unmarshaler.

func (Map) Value

func (n Map) Value() Value

Value returns nil.

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 ToNode

func ToNode(v interface{}) Node

ToNode converts the specified v to an 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

func UnmarshalJSON(data []byte) (Node, error)

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

func UnmarshalYAML(data []byte) (Node, error)

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) Array

func (n NumberValue) Array() Array

Array returns nil.

func (NumberValue) Bool

func (n NumberValue) Bool() bool

Bool returns false.

func (NumberValue) Float64

func (n NumberValue) Float64() float64

Float64 returns float64(n).

func (NumberValue) Get added in v0.1.1

func (n NumberValue) Get(key interface{}) Node

Get returns nil.

func (NumberValue) Int

func (n NumberValue) Int() int

Int returns int(n).

func (NumberValue) Int64

func (n NumberValue) Int64() int64

Int64 returns int64(n).

func (NumberValue) Map

func (n NumberValue) Map() Map

Map returns nil.

func (NumberValue) String

func (n NumberValue) String() string

String returns this as string using strconv.FormatFloat(float64(n), 'f', -1, 64).

func (NumberValue) Type

func (n NumberValue) Type() Type

Type returns TypeValue.

func (NumberValue) Value

func (n NumberValue) Value() Value

Value returns this.

type StringValue

type StringValue string

A StringValue represents a string value.

func (StringValue) Array

func (n StringValue) Array() Array

Array returns nil.

func (StringValue) Bool

func (n StringValue) Bool() bool

Bool returns false.

func (StringValue) Float64

func (n StringValue) Float64() float64

Float64 returns 0.

func (StringValue) Get added in v0.1.1

func (n StringValue) Get(key interface{}) Node

Get returns nil.

func (StringValue) Int

func (n StringValue) Int() int

Int returns 0.

func (StringValue) Int64

func (n StringValue) Int64() int64

Int64 returns 0.

func (StringValue) Map

func (n StringValue) Map() Map

Map returns nil.

func (StringValue) String

func (n StringValue) String() string

String returns this as string.

func (StringValue) Type

func (n StringValue) Type() Type

Type returns TypeValue.

func (StringValue) Value

func (n StringValue) Value() Value

Value returns this.

type Type

type Type int

Type represents the Node type.

func (Type) IsArray

func (t Type) IsArray() bool

IsArray returns t == TypeArray.

func (Type) IsBoolValue

func (t Type) IsBoolValue() bool

IsBoolValue returns t == TypeBoolValue.

func (Type) IsMap

func (t Type) IsMap() bool

IsMap returns t == TypeMap.

func (Type) IsNumberValue

func (t Type) IsNumberValue() bool

IsNumberValue returns t == TypeNumberValue.

func (Type) IsStringValue

func (t Type) IsStringValue() bool

IsStringValue returns t == TypeStringValue.

func (Type) IsValue

func (t Type) IsValue() bool

IsValue returns true if t is TypeStringValue or TypeBoolValue or TypeNumberValue.

type Value

type Value interface {
	String() string
	Bool() bool
	Int() int
	Int64() int64
	Float64() float64
}

Value provides the accessor of primitive value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL