William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [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) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 13 | getUVarint() (uint64, error) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 14 | getArrayLength() (int, error) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 15 | getCompactArrayLength() (int, error) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 16 | getBool() (bool, error) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 17 | getEmptyTaggedFieldArray() (int, error) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 18 | |
| 19 | // Collections |
| 20 | getBytes() ([]byte, error) |
| 21 | getVarintBytes() ([]byte, error) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 22 | getCompactBytes() ([]byte, error) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 23 | getRawBytes(length int) ([]byte, error) |
| 24 | getString() (string, error) |
| 25 | getNullableString() (*string, error) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 26 | getCompactString() (string, error) |
| 27 | getCompactNullableString() (*string, error) |
| 28 | getCompactInt32Array() ([]int32, error) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 29 | getInt32Array() ([]int32, error) |
| 30 | getInt64Array() ([]int64, error) |
| 31 | getStringArray() ([]string, error) |
| 32 | |
| 33 | // Subsets |
| 34 | remaining() int |
| 35 | getSubset(length int) (packetDecoder, error) |
| 36 | peek(offset, length int) (packetDecoder, error) // similar to getSubset, but it doesn't advance the offset |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 37 | peekInt8(offset int) (int8, error) // similar to peek, but just one byte |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 38 | |
| 39 | // Stacks, see PushDecoder |
| 40 | push(in pushDecoder) error |
| 41 | pop() error |
| 42 | } |
| 43 | |
| 44 | // PushDecoder is the interface for decoding fields like CRCs and lengths where the validity |
| 45 | // of the field depends on what is after it in the packet. Start them with PacketDecoder.Push() where |
| 46 | // the actual value is located in the packet, then PacketDecoder.Pop() them when all the bytes they |
| 47 | // depend upon have been decoded. |
| 48 | type pushDecoder interface { |
| 49 | // Saves the offset into the input buffer as the location to actually read the calculated value when able. |
| 50 | saveOffset(in int) |
| 51 | |
| 52 | // Returns the length of data to reserve for the input of this encoder (eg 4 bytes for a CRC32). |
| 53 | reserveLength() int |
| 54 | |
| 55 | // Indicates that all required data is now available to calculate and check the field. |
| 56 | // SaveOffset is guaranteed to have been called first. The implementation should read ReserveLength() bytes |
| 57 | // of data from the saved offset, and verify it based on the data between the saved offset and curOffset. |
| 58 | check(curOffset int, buf []byte) error |
| 59 | } |
| 60 | |
| 61 | // dynamicPushDecoder extends the interface of pushDecoder for uses cases where the length of the |
| 62 | // fields itself is unknown until its value was decoded (for instance varint encoded length |
| 63 | // fields). |
| 64 | // During push, dynamicPushDecoder.decode() method will be called instead of reserveLength() |
| 65 | type dynamicPushDecoder interface { |
| 66 | pushDecoder |
| 67 | decoder |
| 68 | } |