blob: 184bc26ae992add50889a720f909469428af8079 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001package 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.
6type packetDecoder interface {
7 // Primitives
8 getInt8() (int8, error)
9 getInt16() (int16, error)
10 getInt32() (int32, error)
11 getInt64() (int64, error)
12 getVarint() (int64, error)
khenaidood948f772021-08-11 17:49:24 -040013 getUVarint() (uint64, error)
khenaidooac637102019-01-14 15:44:34 -050014 getArrayLength() (int, error)
khenaidood948f772021-08-11 17:49:24 -040015 getCompactArrayLength() (int, error)
khenaidooac637102019-01-14 15:44:34 -050016 getBool() (bool, error)
khenaidood948f772021-08-11 17:49:24 -040017 getEmptyTaggedFieldArray() (int, error)
khenaidooac637102019-01-14 15:44:34 -050018
19 // Collections
20 getBytes() ([]byte, error)
21 getVarintBytes() ([]byte, error)
khenaidood948f772021-08-11 17:49:24 -040022 getCompactBytes() ([]byte, error)
khenaidooac637102019-01-14 15:44:34 -050023 getRawBytes(length int) ([]byte, error)
24 getString() (string, error)
25 getNullableString() (*string, error)
khenaidood948f772021-08-11 17:49:24 -040026 getCompactString() (string, error)
27 getCompactNullableString() (*string, error)
28 getCompactInt32Array() ([]int32, error)
khenaidooac637102019-01-14 15:44:34 -050029 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
Scott Baker8461e152019-10-01 14:44:30 -070037 peekInt8(offset int) (int8, error) // similar to peek, but just one byte
khenaidooac637102019-01-14 15:44:34 -050038
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.
48type 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()
65type dynamicPushDecoder interface {
66 pushDecoder
67 decoder
68}