-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.go
More file actions
65 lines (58 loc) · 1.4 KB
/
utils.go
File metadata and controls
65 lines (58 loc) · 1.4 KB
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
package bitxid
import (
"bytes"
"encoding/gob"
"fmt"
)
// Marshal .
func Marshal(s interface{}) ([]byte, error) {
buf := bytes.Buffer{}
err := gob.NewEncoder(&buf).Encode(s)
if err != nil {
return []byte{}, fmt.Errorf("gob encode err: %w", err)
}
return buf.Bytes(), nil
}
// Unmarshal .
func Unmarshal(b []byte, s interface{}) error {
buf := bytes.NewBuffer(b)
err := gob.NewDecoder(buf).Decode(s)
if err != nil {
return fmt.Errorf("gob decode err: %w", err)
}
return nil
}
// UnmarshalAccountDoc converts byte doc to struct doc
func UnmarshalAccountDoc(docBytes []byte) (AccountDoc, error) {
docStruct := AccountDoc{}
err := Unmarshal(docBytes, &docStruct)
if err != nil {
return AccountDoc{}, err
}
return docStruct, nil
}
// MarshalAccountDoc converts struct doc to byte doc
func MarshalAccountDoc(docStruct AccountDoc) ([]byte, error) {
docBytes, err := Marshal(docStruct)
if err != nil {
return nil, err
}
return docBytes, nil
}
// UnmarshalChainDoc converts byte doc to struct doc
func UnmarshalChainDoc(docBytes []byte) (ChainDoc, error) {
docStruct := ChainDoc{}
err := Unmarshal(docBytes, &docStruct)
if err != nil {
return ChainDoc{}, err
}
return docStruct, nil
}
// MarshalChainDoc converts struct doc to byte doc
func MarshalChainDoc(docStruct ChainDoc) ([]byte, error) {
docBytes, err := Marshal(docStruct)
if err != nil {
return nil, err
}
return docBytes, nil
}