blob: 74805ccbf53f7b7427582d7cf121ade407e92ab5 [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001package 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)
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
30
31 // Stacks, see PushDecoder
32 push(in pushDecoder) error
33 pop() error
34}
35
36// PushDecoder is the interface for decoding fields like CRCs and lengths where the validity
37// of the field depends on what is after it in the packet. Start them with PacketDecoder.Push() where
38// the actual value is located in the packet, then PacketDecoder.Pop() them when all the bytes they
39// depend upon have been decoded.
40type pushDecoder interface {
41 // Saves the offset into the input buffer as the location to actually read the calculated value when able.
42 saveOffset(in int)
43
44 // Returns the length of data to reserve for the input of this encoder (eg 4 bytes for a CRC32).
45 reserveLength() int
46
47 // Indicates that all required data is now available to calculate and check the field.
48 // SaveOffset is guaranteed to have been called first. The implementation should read ReserveLength() bytes
49 // of data from the saved offset, and verify it based on the data between the saved offset and curOffset.
50 check(curOffset int, buf []byte) error
51}
52
53// dynamicPushDecoder extends the interface of pushDecoder for uses cases where the length of the
54// fields itself is unknown until its value was decoded (for instance varint encoded length
55// fields).
56// During push, dynamicPushDecoder.decode() method will be called instead of reserveLength()
57type dynamicPushDecoder interface {
58 pushDecoder
59 decoder
60}