-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
37 lines (33 loc) · 765 Bytes
/
node.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
package pubsubmodel
import (
"bytes"
"encoding/gob"
)
// Node represents a pubsub node.
type Node struct {
Host string
Name string
Options Options
}
// FromBytes deserializes a Node entity from its binary representation.
func (n *Node) FromBytes(buf *bytes.Buffer) error {
dec := gob.NewDecoder(buf)
if err := dec.Decode(&n.Host); err != nil {
return err
}
if err := dec.Decode(&n.Name); err != nil {
return err
}
return dec.Decode(&n.Options)
}
// ToBytes converts a Node entity to its binary representation.
func (n *Node) ToBytes(buf *bytes.Buffer) error {
enc := gob.NewEncoder(buf)
if err := enc.Encode(n.Host); err != nil {
return err
}
if err := enc.Encode(n.Name); err != nil {
return err
}
return enc.Encode(n.Options)
}