-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget.go
70 lines (56 loc) · 1.46 KB
/
get.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
// Copyright 2018-2019 The vogo Authors. All rights reserved.
// author: wongoo
// since: 2018/12/27
//
package zkclient
import (
"encoding/json"
"reflect"
)
// Encode value from zookeeper, the raw value will be decoded by codec
func (cli *Client) Get(path string, codec Codec) (interface{}, error) {
data, _, err := cli.Conn().Get(path)
if err != nil {
return nil, err
}
v, err := codec.Decode(data)
if err != nil {
return nil, err
}
return v, nil
}
// GetString get string value from zookeeper
func (cli *Client) GetString(path string) (string, error) {
data, _, err := cli.Conn().Get(path)
if err != nil {
return "", err
}
return string(data), nil
}
// GetJSON get json value from zookeeper
func (cli *Client) GetJSON(path string, typ reflect.Type) (interface{}, error) {
data, _, err := cli.Conn().Get(path)
if err != nil {
return nil, err
}
c := &JSONCodec{typ: typ}
return c.Decode(data)
}
// ParseJSON parse json value from zookeeper into target object
func (cli *Client) ParseJSON(path string, target interface{}) error {
data, _, err := cli.Conn().Get(path)
if err != nil {
return err
}
return json.Unmarshal(data, target)
}
// GetChildren get child nodes
func (cli *Client) GetChildren(path string) ([]string, error) {
children, _, err := cli.Conn().Children(path)
return children, err
}
// Exists check node exists
func (cli *Client) Exists(path string) (bool, error) {
exists, _, err := cli.Conn().Exists(path)
return exists, err
}