Last active
August 5, 2022 13:00
-
-
Save mattburman/0046a78a54b623f309d9171febc20274 to your computer and use it in GitHub Desktop.
An example of encoding and decoding the start of a kafka message that conforms to the Confluent Schema Registry wire format
This file contains hidden or 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
| // An example of encoding and decoding the start of a kafka message that conforms | |
| // to the Confluent Schema Registry wire format: | |
| // see https://docs.confluent.io/current/schema-registry/serdes-develop/index.html#wire-format | |
| package main | |
| import ( | |
| "encoding/binary" | |
| "fmt" | |
| "github.com/pkg/errors" | |
| ) | |
| // schemaID gets the schema id from the first 5 bytes of the data | |
| func schemaID(data []byte) (int, error) { | |
| if data == nil || len(data) < 5 { | |
| return 0, nil | |
| } | |
| if data[0] != 0 { | |
| return 0, errors.New("unknown magic byte") | |
| } | |
| return int(binary.BigEndian.Uint32(data[1:5])), nil | |
| } | |
| func getFourBytes(v uint32) []byte { | |
| fourByteBuffer := make([]byte, 4) | |
| binary.BigEndian.PutUint32(fourByteBuffer, v) | |
| return fourByteBuffer | |
| } | |
| func main() { | |
| // a byte of zero i.e. 0x0 (hex representation) or 0b00000000 (binary representation) | |
| // this is the "magic byte" | |
| zero := byte(0) | |
| // a four-byte (32-bit) buffer to store the schema ID as a uint32 | |
| fourByteBuffer := getFourBytes(uint32(56789)) | |
| // construct a final five byte representation with the "magic byte" then the four schema bytes | |
| fivebytes := make([]byte, 0, 5) | |
| fivebytes = append(fivebytes, zero) | |
| fivebytes = append(fivebytes, fourByteBuffer...) | |
| fmt.Println(fivebytes) | |
| // test schemaID can extract the 5 bytes! | |
| id, err := schemaID(fivebytes) | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| fmt.Println(id) // should be v above | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment