forked from tendermint/go-amino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.go
171 lines (153 loc) · 4.7 KB
/
wire.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package wire
import (
"bytes"
"encoding/json"
"errors"
"io"
"reflect"
cmn "github.com/tendermint/tmlibs/common"
)
var (
ErrBinaryReadOverflow = errors.New("Error: binary read overflow")
ErrBinaryReadInvalidLength = errors.New("Error: binary read invalid length")
ErrBinaryReadInvalidTimeNegative = errors.New("Error: binary read invalid time - negative")
ErrBinaryReadInvalidTimeSubMillisecond = errors.New("Error: binary read invalid time - sub millisecond")
ErrBinaryWriteOverflow = errors.New("Error: binary write overflow")
)
const (
ReadSliceChunkSize = 1024
)
func ReadBinary(o interface{}, r io.Reader, lmt int, n *int, err *error) (res interface{}) {
rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
if rv.Kind() == reflect.Ptr {
if rv.IsNil() {
// This allows ReadBinary() to return a nil pointer,
// if the value read is nil.
rvPtr := reflect.New(rt)
ReadBinaryPtr(rvPtr.Interface(), r, lmt, n, err)
res = rvPtr.Elem().Interface()
} else {
readReflectBinary(rv, rt, Options{}, r, lmt, n, err)
res = o
}
} else {
ptrRv := reflect.New(rt)
readReflectBinary(ptrRv.Elem(), rt, Options{}, r, lmt, n, err)
res = ptrRv.Elem().Interface()
}
if lmt != 0 && lmt < *n && *err == nil {
*err = ErrBinaryReadOverflow
}
return res
}
func ReadBinaryPtr(o interface{}, r io.Reader, lmt int, n *int, err *error) (res interface{}) {
rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
if rv.Kind() == reflect.Ptr {
readReflectBinary(rv.Elem(), rt.Elem(), Options{}, r, lmt, n, err)
} else {
cmn.PanicSanity("ReadBinaryPtr expects o to be a pointer")
}
res = o
if lmt != 0 && lmt < *n && *err == nil {
*err = ErrBinaryReadOverflow
}
return res
}
func ReadBinaryPtrLengthPrefixed(o interface{}, r io.Reader, lmt int, n *int, err *error) (res interface{}) {
length := ReadVarint(r, n, err)
nSave := *n
res = ReadBinaryPtr(o, r, lmt, n, err)
nRes := *n - nSave
if nRes != length && *err == nil {
*err = errors.New(cmn.Fmt("Error: binary ready wrong length prefix. Declared:%v vs actual:%v", length, nRes))
}
return res
}
// WriteBinary is the binary encoder. Its arguments are the subject to be
// encoded, the writer that'll receive the encoded bytes, as well as a
// receiver to store the bytes written and any error encountered.
func WriteBinary(o interface{}, w io.Writer, n *int, err *error) {
rv := reflect.ValueOf(o)
rt := reflect.TypeOf(o)
writeReflectBinary(rv, rt, Options{}, w, n, err)
}
func WriteBinaryLengthPrefixed(o interface{}, w io.Writer, n *int, err *error) {
var bufN int
var buf = new(bytes.Buffer)
WriteBinary(o, buf, &bufN, err)
WriteVarint(buf.Len(), w, n, err)
WriteTo(buf.Bytes(), w, n, err)
}
func ReadJSON(o interface{}, bytes []byte, err *error) interface{} {
var object interface{}
*err = json.Unmarshal(bytes, &object)
if *err != nil {
return o
}
return ReadJSONObject(o, object, err)
}
func ReadJSONPtr(o interface{}, bytes []byte, err *error) interface{} {
var object interface{}
*err = json.Unmarshal(bytes, &object)
if *err != nil {
return o
}
return ReadJSONObjectPtr(o, object, err)
}
// o is the ultimate destination, object is the result of json unmarshal
func ReadJSONObject(o interface{}, object interface{}, err *error) interface{} {
rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
if rv.Kind() == reflect.Ptr {
if rv.IsNil() {
// This allows ReadJSONObject() to return a nil pointer
// if the value read is nil.
rvPtr := reflect.New(rt)
ReadJSONObjectPtr(rvPtr.Interface(), object, err)
return rvPtr.Elem().Interface()
} else {
readReflectJSON(rv, rt, Options{}, object, err)
return o
}
} else {
ptrRv := reflect.New(rt)
readReflectJSON(ptrRv.Elem(), rt, Options{}, object, err)
return ptrRv.Elem().Interface()
}
}
func ReadJSONObjectPtr(o interface{}, object interface{}, err *error) interface{} {
rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
if rv.Kind() == reflect.Ptr {
readReflectJSON(rv.Elem(), rt.Elem(), Options{}, object, err)
} else {
cmn.PanicSanity("ReadJSON(Object)Ptr expects o to be a pointer")
}
return o
}
func WriteJSON(o interface{}, w io.Writer, n *int, err *error) {
rv := reflect.ValueOf(o)
rt := reflect.TypeOf(o)
if rv.Kind() == reflect.Ptr {
rv, rt = rv.Elem(), rt.Elem()
}
writeReflectJSON(rv, rt, Options{}, w, n, err)
}
// Write all of bz to w
// Increment n and set err accordingly.
func WriteTo(bz []byte, w io.Writer, n *int, err *error) {
if *err != nil {
return
}
n_, err_ := w.Write(bz)
*n += n_
*err = err_
}
// Read len(buf) from r
// Increment n and set err accordingly.
func ReadFull(buf []byte, r io.Reader, n *int, err *error) {
if *err != nil {
return
}
n_, err_ := io.ReadFull(r, buf)
*n += n_
*err = err_
}