1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
# nested
Easier way to handle the nested data structure
[](https://godoc.org/github.com/knqyf263/nested)
[](https://travis-ci.org/knqyf263/nested)
[](https://coveralls.io/github/knqyf263/nested?branch=master)
[](https://goreportcard.com/report/github.com/knqyf263/nested)
[](https://github.com/knqyf263/nested/blob/master/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
- see [GitHub and Go: forking, pull requests, and go-getting](http://blog.campoy.cat/2014/03/github-and-go-forking-pull-requests-and.html)
----
## License
MIT
## Author
Teppei Fukuda
|