forked from fagongzi/manba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
60 lines (48 loc) · 1017 Bytes
/
bind.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
package model
import (
"encoding/json"
"fmt"
"io"
"strings"
)
// Bind a bind server and cluster
type Bind struct {
ClusterName string `json:"clusterName,omitempty"`
ServerAddr string `json:"serverAddr,omitempty"`
}
// UnMarshalBindFromReader unmarshal
func UnMarshalBindFromReader(r io.Reader) (*Bind, error) {
v := &Bind{}
decoder := json.NewDecoder(r)
err := decoder.Decode(v)
if nil != err {
return nil, err
}
return v, nil
}
// ToString return a desc string
func (b *Bind) ToString() string {
return fmt.Sprintf("%s-%s", b.ServerAddr, b.ClusterName)
}
// UnMarshalBindFromString unmarshal
func UnMarshalBindFromString(key string) *Bind {
info := strings.Split(key, "-")
return &Bind{
ServerAddr: info[0],
ClusterName: info[1],
}
}
// Marshal marshal
func (b *Bind) Marshal() []byte {
v, _ := json.Marshal(b)
return v
}
// UnMarshalBind unmarshal
func UnMarshalBind(data []byte) *Bind {
v := &Bind{}
err := json.Unmarshal(data, v)
if err != nil {
return v
}
return v
}