Skip to content

Commit

Permalink
Removing dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiojmendes committed Apr 25, 2022
1 parent 141bd0a commit db491d8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
29 changes: 28 additions & 1 deletion pkg/avro/avro.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func NewAvroCodec(schemaID int, cache *SchemaCache) *AvroCodec {
return &AvroCodec{schemaID, cache}
}

// Encode returns a binary representation of an Avro-encoded message.
func (a *AvroCodec) Encode(in []byte) ([]byte, error) {
codec, err := a.schemaCache.getCodecForSchemaID(a.encodeSchemaID)
if err != nil {
Expand All @@ -39,6 +40,32 @@ func (a *AvroCodec) Encode(in []byte) ([]byte, error) {
return message, nil
}

// Decode returns a text representation of an Avro-encoded message.
func (a *AvroCodec) Decode(in []byte) ([]byte, error) {
return a.schemaCache.DecodeMessage(in)
// Ensure avro header is present with the magic start-byte.
if len(in) < 5 || in[0] != 0x00 {
// The message does not contain Avro-encoded data
return in, nil
}

// Schema ID is stored in the 4 bytes following the magic byte.
schemaID := binary.BigEndian.Uint32(in[1:5])
codec, err := a.schemaCache.getCodecForSchemaID(int(schemaID))
if err != nil {
return in, err
}

// Convert binary Avro data back to native Go form
native, _, err := codec.NativeFromBinary(in[5:])
if err != nil {
return in, err
}

// Convert native Go form to textual Avro data
message, err := codec.TextualFromNative(nil, native)
if err != nil {
return in, err
}

return message, nil
}
27 changes: 0 additions & 27 deletions pkg/avro/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,3 @@ func (c *SchemaCache) DecodeMessage(b []byte) (message []byte, err error) {

return message, nil
}

// EncodeMessage returns a binary representation of an Avro-encoded message.
func (c *SchemaCache) EncodeMessage(schemaID int, json []byte) (message []byte, err error) {
codec, err := c.getCodecForSchemaID(schemaID)
if err != nil {
return nil, err
}

// Creates a header with an initial zero byte and
// the schema id encoded as a big endian uint32
buf := make([]byte, 5)
binary.BigEndian.PutUint32(buf[1:5], uint32(schemaID))

// Convert textual json data to native Go form
native, _, err := codec.NativeFromTextual(json)
if err != nil {
return nil, err
}

// Convert native Go form to binary Avro data
message, err = codec.BinaryFromNative(buf, native)
if err != nil {
return nil, err
}

return message, nil
}
8 changes: 8 additions & 0 deletions test/user.avro
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "user",
"type": "record",
"fields": [
{ "name": "id", "type": "int" },
{ "name": "name", "type": "string" }
]
}

0 comments on commit db491d8

Please # to comment.