-
Notifications
You must be signed in to change notification settings - Fork 0
/
gob64-serializer.go
76 lines (62 loc) · 1.67 KB
/
gob64-serializer.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
71
72
73
74
75
76
package quota
import (
"bytes"
"encoding/base64"
"encoding/gob"
"fmt"
)
// GOB64 is a serializer that uses gob and base64.
// serialize serializes the given data.
func serialize(i interface{}) (string, error) {
b := bytes.Buffer{}
e := gob.NewEncoder(&b)
err := e.Encode(i)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b.Bytes()), nil
}
// SerializeCandle serializes the given candle.
func SerializeCandle(c *Candle) (data string, err error) {
data, err = serialize(c)
if err != nil {
return "", fmt.Errorf("failed to encode the candle: %s", err)
}
return data, err
}
// DeserializeCandle deserializes the given data.
func DeserializeCandle(data string) (c *Candle, err error) {
b, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, fmt.Errorf("failed to decode the candle: %s", err)
}
bReader := bytes.NewReader(b)
d := gob.NewDecoder(bReader)
err = d.Decode(c)
if err != nil {
return nil, fmt.Errorf("failed to decode the candle: %s", err)
}
return c, err
}
// SerializeQuota serializes the given quota.
func SerilizeQuota(q *Quota) (data string, err error) {
data, err = serialize(q)
if err != nil {
return "", fmt.Errorf("failed to encode the quota: %s", err)
}
return data, err
}
// DeserializeQuota deserializes the given data.
func DeserializeQuota(data string) (q *Quota, err error) {
b, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, fmt.Errorf("failed to decode the quota: %s", err)
}
bReader := bytes.NewReader(b)
d := gob.NewDecoder(bReader)
err = d.Decode(q)
if err != nil {
return nil, fmt.Errorf("failed to decode the quota: %s", err)
}
return q, err
}