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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
package nested_test
import (
"fmt"
"strings"
"github.com/knqyf263/nested"
)
func Example() {
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
}
func ExampleNested_Set() {
n := nested.Nested{}
n.Set([]string{"a", "b", "c"}, 1)
fmt.Printf("%v", n)
// Output: map[a:map[b:map[c:1]]]
}
func ExampleNested_SetByString() {
n := nested.Nested{}
n.SetByString("a.b.c", ".", "test")
fmt.Printf("%v", n)
// Output: map[a:map[b:map[c:test]]]
}
func ExampleNested_Get() {
n := nested.Nested{"a": map[string]interface{}{"b": 1}}
result, _ := n.Get([]string{"a", "b"})
fmt.Printf("%v", result)
// Output: 1
}
func ExampleNested_GetByString() {
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 ExampleNested_Delete() {
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 ExampleNested_DeleteByString() {
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 ExampleNested_Walk() {
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
}
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]]
}
|