forked from zazab/zhash
/
example_test.go
58 lines (47 loc) · 1.08 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
package zhash_test
import (
"bytes"
"encoding/json"
"fmt"
"log"
"github.com/zazab/zhash"
)
// Example HashFromMap shows how to initialize your hash from map
func Example_hashFromMap() {
m := map[string]interface{}{
"plainValue": 10.1,
"subMap": map[string]interface{}{
"elem1": 10,
"elem2": true,
},
}
h := zhash.HashFromMap(m)
f, err := h.GetFloat("plainValue")
if err != nil {
log.Fatal(err)
}
fmt.Println(f)
// Output:
// 10.1
}
// Example ReadHash shows how to initialize your hash using Unmarshal function
// You can use any function that satisfies Unmarshaller type for ReadHash. For
// example, see TomlExample for example of using BurntSushi/toml for
// unmarshalling
func Example_readHash() {
h := zhash.NewHash()
h.SetMarshallerFunc(json.Marshal)
h.SetUnmarshallerFunc(json.Unmarshal)
b := bytes.NewBuffer([]byte("{\"this\": \"is\", \"some_json_map\": 14.1}"))
err := h.ReadHash(b)
if err != nil {
log.Fatal(err)
}
s, err := h.GetString("this")
if err != nil {
log.Fatal(err)
}
fmt.Printf("This %s working", s)
// Output:
// This is working
}