Skip to content

Commit 02ed82e

Browse files
committed
Fixed some lint errors; renamed util functions
1 parent 9cbee05 commit 02ed82e

File tree

6 files changed

+45
-51
lines changed

6 files changed

+45
-51
lines changed

schemer.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -510,24 +510,25 @@ func DecodeSchemaJSON(r io.Reader) (Schema, error) {
510510
}
511511

512512
return s, nil
513-
} else {
514-
s := &VarArraySchema{}
515-
s.SetNullable(nullable)
513+
}
516514

517-
// process the array element
518-
tmp, err := json.Marshal(fields["element"])
519-
if err != nil {
520-
return nil, err
521-
}
515+
// array length not present
516+
s := &VarArraySchema{}
517+
s.SetNullable(nullable)
522518

523-
s.Element, err = DecodeSchemaJSON(bytes.NewReader(tmp))
524-
if err != nil {
525-
return nil, err
526-
}
519+
// process the array element
520+
tmp, err := json.Marshal(fields["element"])
521+
if err != nil {
522+
return nil, err
523+
}
527524

528-
return s, nil
525+
s.Element, err = DecodeSchemaJSON(bytes.NewReader(tmp))
526+
if err != nil {
527+
return nil, err
529528
}
530529

530+
return s, nil
531+
531532
case "object":
532533
fieldsI, ok := fields["fields"]
533534

@@ -849,10 +850,9 @@ func PreEncode(w io.Writer, v *reflect.Value, nullable bool) (bool, error) {
849850
// 1 indicates null
850851
w.Write([]byte{1})
851852
return true, nil
852-
} else {
853-
// 0 indicates not null
854-
w.Write([]byte{0})
855853
}
854+
// 0 indicates not null
855+
w.Write([]byte{0})
856856
} else if isNil {
857857
return false, fmt.Errorf("cannot encode nil value: schema is not nullable")
858858
}
@@ -894,9 +894,8 @@ func PreDecode(r io.Reader, v *reflect.Value, nullable bool) (bool, error) {
894894
return true, nil
895895
}
896896
return false, fmt.Errorf("destination not settable")
897-
} else {
898-
return false, fmt.Errorf("cannot decode null value to a %s", v.Kind())
899897
}
898+
return false, fmt.Errorf("cannot decode null value to a %s", v.Kind())
900899
}
901900
}
902901

util.go

+18-23
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,40 @@ func (r byter) ReadVarint() (int64, error) {
3131
return binary.ReadVarint(r)
3232
}
3333

