nested

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2019 License: MIT Imports: 2 Imported by: 16

README

nested

Easier way to handle the nested data structure

GoDoc Build Status Coverage Status Go Report Card MIT License

Usage

Set/Get/Delete
package main

import (
	"fmt"

	"github.com/knqyf263/nested"
)

func main() {
	n := nested.Nested{}

	n.Set([]string{"a", "b"}, 1)
	n.SetByString("a.c.d", ".", "test")
	n.SetByString("/e/f", "/", true)

	var result interface{}
	result, _ = n.Get([]string{"a", "c", "d"})
	fmt.Println(result)
	// Output: test

	result, _ = n.GetByString("e/f", "/")
	fmt.Println(result)
	// Output: true

	var b int
	b, _ = n.GetInt([]string{"a", "b"})
	fmt.Println(b)
	// Output: 1
	
	n.Delete([]string{"a", "c"})
}
Walk
func main() {
	n := nested.Nested{}
	n.Set([]string{"a", "b", "c"}, 1)
	n.Set([]string{"d", "e"}, 2)
	n.SetByString("f.g.h", ".", false)

	walkFn := func(keys []string, value interface{}) error {
		key := strings.Join(keys, ".")
		// Skip all keys under "f"
		if key == "f" {
			return nested.SkipKey
		}
		fmt.Println(key, value)
		return nil
	}
	n.Walk(walkFn)

	// Output:
	// a map[b:map[c:1]]
	// a.b map[c:1]
	// a.b.c 1
	// d map[e:2]
	// d.e 2
	// f map[g:map[h:1]]
}

Install

$ go get github.com/knqyf263/nested

Contribute

  1. fork a repository: github.com/knqyf263/nested to github.com/you/repo
  2. get original code: go get github.com/knqyf263/nested
  3. work on original code
  4. add remote to your repo: git remote add myfork https://github.com/you/repo.git
  5. push your changes: git push myfork
  6. create a new Pull Request

License

MIT

Author

Teppei Fukuda

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/knqyf263/nested"
)

func main() {
	n := nested.Nested{}

	n.Set([]string{"a", "b"}, 1)
	n.SetByString("a.c.d", ".", "test")
	n.SetByString("/e/f", "/", true)

	var result interface{}
	result, _ = n.Get([]string{"a", "c", "d"})
	fmt.Println(result)

	result, _ = n.GetByString("e/f", "/")
	fmt.Println(result)

	var b int
	b, _ = n.GetInt([]string{"a", "b"})
	fmt.Println(b)

}
Output:

test
true
1

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNoSuchKey     = errors.New("no such key")
	ErrUnmatchedType = errors.New("unmatched type")
)
View Source
var SkipKey = errors.New("skip this key")

Functions

This section is empty.

Types

type Nested

type Nested map[string]interface{}

func (Nested) Delete

func (n Nested) Delete(keys []string) error
Example
package main

import (
	"fmt"

	"github.com/knqyf263/nested"
)

func main() {
	n := nested.Nested{"a": map[string]interface{}{"b": 1, "c": 2}}
	n.Delete([]string{"a", "b"})
	fmt.Printf("%v", n)
}
Output:

map[a:map[c:2]]

func (Nested) DeleteByString

func (n Nested) DeleteByString(key, sep string) error
Example
package main

import (
	"fmt"

	"github.com/knqyf263/nested"
)

func main() {
	n := nested.Nested{"a": map[string]interface{}{"b": 1, "c": 2}}
	n.DeleteByString("a/b", "/")
	fmt.Printf("%v", n)
}
Output:

map[a:map[c:2]]

func (Nested) Get

func (n Nested) Get(keys []string) (value interface{}, err error)
Example
package main

import (
	"fmt"

	"github.com/knqyf263/nested"
)

func main() {
	n := nested.Nested{"a": map[string]interface{}{"b": 1}}
	result, _ := n.Get([]string{"a", "b"})
	fmt.Printf("%v", result)
}
Output:

1

func (Nested) GetBool

func (n Nested) GetBool(keys []string) (value bool, err error)

func (Nested) GetByString

func (n Nested) GetByString(key, sep string) (value interface{}, err error)
Example
package main

import (
	"fmt"

	"github.com/knqyf263/nested"
)

func main() {
	n := nested.Nested{"a": map[string]interface{}{"b": map[string]interface{}{"c": 2}}}
	result, _ := n.GetByString("a.b.c", ".")
	fmt.Printf("%v", result)
}
Output:

2

func (Nested) GetInt

func (n Nested) GetInt(keys []string) (value int, err error)

func (Nested) GetString

func (n Nested) GetString(keys []string) (value string, err error)

func (Nested) Set

func (n Nested) Set(keys []string, value interface{})
Example
package main

import (
	"fmt"

	"github.com/knqyf263/nested"
)

func main() {
	n := nested.Nested{}
	n.Set([]string{"a", "b", "c"}, 1)
	fmt.Printf("%v", n)
}
Output:

map[a:map[b:map[c:1]]]

func (Nested) SetByString

func (n Nested) SetByString(key, sep string, value interface{})
Example
package main

import (
	"fmt"

	"github.com/knqyf263/nested"
)

func main() {
	n := nested.Nested{}
	n.SetByString("a.b.c", ".", "test")
	fmt.Printf("%v", n)
}
Output:

map[a:map[b:map[c:test]]]

func (Nested) Walk

func (n Nested) Walk(walkFn WalkFunc) error
Example
package main

import (
	"fmt"

	"github.com/knqyf263/nested"
)

func main() {
	n := nested.Nested{}
	n.Set([]string{"a", "b", "c"}, 1)
	n.Set([]string{"d", "e"}, 2)

	walkFn := func(keys []string, value interface{}) error {
		fmt.Println(keys, value)
		return nil
	}
	n.Walk(walkFn)

	// [a] map[b:map[c:1]]
	// [a b] map[c:1]
	// [a b c] 1
	// [d] map[e:2]
	// [d e] 2
}
Output:

type WalkFunc

type WalkFunc func(keys []string, value interface{}) error

Jump to

Keyboard shortcuts

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