-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Use a proper Schema Registry client to fix issues in #22 * Add srclient dependency as a better way to access Schema Registry * Combine functions in Avro serdes * Change SchemaRegistryConfiguration and BasicAuth to fix issues (breaking change) * Validate configuration for all supported (and unsupported) serde functionality * Default serde is now StringSerializer and StringDeserializer * Refactor wire format de/encoding * Fix bugs and typos and refactor the rest * Refactor basic auth function and move structs to their relevant files * Fix linting errors reported by gosimple (golangci-lint)
- Loading branch information
Showing
12 changed files
with
256 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,57 @@ | ||
package kafka | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/linkedin/goavro/v2" | ||
) | ||
|
||
func SerializeAvro(configuration Configuration, topic string, data interface{}, keyOrValue string, schema string) ([]byte, error) { | ||
key := []byte(data.(string)) | ||
func SerializeAvro(configuration Configuration, topic string, data interface{}, element Element, schema string, version int) ([]byte, error) { | ||
bytesData := []byte(data.(string)) | ||
if schema != "" { | ||
key = ToAvro(data.(string), schema) | ||
codec, err := goavro.NewCodec(schema) | ||
if err != nil { | ||
ReportError(err, "Failed to create codec for encoding Avro") | ||
} | ||
|
||
avroEncodedData, _, err := codec.NativeFromTextual(bytesData) | ||
if err != nil { | ||
ReportError(err, "Failed to encode data into Avro") | ||
} | ||
|
||
bytesData, err = codec.BinaryFromNative(nil, avroEncodedData) | ||
if err != nil { | ||
ReportError(err, "Failed to encode Avro data into binary") | ||
} | ||
} | ||
|
||
byteData, err := addMagicByteAndSchemaIdPrefix(configuration, key, topic, keyOrValue, schema) | ||
byteData, err := encodeWireFormat(configuration, bytesData, topic, element, schema, version) | ||
if err != nil { | ||
ReportError(err, "Failed to add wire format to the binary data") | ||
return nil, err | ||
} | ||
|
||
return byteData, nil | ||
} | ||
|
||
func ToAvro(value string, schema string) []byte { | ||
codec, err := goavro.NewCodec(schema) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
native, _, err := codec.NativeFromTextual([]byte(value)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
binary, err := codec.BinaryFromNative(nil, native) | ||
func DeserializeAvro(configuration Configuration, data []byte, element Element, schema string, version int) interface{} { | ||
bytesDecodedData, err := decodeWireFormat(configuration, data, element) | ||
if err != nil { | ||
log.Fatal(err) | ||
ReportError(err, "Failed to remove wire format from the binary data") | ||
return nil | ||
} | ||
|
||
return binary | ||
} | ||
|
||
func DeserializeAvro(configuration Configuration, data []byte, keyOrValue string, schema string) interface{} { | ||
dataWithoutPrefix := removeMagicByteAndSchemaIdPrefix(configuration, data, keyOrValue) | ||
|
||
if schema != "" { | ||
return FromAvro(dataWithoutPrefix, schema) | ||
} | ||
codec, err := goavro.NewCodec(schema) | ||
if err != nil { | ||
ReportError(err, "Failed to create codec for decoding Avro") | ||
} | ||
|
||
return dataWithoutPrefix | ||
} | ||
avroDecodedData, _, err := codec.NativeFromBinary(bytesDecodedData) | ||
if err != nil { | ||
ReportError(err, "Failed to decode data from Avro") | ||
} | ||
|
||
func FromAvro(message []byte, schema string) interface{} { | ||
codec, err := goavro.NewCodec(schema) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
native, _, err := codec.NativeFromBinary(message) | ||
if err != nil { | ||
log.Fatal(err) | ||
return avroDecodedData | ||
} | ||
|
||
return native | ||
return bytesDecodedData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.