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