khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 1 | package sarama |
| 2 | |
| 3 | // PacketDecoder is the interface providing helpers for reading with Kafka's encoding rules. |
| 4 | // Types implementing Decoder only need to worry about calling methods like GetString, |
| 5 | // not about how a string is represented in Kafka. |
| 6 | type packetDecoder interface { |
| 7 | // Primitives |
| 8 | getInt8() (int8, error) |
| 9 | getInt16() (int16, error) |
| 10 | getInt32() (int32, error) |
| 11 | getInt64() (int64, error) |
| 12 | getVarint() (int64, error) |
| 13 | getArrayLength() (int, error) |
| 14 | getBool() (bool, error) |
| 15 | |
| 16 | // Collections |
| 17 | getBytes() ([]byte, error) |
| 18 | getVarintBytes() ([]byte, error) |
| 19 | getRawBytes(length int) ([]byte, error) |
| 20 | getString() (string, error) |
| 21 | getNullableString() (*string, error) |
| 22 | getInt32Array() ([]int32, error) |
| 23 | getInt64Array() ([]int64, error) |
| 24 | getStringArray() ([]string, error) |
| 25 | |
| 26 | // Subsets |
| 27 | remaining() int |
| 28 | getSubset(length int) (packetDecoder, error) |
| 29 | peek(offset, length int) (packetDecoder, error) // similar to getSubset, but it doesn't advance the offset |
Scott Baker | 8461e15 | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 30 | peekInt8(offset int) (int8, error) // similar to peek, but just one byte |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 31 | |
| 32 | // Stacks, see PushDecoder |
| 33 | push(in pushDecoder) error |
| 34 | pop() error |
| 35 | } |
| 36 | |
| 37 | // PushDecoder is the interface for decoding fields like CRCs and lengths where the validity |
| 38 | // of the field depends on what is after it in the packet. Start them with PacketDecoder.Push() where |
| 39 | // the actual value is located in the packet, then PacketDecoder.Pop() them when all the bytes they |
| 40 | // depend upon have been decoded. |
| 41 | type pushDecoder interface { |
| 42 | // Saves the offset into the input buffer as the location to actually read the calculated value when able. |
| 43 | saveOffset(in int) |
| 44 | |
| 45 | // Returns the length of data to reserve for the input of this encoder (eg 4 bytes for a CRC32). |
| 46 | reserveLength() int |
| 47 | |
| 48 | // Indicates that all required data is now available to calculate and check the field. |
| 49 | // SaveOffset is guaranteed to have been called first. The implementation should read ReserveLength() bytes |
| 50 | // of data from the saved offset, and verify it based on the data between the saved offset and curOffset. |
| 51 | check(curOffset int, buf []byte) error |
| 52 | } |
| 53 | |
| 54 | // dynamicPushDecoder extends the interface of pushDecoder for uses cases where the length of the |
| 55 | // fields itself is unknown until its value was decoded (for instance varint encoded length |
| 56 | // fields). |
| 57 | // During push, dynamicPushDecoder.decode() method will be called instead of reserveLength() |
| 58 | type dynamicPushDecoder interface { |
| 59 | pushDecoder |
| 60 | decoder |
| 61 | } |