-
Notifications
You must be signed in to change notification settings - Fork 178
/
codec.go
69 lines (50 loc) · 1.13 KB
/
codec.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
package rlp
import (
"io"
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/flow-go/model/encoding"
)
var _ encoding.Marshaler = (*Marshaler)(nil)
type Marshaler struct{}
func NewMarshaler() *Marshaler {
return &Marshaler{}
}
func (m *Marshaler) Marshal(val interface{}) ([]byte, error) {
return rlp.EncodeToBytes(val)
}
func (m *Marshaler) Unmarshal(b []byte, val interface{}) error {
return rlp.DecodeBytes(b, val)
}
func (m *Marshaler) MustMarshal(val interface{}) []byte {
b, err := m.Marshal(val)
if err != nil {
panic(err)
}
return b
}
func (m *Marshaler) MustUnmarshal(b []byte, val interface{}) {
err := m.Unmarshal(b, val)
if err != nil {
panic(err)
}
}
var _ encoding.Codec = (*Codec)(nil)
type Codec struct{}
func (c *Codec) NewEncoder(w io.Writer) encoding.Encoder {
return &Encoder{w}
}
func (c *Codec) NewDecoder(r io.Reader) encoding.Decoder {
return &Decoder{r}
}
type Encoder struct {
w io.Writer
}
func (e *Encoder) Encode(v interface{}) error {
return rlp.Encode(e.w, v)
}
type Decoder struct {
r io.Reader
}
func (e *Decoder) Decode(v interface{}) error {
return rlp.Decode(e.r, v)
}