Skip to content

Commit 06e4794

Browse files
committed
Cleaned up binary schema encoding/decoding
Clarified bit masks
1 parent 87dee6b commit 06e4794

18 files changed

+188
-180
lines changed

bool.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ func (s *BoolSchema) MarshalJSON() ([]byte, error) {
3232
// Bytes encodes the schema in a portable binary format
3333
func (s *BoolSchema) MarshalSchemer() ([]byte, error) {
3434
// bool schemas are 1 byte long
35-
var schema []byte = []byte{BoolSchemaMask}
35+
var schema []byte = []byte{BoolByte}
3636

37-
// The most signifiant bit indicates whether or not the type is nullable
37+
// The most significant bit indicates whether or not the type is nullable
3838
if s.Nullable() {
39-
schema[0] |= 0x80
39+
schema[0] |= NullMask
4040
}
4141

4242
return schema, nil

complex.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ func (s *ComplexSchema) MarshalJSON() ([]byte, error) {
5353
func (s *ComplexSchema) MarshalSchemer() ([]byte, error) {
5454

5555
// floating point schemas are 1 byte long
56-
var schema []byte = []byte{ComplexSchemaMask}
56+
var schema []byte = []byte{ComplexByte}
5757

5858
// bit 8 indicates whether or not the type is nullable
5959
if s.Nullable() {
60-
schema[0] |= 0x80
60+
schema[0] |= NullMask
6161
}
6262

6363
// bit 1 = complex number size in (64 << n) bits

date.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ func (sg dateSchemaGenerator) DecodeSchema(r io.Reader) (Schema, error) {
4646
return nil, err
4747
}
4848

49-
if tmpBuf[0]&CustomSchemaMask == CustomSchemaMask {
49+
if tmpBuf[0]&CustomMask == CustomMask {
5050
if tmpBuf[0]&(dateSchemaUUID<<4) != (dateSchemaUUID << 4) {
5151
return nil, nil
5252
}
5353
} else {
5454
return nil, nil
5555
}
5656

57-
nullable := (tmpBuf[0]&SchemaNullBit == SchemaNullBit)
57+
nullable := (tmpBuf[0]&NullMask == NullMask)
5858

5959
s := DateSchema{}
6060
s.SetNullable(nullable)
@@ -130,8 +130,8 @@ func (s *DateSchema) MarshalSchemer() ([]byte, error) {
130130
// string schemas are 1 byte long
131131
var schema []byte = make([]byte, schemerDateSize)
132132

133-
schema[0] |= CustomSchemaMask
134-
schema[0] |= (dateSchemaUUID<<4)
133+
schema[0] |= CustomMask
134+
schema[0] |= (dateSchemaUUID << 4)
135135

136136
// The most signifiant bit indicates whether or not the type is nullable
137137
if s.Nullable() {

enum.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,22 @@ func (s *EnumSchema) MarshalJSON() ([]byte, error) {
4242
func (s EnumSchema) MarshalSchemer() ([]byte, error) {
4343

4444
// fixed length schemas are 1 byte long total
45-
var schema []byte = []byte{EnumSchemaMask}
45+
var schema []byte = []byte{EnumByte}
4646

4747
// The most signifiant bit indicates whether or not the type is nullable
4848
if s.Nullable() {
49-
schema[0] |= 0x80
49+
schema[0] |= NullMask
5050
}
5151

5252
// write all the enumerated values as part of the schema...
5353
var buf bytes.Buffer
5454

55-
varObjectSchema, err := SchemaOf(s.Values)
56-
if err != nil {
57-
return nil, err
55+
mapSchema := VarObjectSchema{
56+
Key: &VarIntSchema{Signed: false},
57+
Value: &VarStringSchema{},
5858
}
5959

60-
err = varObjectSchema.Encode(&buf, s.Values)
60+
err := mapSchema.Encode(&buf, s.Values)
6161
if err != nil {
6262
return nil, err
6363
}

fixedarray.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func (s *FixedArraySchema) Valid() bool {
3333
func (s *FixedArraySchema) MarshalSchemer() ([]byte, error) {
3434

3535
// fixed length schemas are 1 byte long total
36-
var schema []byte = []byte{FixedArraySchemaMask}
36+
var schema []byte = []byte{FixedArrayByte}
3737

3838
// The most signifiant bit indicates whether or not the type is nullable
3939
if s.Nullable() {
40-
schema[0] |= 0x80
40+
schema[0] |= NullMask
4141
}
4242

4343
// encode array fixed length as a varint

fixedint.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ func (s *FixedIntSchema) Valid() bool {
7171
func (s *FixedIntSchema) MarshalSchemer() ([]byte, error) {
7272

7373
// fixed length schemas are 1 byte long total
74-
var schema []byte = []byte{FixedIntSchemaMask}
74+
var schema []byte = []byte{FixedIntByte}
7575

7676
// bit8 indicates whether or not the type is nullable
7777
if s.Nullable() {
78-
schema[0] |= 0x80
78+
schema[0] |= NullMask
7979
}
8080

8181
// bit1 indicates if the the fixed length int is signed or not

fixedobject.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ func (s *FixedObjectSchema) MarshalJSON() ([]byte, error) {
7272
func (s *FixedObjectSchema) MarshalSchemer() ([]byte, error) {
7373

7474
// fixedObject schemas are 1 byte long
75-
var schemaBytes []byte = []byte{FixedObjectSchemaMask}
75+
var schemaBytes []byte = []byte{FixedObjectByte}
7676

7777
// The most signifiant bit indicates whether or not the type is nullable
7878
if s.Nullable() {
79-
schemaBytes[0] |= 0x80
79+
schemaBytes[0] |= NullMask
8080
}
8181

8282
// encode total number of fields as a varint

fixedstring.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ func (s *FixedStringSchema) MarshalJSON() ([]byte, error) {
4848
func (s *FixedStringSchema) MarshalSchemer() ([]byte, error) {
4949

5050
// string schemas are 1 byte long
51-
var schema []byte = []byte{FixedStringSchemaMask}
51+
var schema []byte = []byte{FixedStringByte}
5252

5353
// The most signifiant bit indicates whether or not the type is nullable
5454
if s.Nullable() {
55-
schema[0] |= 0x80
55+
schema[0] |= NullMask
5656
}
5757

5858
// set bit 1, which indicates this is fixed len string

float.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func (s *FloatSchema) MarshalJSON() ([]byte, error) {
5252
func (s *FloatSchema) MarshalSchemer() ([]byte, error) {
5353

5454
// floating point schemas are 1 byte long
55-
var schema []byte = []byte{FloatBinarySchemaFormat}
55+
var schema []byte = []byte{FloatByte}
5656

5757
// The most signifiant bit indicates whether or not the type is nullable
5858
if s.Nullable() {
59-
schema[0] |= 0x80
59+
schema[0] |= NullMask
6060
}
6161

6262
if s.Bits == 32 {

ipv4.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ func (sg ipv4SchemaGenerator) DecodeSchema(r io.Reader) (Schema, error) {
4646
return nil, err
4747
}
4848

49-
if tmpBuf[0]&CustomSchemaMask == CustomSchemaMask {
49+
if tmpBuf[0]&CustomMask == CustomMask {
5050
if tmpBuf[0]&(ipV4SchemaUUID<<4) != (ipV4SchemaUUID << 4) {
5151
return nil, nil
5252
}
5353
} else {
5454
return nil, nil
5555
}
5656

57-
nullable := (tmpBuf[0]&SchemaNullBit == SchemaNullBit)
57+
nullable := (tmpBuf[0]&NullMask == NullMask)
5858

5959
s := ipv4Schema{}
6060
s.SetNullable(nullable)
@@ -138,7 +138,7 @@ func (s *ipv4Schema) MarshalSchemer() ([]byte, error) {
138138
// string schemas are 1 byte long
139139
var schema []byte = make([]byte, schemerDateSize)
140140

141-
schema[0] |= CustomSchemaMask
141+
schema[0] |= CustomMask
142142
schema[0] |= (ipV4SchemaUUID << 4)
143143

144144
// The most signifiant bit indicates whether or not the type is nullable

0 commit comments

Comments
 (0)