-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
89 lines (68 loc) · 1.52 KB
/
example_test.go
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
package jstore_test
import (
"fmt"
"github.com/andresbott/jstore"
"strings"
)
type Contact struct {
ID int
Name string
}
func ExampleStoreMultipleItem() {
// note error handling has been ignored in this example
// new Database either on a file or in memory
db, _ := jstore.New(jstore.InMemoryDb)
// use a collection
collection := db.Use("contacts")
cntcs := []Contact{
{
ID: 1,
Name: "Luke",
},
{
ID: 2,
Name: "Leia",
},
}
// write data into the collection
_ = collection.Set(cntcs)
newCntcs := []Contact{}
_ = collection.Get(&newCntcs)
names := []string{}
for _, c := range newCntcs {
names = append(names, c.Name)
}
fmt.Printf("there are %d contacts in your list: %s \n", len(newCntcs), strings.Join(names, ","))
// Output: there are 2 contacts in your list: Luke,Leia
}
func ExampleKV() {
// note error handling has been ignored in this example
// new Database either on a file or in memory
db, _ := jstore.New(jstore.InMemoryDb)
// create a collection
kv := db.Kv()
// set two keys
_ = kv.Set("key-1", 100)
_ = kv.Set("key-2", "2")
// update a key
_ = kv.Set("key-2", "this is an updated value")
// get a values
val := ""
_ = kv.Get("key-2", &val)
fmt.Println(val)
// check if key exists
fmt.Println(kv.Exists("key-1"))
// delete a key
_ = kv.Del("key-1")
// get the Json representation
fmt.Println(string(db.Json()))
// Output:
// this is an updated value
// true
// {
// "kv": {
// "key-2": "this is an updated value"
// }
// }
//
}