Skip to content

Commit f18495b

Browse files
committedOct 11, 2021
More cleanup (changed PreEncode / PreDecode signature)
1 parent b58cf9b commit f18495b

14 files changed

+143
-228
lines changed
 

‎bool.go

+43-44
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,24 @@ import (
88
"reflect"
99
)
1010

11+
// BoolSchema is a Schema for encoding and decoding boolean values
1112
type BoolSchema struct {
1213
SchemaOptions
1314
}
1415

15-
func (s *BoolSchema) GoType() reflect.Type {
16-
var b bool
17-
retval := reflect.TypeOf(b)
18-
19-
if s.Nullable() {
20-
retval = reflect.PtrTo(retval)
21-
}
22-
return retval
23-
}
24-
25-
func (s *BoolSchema) MarshalJSON() ([]byte, error) {
26-
return json.Marshal(map[string]interface{}{
27-
"type": "bool",
28-
"nullable": s.Nullable(),
29-
})
30-
}
31-
32-
// Bytes encodes the schema in a portable binary format
33-
func (s *BoolSchema) MarshalSchemer() ([]byte, error) {
34-
// bool schemas are 1 byte long
35-
var schema []byte = []byte{BoolByte}
36-
37-
// The most significant bit indicates whether or not the type is nullable
38-
if s.Nullable() {
39-
schema[0] |= NullMask
40-
}
41-
42-
return schema, nil
43-
}
44-
4516
// Encode uses the schema to write the encoded value of i to the output stream
4617
func (s *BoolSchema) Encode(w io.Writer, i interface{}) error {
4718
return s.EncodeValue(w, reflect.ValueOf(i))
4819
}
4920

