forked from hyperboloide/lk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
81 lines (63 loc) · 1.39 KB
/
helpers.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
77
78
79
80
81
package lk
import (
"bytes"
"encoding/base32"
"encoding/base64"
"encoding/gob"
"encoding/hex"
)
func toBytes(obj interface{}) ([]byte, error) {
var buffBin bytes.Buffer
encoderBin := gob.NewEncoder(&buffBin)
if err := encoderBin.Encode(obj); err != nil {
return nil, err
}
return buffBin.Bytes(), nil
}
func toB64String(obj interface{}) (string, error) {
b, err := toBytes(obj)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b), nil
}
func toB32String(obj interface{}) (string, error) {
b, err := toBytes(obj)
if err != nil {
return "", err
}
return base32.StdEncoding.EncodeToString(b), nil
}
func toHexString(obj interface{}) (string, error) {
b, err := toBytes(obj)
if err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}
func fromBytes(obj interface{}, b []byte) error {
buffBin := bytes.NewBuffer(b)
decoder := gob.NewDecoder(buffBin)
return decoder.Decode(obj)
}
func fromB64String(obj interface{}, s string) error {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return err
}
return fromBytes(obj, b)
}
func fromB32String(obj interface{}, s string) error {
b, err := base32.StdEncoding.DecodeString(s)
if err != nil {
return err
}
return fromBytes(obj, b)
}
func fromHexString(obj interface{}, s string) error {
b, err := hex.DecodeString(s)
if err != nil {
return err
}
return fromBytes(obj, b)
}