forked from leprosus/golang-p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
64 lines (48 loc) · 959 Bytes
/
data.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
package p2p
import (
"bytes"
"encoding/gob"
"encoding/json"
)
type Data struct {
Bytes []byte
Addr string
}
func (d *Data) SetBytes(bs []byte) {
d.Bytes = bs
}
func (d *Data) GetBytes() (bs []byte) {
return d.Bytes
}
func (d *Data) SetGob(val interface{}) (err error) {
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(val)
if err != nil {
return
}
d.Bytes = buf.Bytes()
return
}
func (d *Data) GetGob(val interface{}) (err error) {
err = gob.NewDecoder(bytes.NewReader(d.Bytes)).Decode(val)
return
}
func (d *Data) SetJson(val interface{}) (err error) {
var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(val)
if err != nil {
return
}
d.Bytes = buf.Bytes()
return
}
func (d *Data) GetJson(val interface{}) (err error) {
err = json.NewDecoder(bytes.NewReader(d.Bytes)).Decode(val)
return
}
func (d *Data) SetAddr(s string) {
d.Addr = s
}
func (d *Data) String() (str string) {
return string(d.Bytes)
}