50-
// EncodeValue uses the schema to write the encoded value of v to the output stream
21+
// EncodeValue uses the schema to write the encoded value of v to the output
22+
// stream
5123
func (s *BoolSchema) EncodeValue(w io.Writer, v reflect.Value) error {
5224

53-
ok, err := PreEncode(s.Nullable(), w, &v)
54-
if err != nil {
25+
done, err := PreEncode(w, &v, s.Nullable())
26+
if err != nil || done {
5527
return err
5628
}
57-
if !ok {
58-
return nil
59-
}
6029

6130
t := v.Type()
6231
k := t.Kind()
@@ -86,25 +55,23 @@ func (s *BoolSchema) EncodeValue(w io.Writer, v reflect.Value) error {
8655
return nil
8756
}
8857

89-
// Decode uses the schema to read the next encoded value from the input stream and store it in i
58+
// Decode uses the schema to read the next encoded value from the input
59+
// stream and stores it in i
9060
func (s *BoolSchema) Decode(r io.Reader, i interface{}) error {
9161
if i == nil {
9262
return fmt.Errorf("cannot decode to nil destination")
9363
}
9464
return s.DecodeValue(r, reflect.ValueOf(i))
9565
}
9666

97-
// DecodeValue uses the schema to read the next encoded value from the input stream and store it in v
67+
// DecodeValue uses the schema to read the next encoded value from the input
68+
// stream and stores it in v
9869
func (s *BoolSchema) DecodeValue(r io.Reader, v reflect.Value) error {
9970

100-
v, err := PreDecode(s.Nullable(), r, v)
101-
if err != nil {
71+
done, err := PreDecode(r, &v, s.Nullable())
72+
if err != nil || done {
10273
return err
10374
}
104-
// if PreDecode() returns a zero value for v, it means we are done decoding
105-
if !(v.IsValid()) {
106-
return nil
107-
}
10875

10976
t := v.Type()
11077
k := t.Kind()
@@ -190,3 +157,35 @@ func (s *BoolSchema) DecodeValue(r io.Reader, v reflect.Value) error {
190157

191158
return nil
192159
}
160+
161+
// GoType returns the default Go type that represents the schema
162+
func (s *BoolSchema) GoType() reflect.Type {
163+
var b bool
164+
retval := reflect.TypeOf(b)
165+
166+
if s.Nullable() {
167+
retval = reflect.PtrTo(retval)
168+
}
169+
return retval
170+
}
171+
172+
// MarshalJSON encodes the schema in a JSON format
173+
func (s *BoolSchema) MarshalJSON() ([]byte, error) {
174+
return json.Marshal(map[string]interface{}{
175+
"type": "bool",
176+
"nullable": s.Nullable(),
177+
})
178+
}
179+
180+
// MarshalSchemer encodes the schema in a portable binary format
181+
func (s *BoolSchema) MarshalSchemer() ([]byte, error) {
182+
// bool schemas are 1 byte long
183+
var schema []byte = []byte{BoolByte}
184+
185+
// The most significant bit indicates whether or not the type is nullable
186+
if s.Nullable() {
187+
schema[0] |= NullMask
188+
}
189+
190+
return schema, nil
191+
}

‎complex.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,10 @@ func (s *ComplexSchema) EncodeValue(w io.Writer, v reflect.Value) error {
8484
return fmt.Errorf("cannot encode using invalid ComplexNumber schema")
8585
}
8686

87-
ok, err := PreEncode(s.Nullable(), w, &v)
88-
if err != nil {
87+
done, err := PreEncode(w, &v, s.Nullable())
88+
if err != nil || done {
8989
return err
9090
}
91-
if !ok {
92-
return nil
93-
}
9491

9592
t := v.Type()
9693
k := t.Kind()
@@ -171,16 +168,11 @@ func (s *ComplexSchema) DecodeValue(r io.Reader, v reflect.Value) error {
171168
return fmt.Errorf("cannot decode using invalid ComplexNumber schema")
172169
}
173170

174-
v, err := PreDecode(s.Nullable(), r, v)
175-
if err != nil {
171+
done, err := PreDecode(r, &v, s.Nullable())
172+
if err != nil || done {
176173
return err
177174
}
178175

179-
// if PreDecode() returns a zero value for v, it means we are done decoding
180-
if !(v.IsValid()) {
181-
return nil
182-
}
183-
184176
t := v.Type()
185177
k := t.Kind()
186178

‎date.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,10 @@ func (s *DateSchema) Encode(w io.Writer, i interface{}) error {
149149
// EncodeValue uses the schema to write the encoded value of he output stream
150150
func (s *DateSchema) EncodeValue(w io.Writer, v reflect.Value) error {
151151

152-
ok, err := PreEncode(s.Nullable(), w, &v)
153-
if err != nil {
152+
done, err := PreEncode(w, &v, s.Nullable())
153+
if err != nil || done {
154154
return err
155155
}
156-
if !ok {
157-
return nil
158-
}
159156

160157
t := v.Type()
161158
k := t.Kind()
@@ -194,14 +191,10 @@ func (s *DateSchema) Decode(r io.Reader, i interface{}) error {
194191
// DecodeValue uses the schema to read the next encoded valuethe input stream and store it in v
195192
func (s *DateSchema) DecodeValue(r io.Reader, v reflect.Value) error {
196193

197-
v, err := PreDecode(s.Nullable(), r, v)
198-
if err != nil {
194+
done, err := PreDecode(r, &v, s.Nullable())
195+
if err != nil || done {
199196
return err
200197
}
201-
// if PreDecode() returns a zero value for v, it means we are done decoding
202-
if !(v.IsValid()) {
203-
return nil
204-
}
205198

206199
t := v.Type()
207200
k := t.Kind()

‎fixedarray.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,10 @@ func (s *FixedArraySchema) EncodeValue(w io.Writer, v reflect.Value) error {
109109
return fmt.Errorf("cannot encode using invalid FixedArraySchema schema")
110110
}
111111

112-
ok, err := PreEncode(s.Nullable(), w, &v)
113-
if err != nil {
112+
done, err := PreEncode(w, &v, s.Nullable())
113+
if err != nil || done {
114114
return err
115115
}
116-
if !ok {
117-
return nil
118-
}
119116

120117
t := v.Type()
121118
k := t.Kind()
@@ -151,16 +148,11 @@ func (s *FixedArraySchema) DecodeValue(r io.Reader, v reflect.Value) error {
151148
return fmt.Errorf("cannot decode using invalid FixedArraySchema schema")
152149
}
153150

154-
v, err := PreDecode(s.Nullable(), r, v)
155-
if err != nil {
151+
done, err := PreDecode(r, &v, s.Nullable())
152+
if err != nil || done {
156153
return err
157154
}
158155

159-
// if PreDecode() returns a zero value for v, it means we are done decoding
160-
if !(v.IsValid()) {
161-
return nil
162-
}
163-
164156
t := v.Type()
165157
k := t.Kind()
166158

‎fixedint.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,10 @@ func (s *FixedIntSchema) EncodeValue(w io.Writer, v reflect.Value) error {
255255
return fmt.Errorf("cannot encode using invalid FixedIntSchema schema")
256256
}
257257

258-
ok, err := PreEncode(s.Nullable(), w, &v)
259-
if err != nil {
258+
done, err := PreEncode(w, &v, s.Nullable())
259+
if err != nil || done {
260260
return err
261261
}
262-
if !ok {
263-
return nil
264-
}
265262

266263
t := v.Type()
267264
k := t.Kind()
@@ -341,14 +338,10 @@ func (s *FixedIntSchema) DecodeValue(r io.Reader, v reflect.Value) error {
341338
return fmt.Errorf("cannot decode using invalid FixedIntSchema schema")
342339
}
343340

344-
v, err := PreDecode(s.Nullable(), r, v)
345-
if err != nil {
341+
done, err := PreDecode(r, &v, s.Nullable())
342+
if err != nil || done {
346343
return err
347344
}
348-
// if PreDecode() returns a zero value for v, it means we are done decoding
349-
if !(v.IsValid()) {
350-
return nil
351-
}
352345

353346
t := v.Type()
354347
k := t.Kind()

‎fixedobject.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,10 @@ func (s *FixedObjectSchema) Encode(w io.Writer, i interface{}) error {
127127
// EncodeValue uses the schema to write the encoded value of v to the output stream
128128
func (s *FixedObjectSchema) EncodeValue(w io.Writer, v reflect.Value) error {
129129

130-
ok, err := PreEncode(s.Nullable(), w, &v)
131-
if err != nil {
130+
done, err := PreEncode(w, &v, s.Nullable())
131+
if err != nil || done {
132132
return err
133133
}
134-
if !ok {
135-
return nil
136-
}
137134

138135
t := v.Type()
139136
k := t.Kind()
@@ -190,14 +187,10 @@ func (s *FixedObjectSchema) Decode(r io.Reader, i interface{}) error {
190187
// DecodeValue uses the schema to read the next encoded value from the input stream and store it in v
191188
func (s *FixedObjectSchema) DecodeValue(r io.Reader, v reflect.Value) error {
192189

193-
v, err := PreDecode(s.Nullable(), r, v)
194-
if err != nil {
190+
done, err := PreDecode(r, &v, s.Nullable())
191+
if err != nil || done {
195192
return err
196193
}
197-
// if PreDecode() returns a zero value for v, it means we are done decoding
198-
if !(v.IsValid()) {
199-
return nil
200-
}
201194

202195
t := v.Type()
203196
k := t.Kind()

‎fixedstring.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,10 @@ func (s *FixedStringSchema) EncodeValue(w io.Writer, v reflect.Value) error {
8080
return fmt.Errorf("cannot encode using invalid FixedStringSchema")
8181
}
8282

83-
ok, err := PreEncode(s.Nullable(), w, &v)
84-
if err != nil {
83+
done, err := PreEncode(w, &v, s.Nullable())
84+
if err != nil || done {
8585
return err
8686
}
87-
if !ok {
88-
return nil
89-
}
9087

9188
t := v.Type()
9289
k := t.Kind()
@@ -126,14 +123,10 @@ func (s *FixedStringSchema) DecodeValue(r io.Reader, v reflect.Value) error {
126123
return fmt.Errorf("cannot decode using invalid FixedStringSchema")
127124
}
128125

129-
v, err := PreDecode(s.Nullable(), r, v)
130-
if err != nil {
126+
done, err := PreDecode(r, &v, s.Nullable())
127+
if err != nil || done {
131128
return err
132129
}
133-
// if PreDecode() returns a zero value for v, it means we are done decoding
134-
if !(v.IsValid()) {
135-
return nil
136-
}
137130

138131
t := v.Type()
139132
k := t.Kind()

‎float.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,10 @@ func (s *FloatSchema) EncodeValue(w io.Writer, v reflect.Value) error {
8282
return fmt.Errorf("cannot encode using invalid StringSchema schema")
8383
}
8484

85-
ok, err := PreEncode(s.Nullable(), w, &v)
86-
if err != nil {
85+
done, err := PreEncode(w, &v, s.Nullable())
86+
if err != nil || done {
8787
return err
8888
}
89-
if !ok {
90-
return nil
91-
}
9289

9390
t := v.Type()
9491
k := t.Kind()
@@ -157,14 +154,10 @@ func (s *FloatSchema) DecodeValue(r io.Reader, v reflect.Value) error {
157154
return fmt.Errorf("cannot decode using invalid floating point schema")
158155
}
159156

160-
v, err := PreDecode(s.Nullable(), r, v)
161-
if err != nil {
157+
done, err := PreDecode(r, &v, s.Nullable())
158+
if err != nil || done {
162159
return err
163160
}
164-
// if PreDecode() returns a zero value for v, it means we are done decoding
165-
if !(v.IsValid()) {
166-
return nil
167-
}
168161

169162
t := v.Type()
170163
k := t.Kind()

‎ipv4.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,10 @@ func (s *ipv4Schema) Encode(w io.Writer, i interface{}) error {
157157
// EncodeValue uses the schema to write the encoded value of he output stream
158158
func (s *ipv4Schema) EncodeValue(w io.Writer, v reflect.Value) error {
159159

160-
ok, err := PreEncode(s.Nullable(), w, &v)
161-
if err != nil {
160+
done, err := PreEncode(w, &v, s.Nullable())
161+
if err != nil || done {
162162
return err
163163
}
164-
if !ok {
165-
return nil
166-
}
167164

168165
t := v.Type()
169166
k := t.Kind()
@@ -204,14 +201,10 @@ func (s *ipv4Schema) Decode(r io.Reader, i interface{}) error {
204201
// DecodeValue uses the schema to read the next encoded valuethe input stream and store it in v
205202
func (s *ipv4Schema) DecodeValue(r io.Reader, v reflect.Value) error {
206203

207-
v, err := PreDecode(s.Nullable(), r, v)
208-
if err != nil {
204+
done, err := PreDecode(r, &v, s.Nullable())
205+
if err != nil || done {
209206
return err
210207
}
211-
// if PreDecode() returns a zero value for v, it means we are done decoding
212-
if !(v.IsValid()) {
213-
return nil
214-
}
215208

216209
t := v.Type()
217210
k := t.Kind()

‎schemer.go

+52-50
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package schemer
22

3-
// TODO:
4-
// -> type TypeByte byte
5-
// - -> model decode after decodeJSON
6-
// - -> squeze custom type into 6 bits , based on reserved type (CustomMask to be 0x40)
7-
//
8-
93
import (
104
"bytes"
115
"encoding/binary"
@@ -17,6 +11,12 @@ import (
1711
"strings"
1812
)
1913

14+
// initialization function for the Schemer Library
15+
func init() {
16+
Register(dateSchemaGenerator{})
17+
Register(ipv4SchemaGenerator{})
18+
}
19+
2020
// Schema is an interface that encodes and decodes data of a specific type
2121
type Schema interface {
2222
// Encode uses the schema to write the encoded value of i to the output
@@ -47,7 +47,7 @@ type Marshaler interface {
4747

4848
// SchemaGenerator is an interface implemented by custom schema generators.
4949
// When Register is called on a SchemaGenerator, the global SchemaOf,
50-
// decodeSchema, and DecodeSchemaJSON functions will call the identically
50+
// DecodeSchema, and DecodeSchemaJSON functions will call the identically
5151
// named method on each schema generator to determine if a custom schema should
5252
// be returned.
5353
// If a SchemaGenerator cannot return a Schema for a specific type, it should
@@ -127,7 +127,9 @@ func SchemaOfType(t reflect.Type) (Schema, error) {
127127
nullable := false
128128

129129
// Dereference pointer / interface types
130-
for k := t.Kind(); k == reflect.Ptr || k == reflect.Interface; k = t.Kind() {
130+
for k := t.Kind(); k == reflect.Ptr ||
131+
k == reflect.Interface; k = t.Kind() {
132+
131133
t = t.Elem()
132134

133135
// If we encounter any pointers, then we know this type is nullable
@@ -270,13 +272,16 @@ func SchemaOfType(t reflect.Type) (Schema, error) {
270272

271273
// Note: only override option if explicitly set in the tag
272274
if tagOpts.NullableSet {
275+
// Note: Most schemas implement SetNullable(bool), but Schema
276+
// does not require it; we must check here
273277
if opt, ok := of.Schema.(interface {
274278
SetNullable(bool)
275279
}); ok {
276280
opt.SetNullable(tagOpts.Nullable)
277281
}
278282
}
279283
if tagOpts.WeakDecodingSet {
284+
// Note: Most schemas implement SetWeakDecoding(bool)
280285
if opt, ok := of.Schema.(interface {
281286
SetWeakDecoding(bool)
282287
}); ok {
@@ -294,6 +299,7 @@ func SchemaOfType(t reflect.Type) (Schema, error) {
294299
}
295300

296301
// DecodeSchemaJSON takes a buffer of JSON data and parses it to create a schema
302+
// The input stream r is read in its entirety before the JSON is decoded.
297303
func DecodeSchemaJSON(r io.Reader) (Schema, error) {
298304

299305
buf, err := io.ReadAll(r)
@@ -873,72 +879,74 @@ func DecodeSchema(r io.Reader) (Schema, error) {
873879
return nil, fmt.Errorf("invalid binary schema encountered")
874880
}
875881

876-
// PreEncode should be called by each Schema's Encode() routine.
877-
// It handles dereferencing pointerss and interfaces, and for writing
878-
// a byte to indicate nullable if the schema indicates it is in fact
879-
// nullable.
880-
// If this routine returns false, no more processing is needed on the
881-
// encoder who called this routine.
882-
func PreEncode(nullable bool, w io.Writer, v *reflect.Value) (bool, error) {
882+
// PreEncode is a helper function that should be called by each Schema's Encode
883+
// routine. It dereferences v if the value is a pointer or interface type and
884+
// writes the null byte if nullable is set.
885+
// If nullable is false and v resolves to nil, an error is returned.
886+
// If nullable is true and v resolves to nil, (true, nil) is returned,
887+
// indicating that no further processing is needed by the encoder who called
888+
// this routine. Otherwise, false, nil is returned.
889+
func PreEncode(w io.Writer, v *reflect.Value, nullable bool) (bool, error) {
883890
// Dereference pointer / interface types
884891
for k := v.Kind(); k == reflect.Ptr || k == reflect.Interface; k = v.Kind() {
885892
*v = v.Elem()
886893
}
887894

895+
// Note: v.Elem() returns invalid Value if v is nil
896+
isNil := !v.IsValid()
897+
888898
if nullable {
889-
// did the caller pass in a nil value, or a null pointer?
890-
if !v.IsValid() {
891-
// per the revised spec, 1 indicates null
899+
if isNil {
900+
// 1 indicates null
892901
w.Write([]byte{1})
893-
return false, nil
902+
return true, nil
894903
} else {
895904
// 0 indicates not null
896905
w.Write([]byte{0})
897906
}
898-
} else {
899-
// if nullable is false
900-
// but they are trying to encode a nil value.. then that is an error
901-
if !v.IsValid() {
902-
return false, fmt.Errorf("cannot enoded nil value when IsNullable is false")
903-
}
907+
} else if isNil {
908+
return false, fmt.Errorf("cannot encode nil value: schema is not nullable")
904909
}
905910

906-
return true, nil
911+
return false, nil
907912
}
908913

909-
// PreDecode() is called before each Decode() routine from all the schemas. This routine
910-
// handles checking on the nullable flag if the schema indicates the schema
911-
// is nullable.
912-
// This routine also handles derefering pointers and interfaces, and returns
913-
// the new value of v after it is set.
914-
func PreDecode(nullable bool, r io.Reader, v reflect.Value) (reflect.Value, error) {
915-
// if t is a ptr or interface type, remove exactly ONE level of indirection
914+
// PreDecode is a helper function that should be called by each Schema's Decode
915+
// routine. It removes exactly one level of indirection for v and reads the
916+
// null byte if nullable is set. If a null value is read, (true, nil) is
917+
// returned, indicating that no further processing is needed by the decoder who
918+
// called this routine. This routine also ensures that the destination value is
919+
// settable and returns errors if not. Finally, this routine populates nested
920+
// pointer values recursively, as needed.
921+
func PreDecode(r io.Reader, v *reflect.Value, nullable bool) (bool, error) {
922+
// if v is a pointer or interface type, remove exactly ONE level of indirection
916923
if k := v.Kind(); !v.CanSet() && (k == reflect.Ptr || k == reflect.Interface) {
917-
v = v.Elem()
924+
*v = v.Elem()
918925
}
919926

920927
// if the data indicates this type is nullable, then the actual
921-
// value is preceeded by one byte [which indicates if the encoder encoded a nill value]
928+
// value is preceded by the null byte
929+
// (which indicates if the encoded value is null)
922930
if nullable {
923931
buf := make([]byte, 1)
924932

925933
// first byte indicates whether value is null or not...
926934
_, err := io.ReadAtLeast(r, buf, 1)
927935
if err != nil {
928-
return reflect.Value{}, err
936+
return false, err
929937
}
930-
valueIsNull := (buf[0] == 1)
938+
isNull := (buf[0] == 1)
931939

932-
if valueIsNull {
940+
if isNull {
933941
if v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
934942
if v.CanSet() {
935943
// special way to set pointer to nil value
936944
v.Set(reflect.Zero(v.Type()))
937-
return reflect.Value{}, nil
945+
return true, nil
938946
}
939-
return reflect.Value{}, fmt.Errorf("destination not settable")
947+
return false, fmt.Errorf("destination not settable")
940948
} else {
941-
return reflect.Value{}, fmt.Errorf("cannot decode null value to non pointer to pointer type")
949+
return false, fmt.Errorf("cannot decode null value to a %s", v.Kind())
942950
}
943951
}
944952
}
@@ -950,18 +958,12 @@ func PreDecode(nullable bool, r io.Reader, v reflect.Value) (reflect.Value, erro
950958
break
951959
}
952960
if !v.CanSet() {
953-
return reflect.Value{}, fmt.Errorf("decode destination is not settable")
961+
return false, fmt.Errorf("destination not settable")
954962
}
955963
v.Set(reflect.New(v.Type().Elem()))
956964
}
957-
v = v.Elem()
965+
*v = v.Elem()
958966
}
959967

960-
return v, nil
961-
}
962-
963-
// initialization function for the Schemer Library
964-
func init() {
965-
Register(dateSchemaGenerator{})
966-
Register(ipv4SchemaGenerator{})
968+
return false, nil
967969
}

‎vararray.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,10 @@ func (s *VarArraySchema) Encode(w io.Writer, i interface{}) error {
7979
// EncodeValue uses the schema to write the encoded value of v to the output stream
8080
func (s *VarArraySchema) EncodeValue(w io.Writer, v reflect.Value) error {
8181

82-
ok, err := PreEncode(s.Nullable(), w, &v)
83-
if err != nil {
82+
done, err := PreEncode(w, &v, s.Nullable())
83+
if err != nil || done {
8484
return err
8585
}
86-
if !ok {
87-
return nil
88-
}
8986

9087
t := v.Type()
9188
k := t.Kind()
@@ -120,14 +117,10 @@ func (s *VarArraySchema) Decode(r io.Reader, i interface{}) error {
120117
// DecodeValue uses the schema to read the next encoded value from the input stream and store it in v
121118
func (s *VarArraySchema) DecodeValue(r io.Reader, v reflect.Value) error {
122119

123-
v, err := PreDecode(s.Nullable(), r, v)
124-
if err != nil {
120+
done, err := PreDecode(r, &v, s.Nullable())
121+
if err != nil || done {
125122
return err
126123
}
127-
// if PreDecode() returns a zero value for v, it means we are done decoding
128-
if !(v.IsValid()) {
129-
return nil
130-
}
131124

132125
t := v.Type()
133126
k := t.Kind()

‎varint.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,10 @@ func (s *VarIntSchema) Encode(w io.Writer, i interface{}) error {
6767
// EncodeValue uses the schema to write the encoded value of v to the output stream
6868
func (s *VarIntSchema) EncodeValue(w io.Writer, v reflect.Value) error {
6969

70-
ok, err := PreEncode(s.Nullable(), w, &v)
71-
if err != nil {
70+
done, err := PreEncode(w, &v, s.Nullable())
71+
if err != nil || done {
7272
return err
7373
}
74-
if !ok {
75-
return nil
76-
}
7774

7875
t := v.Type()
7976
k := t.Kind()
@@ -122,14 +119,10 @@ func (s *VarIntSchema) Decode(r io.Reader, i interface{}) error {
122119
// DecodeValue uses the schema to read the next encoded value from the input stream and store it in v
123120
func (s *VarIntSchema) DecodeValue(r io.Reader, v reflect.Value) error {
124121

125-
v, err := PreDecode(s.Nullable(), r, v)
126-
if err != nil {
122+
done, err := PreDecode(r, &v, s.Nullable())
123+
if err != nil || done {
127124
return err
128125
}
129-
// if PreDecode() returns a zero value for v, it means we are done decoding
130-
if !(v.IsValid()) {
131-
return nil
132-
}
133126

134127
t := v.Type()
135128
k := t.Kind()

‎varobject.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,10 @@ func (s *VarObjectSchema) Encode(w io.Writer, i interface{}) error {
114114
// EncodeValue uses the schema to write the encoded value of v to the output streamm
115115
func (s *VarObjectSchema) EncodeValue(w io.Writer, v reflect.Value) error {
116116

117-
ok, err := PreEncode(s.Nullable(), w, &v)
118-
if err != nil {
117+
done, err := PreEncode(w, &v, s.Nullable())
118+
if err != nil || done {
119119
return err
120120
}
121-
if !ok {
122-
return nil
123-
}
124121

125122
t := v.Type()
126123
k := t.Kind()
@@ -161,14 +158,10 @@ func (s *VarObjectSchema) Decode(r io.Reader, i interface{}) error {
161158
// DecodeValue uses the schema to read the next encoded value from the input stream and store it in v
162159
func (s *VarObjectSchema) DecodeValue(r io.Reader, v reflect.Value) error {
163160

164-
v, err := PreDecode(s.Nullable(), r, v)
165-
if err != nil {
161+
done, err := PreDecode(r, &v, s.Nullable())
162+
if err != nil || done {
166163
return err
167164
}
168-
// if PreDecode() returns a zero value for v, it means we are done decoding
169-
if !(v.IsValid()) {
170-
return nil
171-
}
172165

173166
t := v.Type()
174167
k := t.Kind()

‎varstring.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ func (s *VarStringSchema) Encode(w io.Writer, i interface{}) error {
5454
// EncodeValue uses the schema to write the encoded value of v to the output stream
5555
func (s *VarStringSchema) EncodeValue(w io.Writer, v reflect.Value) error {
5656

57-
ok, err := PreEncode(s.Nullable(), w, &v)
58-
if err != nil {
57+
done, err := PreEncode(w, &v, s.Nullable())
58+
if err != nil || done {
5959
return err
6060
}
61-
if !ok {
62-
return nil
63-
}
6461

6562
t := v.Type()
6663
k := t.Kind()
@@ -104,14 +101,10 @@ func (s *VarStringSchema) Decode(r io.Reader, i interface{}) error {
104101
// DecodeValue uses the schema to read the next encoded value from the input stream and store it in v
105102
func (s *VarStringSchema) DecodeValue(r io.Reader, v reflect.Value) error {
106103

107-
v, err := PreDecode(s.Nullable(), r, v)
108-
if err != nil {
104+
done, err := PreDecode(r, &v, s.Nullable())
105+
if err != nil || done {
109106
return err
110107
}
111-
// if PreDecode() returns a zero value for v, it means we are done decoding
112-
if !(v.IsValid()) {
113-
return nil
114-
}
115108

116109
t := v.Type()
117110
k := t.Kind()

0 commit comments

Comments
 (0)
Please sign in to comment.