tree

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree is a wrapper over tha root node od a tree

func New

func New(v interface{}) (*Tree, error)

New returns a new Tee containning a decomposed version of v

Example
sample := map[string]interface{}{
	"supu": 42,
	"tupu": false,
	"foo":  "bar",
	"aa":   23,
	"a": map[string]interface{}{
		"aa":  23,
		"ac":  23,
		"abc": 23,
		"a": map[string]interface{}{
			"a": map[string]interface{}{
				"b": true,
				"c": 42,
				"d": "tupu",
			},
			"b": true,
			"c": 42,
			"d": "tupu",
		},
		"ab": map[string]interface{}{
			"b": true,
			"c": 42,
			"d": "tupu",
		},
		"b": true,
		"c": 42,
		"d": "tupu",
		"collection": []interface{}{
			map[string]interface{}{
				"b":  false,
				"d":  "aaaa",
				"dx": "foobar",
			},
			map[string]interface{}{
				"b": true,
				"c": 42,
				"d": "tupu",
			},
		},
		"collection2": []interface{}{
			map[string]interface{}{
				"d": []interface{}{
					map[string]interface{}{
						"a": 1,
						"d": []int{1, 2, 3, 4, 5},
					},
					map[string]interface{}{
						"d": []int{1, 2, 3, 4, 5},
					},
					map[string]interface{}{
						"d": []int{1, 2, 3, 4, 5},
					},
				},
			},
			map[string]interface{}{
				"d": []int{1, 2, 3, 4, 5},
			},
			map[string]interface{}{
				"d": []int{1, 2, 3, 4, 5},
			},
		},
	},
}

tree, err := New(sample)
tree.Sort()
fmt.Println("error:", err)
// fmt.Println(tree)

tree.Del([]string{"a", "b"})
tree.Del([]string{"a", "collection", "0"})
tree.Del([]string{"a", "collection2", "*", "d", "*", "d"})
fmt.Println(tree)
b, _ := json.MarshalIndent(tree.Get([]string{}), "", "\t")
fmt.Println(string(b))
Output:

error: <nil>

├── a
│   ├── a
│   │   ├── a
│   │   │   ├── b	true
│   │   │   ├── c	42
│   │   │   └── d	tupu
│   │   ├── b	true
│   │   ├── c	42
│   │   └── d	tupu
│   ├── aa	23
│   ├── ab
│   │   ├── b	true
│   │   ├── c	42
│   │   └── d	tupu
│   ├── abc	23
│   ├── ac	23
│   ├── c	42
│   ├── collection []
│   │   └── 1
│   │       ├── b	true
│   │       ├── c	42
│   │       └── d	tupu
│   ├── collection2 []
│   │   ├── 0
│   │   │   └── d []
│   │   │       ├── 0
│   │   │       │   └── a	1
│   │   │       ├── 1	<nil>
│   │   │       └── 2	<nil>
│   │   ├── 1
│   │   │   └── d	[1 2 3 4 5]
│   │   └── 2
│   │       └── d	[1 2 3 4 5]
│   └── d	tupu
├── aa	23
├── foo	bar
├── supu	42
└── tupu	false

{
	"a": {
		"a": {
			"a": {
				"b": true,
				"c": 42,
				"d": "tupu"
			},
			"b": true,
			"c": 42,
			"d": "tupu"
		},
		"aa": 23,
		"ab": {
			"b": true,
			"c": 42,
			"d": "tupu"
		},
		"abc": 23,
		"ac": 23,
		"c": 42,
		"collection": [
			{
				"b": true,
				"c": 42,
				"d": "tupu"
			}
		],
		"collection2": [
			{
				"d": [
					{
						"a": 1
					},
					null,
					null
				]
			},
			{
				"d": [
					1,
					2,
					3,
					4,
					5
				]
			},
			{
				"d": [
					1,
					2,
					3,
					4,
					5
				]
			}
		],
		"d": "tupu"
	},
	"aa": 23,
	"foo": "bar",
	"supu": 42,
	"tupu": false
}

func (*Tree) Add

func (t *Tree) Add(ks []string, v interface{})

Add introduces the value v at the path defined by ks

func (*Tree) Append

func (t *Tree) Append(src, dst []string)

Append appends the contents of the array src to the array dst

func (*Tree) Del

func (t *Tree) Del(ks []string)

Del removes all nodes and edges in the tree matching the path defined by ks

func (*Tree) Get

func (t *Tree) Get(ks []string) interface{}

Get returns all nodes and edges in the tree matching the path defined by ks

func (*Tree) Move

func (t *Tree) Move(src, dst []string)

Move moves all nodes and edges in the tree matching the path defined by src to dst

func (*Tree) Sort

func (t *Tree) Sort()

Sort sorts the edges in the tree

Jump to

Keyboard shortcuts

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