34-
func VarIntFromIOReader(r io.Reader) (int64, error) {
35-
b := &byter{r}
36-
return binary.ReadVarint(b)
37-
}
38-
39-
// ReadVarUint() returns an uint64 by reading from r
40-
func ReadVarUint(r io.Reader) (uint64, error) {
34+
// ReadUvarint reads an Uvarint from r one byte at a time
35+
func ReadUvarint(r io.Reader) (uint64, error) {
4136

37+
rb := byter{r}
4238
buf := make([]byte, binary.MaxVarintLen64)
43-
counter := 0
4439

45-
// read one byte at a a time
46-
for {
47-
b := make([]byte, 1)
48-
_, err := io.ReadAtLeast(r, b, 1)
40+
// Read first byte into `buf`
41+
b, err := rb.ReadByte()
42+
if err != nil {
43+
return 0, err
44+
}
45+
buf[0] = b
46+
47+
// Read subsequent bytes into `buf`
48+
i := 1
49+
for ; b&0x80 > 0; i++ {
50+
b, err = rb.ReadByte()
4951
if err != nil {
5052
return 0, err
5153
}
52-
buf[counter] = b[0]
53-
54-
// keep reading out bytes until no more data
55-
if b[0]&128 != 128 {
56-
break
57-
}
58-
59-
counter++
54+
buf[i] = b
6055
}
6156

6257
decodedUInt, n := binary.Uvarint(buf)
63-
if n != counter+1 {
58+
if n != i {
6459
return 0, fmt.Errorf("uvarint did not consume expected number of bytes")
6560
}
6661

6762
return decodedUInt, nil
6863

6964
}
7065

71-
// WriteVarUint() writes a 64 bit unsigned integer to w
72-
func WriteVarUint(w io.Writer, v uint64) error {
66+
// WriteUvarint writes v to w as an Uvarint
67+
func WriteUvarint(w io.Writer, v uint64) error {
7368

7469
buf := make([]byte, binary.MaxVarintLen64)
7570
varIntBytes := binary.PutUvarint(buf, v)

vararray.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (s *VarArraySchema) EncodeValue(w io.Writer, v reflect.Value) error {
9191
return fmt.Errorf("VarArraySchema can only encode slices")
9292
}
9393

94-
err = WriteVarUint(w, uint64(v.Len()))
94+
err = WriteUvarint(w, uint64(v.Len()))
9595
if err != nil {
9696
return errors.New("cannot encode var string length as var int")
9797
}
@@ -137,7 +137,7 @@ func (s *VarArraySchema) DecodeValue(r io.Reader, v reflect.Value) error {
137137
return fmt.Errorf("VarArraySchema can only decode to slices")
138138
}
139139

140-
expectedLen, err := ReadVarUint(r)
140+
expectedLen, err := ReadUvarint(r)
141141
if err != nil {
142142
return err
143143
}

varint.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (s *VarIntSchema) EncodeValue(w io.Writer, v reflect.Value) error {
9191
if intVal < 0 {
9292
uintVal = ^uintVal
9393
}
94-
return WriteVarUint(w, uintVal)
94+
return WriteUvarint(w, uintVal)
9595
case reflect.Uint:
9696
fallthrough
9797
case reflect.Uint8:
@@ -102,7 +102,7 @@ func (s *VarIntSchema) EncodeValue(w io.Writer, v reflect.Value) error {
102102
fallthrough
103103
case reflect.Uint64:
104104
uintVal := v.Uint()
105-
return WriteVarUint(w, uintVal)
105+
return WriteUvarint(w, uintVal)
106106
}
107107

108108
return nil
@@ -137,7 +137,7 @@ func (s *VarIntSchema) DecodeValue(r io.Reader, v reflect.Value) error {
137137

138138
// Decode value
139139
if s.Signed {
140-
uintVal, err := ReadVarUint(r)
140+
uintVal, err := ReadUvarint(r)
141141
if err != nil {
142142
return err
143143
}
@@ -220,7 +220,7 @@ func (s *VarIntSchema) DecodeValue(r io.Reader, v reflect.Value) error {
220220
}
221221
} else {
222222
// Unsigned
223-
uintVal, err := ReadVarUint(r)
223+
uintVal, err := ReadUvarint(r)
224224
if err != nil {
225225
return err
226226
}

varobject.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (s *VarObjectSchema) EncodeValue(w io.Writer, v reflect.Value) error {
126126
return fmt.Errorf("varObjectSchema can only encode maps")
127127
}
128128

129-
err = WriteVarUint(w, uint64(v.Len()))
129+
err = WriteUvarint(w, uint64(v.Len()))
130130
if err != nil {
131131
return errors.New("cannot encode var string length as var int")
132132
}
@@ -182,7 +182,7 @@ func (s *VarObjectSchema) DecodeValue(r io.Reader, v reflect.Value) error {
182182

183183
// we wrote the number of entries in the map as a var int
184184
// when we did the encoding
185-
expectedNumEntries, err := ReadVarUint(r)
185+
expectedNumEntries, err := ReadUvarint(r)
186186
if err != nil {
187187
return err
188188
}

varstring.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (s *VarStringSchema) EncodeValue(w io.Writer, v reflect.Value) error {
7777
var stringToEncode string = v.String()
7878
var stringLen int = len(stringToEncode)
7979

80-
err = WriteVarUint(w, uint64(stringLen))
80+
err = WriteUvarint(w, uint64(stringLen))
8181
if err != nil {
8282
return errors.New("cannot encode var string length as var int")
8383
}
@@ -117,7 +117,7 @@ func (s *VarStringSchema) DecodeValue(r io.Reader, v reflect.Value) error {
117117
k = t.Kind()
118118
}
119119

120-
expectedLen, err := ReadVarUint(r)
120+
expectedLen, err := ReadUvarint(r)
121121
if err != nil {
122122
return err
123123
}

0 commit comments

Comments
 (0)