forked from iost-official/go-iost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.go
137 lines (118 loc) · 3.2 KB
/
serialize.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package common
import (
"bytes"
"fmt"
)
// SimpleEncoder is a simple encoder used to convert struct to bytes.
type SimpleEncoder struct {
buf bytes.Buffer
}
// NewSimpleEncoder returns a new SimpleEncoder instance.
func NewSimpleEncoder() *SimpleEncoder {
return &SimpleEncoder{}
}
// WriteByte writes a byte to buffer.
func (se *SimpleEncoder) WriteByte(b byte) { // nolint
se.buf.WriteByte(b)
}
// WriteBytes writes a byte slice to buffer.
func (se *SimpleEncoder) WriteBytes(bs []byte) {
se.WriteInt32(int32(len(bs)))
se.buf.Write(bs)
}
// WriteString writes a string to buffer.
func (se *SimpleEncoder) WriteString(s string) {
se.WriteBytes([]byte(s))
}
// WriteInt64 writes a int64 to buffer.
func (se *SimpleEncoder) WriteInt64(i int64) {
se.buf.Write(Int64ToBytes(i))
}
// WriteInt32 writes a int32 to buffer.
func (se *SimpleEncoder) WriteInt32(i int32) {
se.buf.Write(Int32ToBytes(i))
}
// WriteFloat64 writes a float64 to buffer.
func (se *SimpleEncoder) WriteFloat64(f float64) {
se.buf.Write(Float64ToBytes(f))
}
// WriteBytesSlice writes a bytes slice to buffer.
func (se *SimpleEncoder) WriteBytesSlice(p [][]byte) {
se.WriteInt32(int32(len(p)))
for _, bs := range p {
se.WriteBytes(bs)
}
}
// WriteStringSlice writes a string slice to buffer.
func (se *SimpleEncoder) WriteStringSlice(p []string) {
se.WriteInt32(int32(len(p)))
for _, bs := range p {
se.WriteString(bs)
}
}
// WriteMapStringToI64 writes a map[string]int64 to buffer.
func (se *SimpleEncoder) WriteMapStringToI64(m map[string]int64) {
key := make([]string, 0, len(m))
for k := range m {
key = append(key, k)
}
for i := 1; i < len(m); i++ {
for j := 0; j < len(m)-i; j++ {
if key[j] > key[j+1] {
key[j], key[j+1] = key[j+1], key[j]
}
}
}
se.WriteInt32(int32(len(m)))
for _, k := range key {
se.WriteString(k)
se.WriteInt64(m[k])
}
}
// Bytes returns the result bytes of buffer.
func (se *SimpleEncoder) Bytes() []byte {
return se.buf.Bytes()
}
// Reset resets the buffer.
func (se *SimpleEncoder) Reset() {
se.buf.Reset()
}
// SimpleDecoder is a simple decoder used to convert bytes to other types. Not used now!!!
type SimpleDecoder struct {
input []byte
}
// NewSimpleDecoder returns a new SimpleDecoder instance.
func NewSimpleDecoder(input []byte) *SimpleDecoder {
return &SimpleDecoder{input}
}
// ParseByte parse input, return first byte
func (sd *SimpleDecoder) ParseByte() (byte, error) {
if len(sd.input) < 1 {
return 0, fmt.Errorf("parse byte fail: invalid len %v", sd.input)
}
result := sd.input[0]
sd.input = sd.input[1:]
return result, nil
}
// ParseInt32 parse input, return first int32
func (sd *SimpleDecoder) ParseInt32() (int32, error) {
if len(sd.input) < 4 {
return 0, fmt.Errorf("parse int32 fail: invalid len %v", sd.input)
}
result := BytesToInt32(sd.input[:4])
sd.input = sd.input[4:]
return result, nil
}
// ParseBytes parse input, return first byte array
func (sd *SimpleDecoder) ParseBytes() ([]byte, error) {
length, err := sd.ParseInt32()
if err != nil {
return nil, err
}
if len(sd.input) < int(length) {
return nil, fmt.Errorf("bytes length too large: %v > %v", length, len(sd.input))
}
result := sd.input[:length]
sd.input = sd.input[length:]
return result, nil
}