Skip to content

Commit 9d69634

Browse files
committed
refactor: replace serializer.GetEncodedBytes with utils.GetEncodedBytes to unify encoding calls across packages
1 parent d401427 commit 9d69634

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

internal/assertions/fetch_response_assertion.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/codecrafters-io/kafka-tester/protocol"
88
kafkaapi "github.com/codecrafters-io/kafka-tester/protocol/api"
9-
"github.com/codecrafters-io/kafka-tester/protocol/serializer"
9+
"github.com/codecrafters-io/kafka-tester/protocol/utils"
1010
"github.com/codecrafters-io/tester-utils/bytes_diff_visualizer"
1111
"github.com/codecrafters-io/tester-utils/logger"
1212
)
@@ -185,8 +185,8 @@ func (a *FetchResponseAssertion) assertRecordBatchBytes(logger *logger.Logger) e
185185
}
186186
}
187187

188-
expectedRecordBatchBytes := serializer.GetEncodedBytes(expectedRecordBatches)
189-
actualRecordBatchBytes := serializer.GetEncodedBytes(actualRecordBatches)
188+
expectedRecordBatchBytes := utils.GetEncodedBytes(expectedRecordBatches)
189+
actualRecordBatchBytes := utils.GetEncodedBytes(actualRecordBatches)
190190
// Byte Comparison for expected v actual RecordBatch bytes
191191
// As we write them to disk, and expect users to not change the values
192192
// we can use a simple byte comparison here.

internal/cluster_metadata_payload_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77

88
kafkaapi "github.com/codecrafters-io/kafka-tester/protocol/api"
9-
"github.com/codecrafters-io/kafka-tester/protocol/serializer"
9+
"github.com/codecrafters-io/kafka-tester/protocol/utils"
1010
"github.com/stretchr/testify/assert"
1111
)
1212

@@ -157,7 +157,7 @@ func TestEncodeBeginTransactionRecordPayload(t *testing.T) {
157157
},
158158
}
159159

160-
bytes := serializer.GetEncodedBytes(beginTransactionRecord)
160+
bytes := utils.GetEncodedBytes(beginTransactionRecord)
161161
fmt.Printf("%s\n", hex.Dump(bytes))
162162

163163
assert.Equal(t, "01170001001212426f6f747374726170207265636f726473", hex.EncodeToString(bytes))
@@ -174,7 +174,7 @@ func TestEncodeFeatureLevelRecordPayload(t *testing.T) {
174174
},
175175
}
176176

177-
bytes := serializer.GetEncodedBytes(featureLevelRecord)
177+
bytes := utils.GetEncodedBytes(featureLevelRecord)
178178
fmt.Printf("%s\n", hex.Dump(bytes))
179179

180180
assert.Equal(t, "010c00116d657461646174612e76657273696f6e001400", hex.EncodeToString(bytes))
@@ -187,7 +187,7 @@ func TestEncodeZKMigrationRecordPayload(t *testing.T) {
187187
Version: 0,
188188
Data: &kafkaapi.ZKMigrationStateRecord{MigrationState: 0},
189189
}
190-
bytes := serializer.GetEncodedBytes(zkMigrationRecord)
190+
bytes := utils.GetEncodedBytes(zkMigrationRecord)
191191
fmt.Printf("%s\n", hex.Dump(bytes))
192192

193193
assert.Equal(t, "0115000000", hex.EncodeToString(bytes))
@@ -201,7 +201,7 @@ func TestEncodeEndTransactionRecordPayload(t *testing.T) {
201201
Data: &kafkaapi.EndTransactionRecord{},
202202
}
203203

204-
bytes := serializer.GetEncodedBytes(endTransactionRecord)
204+
bytes := utils.GetEncodedBytes(endTransactionRecord)
205205
fmt.Printf("%s\n", hex.Dump(bytes))
206206

207207
assert.Equal(t, "01180000", hex.EncodeToString(bytes))
@@ -216,7 +216,7 @@ func TestEncodeTopicRecordPayload(t *testing.T) {
216216
TopicName: "foo",
217217
TopicUUID: "bfd99e5e-3235-4552-81f8-d4af1741970c"},
218218
}
219-
bytes := serializer.GetEncodedBytes(topicRecord)
219+
bytes := utils.GetEncodedBytes(topicRecord)
220220
fmt.Printf("%s\n", hex.Dump(bytes))
221221

222222
assert.Equal(t, "01020004666f6fbfd99e5e3235455281f8d4af1741970c00", hex.EncodeToString(bytes))
@@ -241,7 +241,7 @@ func TestEncodePartitionRecordPayload(t *testing.T) {
241241
},
242242
}
243243

244-
bytes := serializer.GetEncodedBytes(partitionRecord)
244+
bytes := utils.GetEncodedBytes(partitionRecord)
245245
fmt.Printf("%s\n", hex.Dump(bytes))
246246

247247
assert.Equal(t, "01030100000000bfd99e5e3235455281f8d4af1741970c020000000102000000010101000000010000000000000000020224973cbadd44cf874445a99619da3400", hex.EncodeToString(bytes))

protocol/api/produce_request.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kafkaapi
33
import (
44
"github.com/codecrafters-io/kafka-tester/protocol/api/headers"
55
"github.com/codecrafters-io/kafka-tester/protocol/encoder"
6+
"github.com/codecrafters-io/kafka-tester/protocol/utils"
67
)
78

89
type ProducePartitionData struct {
@@ -12,7 +13,7 @@ type ProducePartitionData struct {
1213

1314
func (p ProducePartitionData) encode(pe *encoder.Encoder) {
1415
pe.PutInt32(p.Index)
15-
pe.PutCompactArrayLength(RecordBatches(p.RecordBatches).getEncodedLength())
16+
pe.PutCompactArrayLength(utils.GetEncodedLength(RecordBatches(p.RecordBatches)))
1617
for _, recordBatch := range p.RecordBatches {
1718
recordBatch.Encode(pe)
1819
}

protocol/serializer/cluster_metadata.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
kafkaapi "github.com/codecrafters-io/kafka-tester/protocol/api"
88
"github.com/codecrafters-io/kafka-tester/protocol/encoder"
9+
"github.com/codecrafters-io/kafka-tester/protocol/utils"
910
"github.com/codecrafters-io/tester-utils/logger"
1011
)
1112

@@ -140,7 +141,7 @@ func writeClusterMetadata(path string, topic1Name string, topic1UUID string, top
140141
Attributes: 0,
141142
TimestampDelta: 0,
142143
Key: nil,
143-
Value: GetEncodedBytes(featureLevelRecord),
144+
Value: utils.GetEncodedBytes(featureLevelRecord),
144145
Headers: []kafkaapi.RecordHeader{},
145146
},
146147
},
@@ -161,14 +162,14 @@ func writeClusterMetadata(path string, topic1Name string, topic1UUID string, top
161162
Attributes: 0,
162163
TimestampDelta: 0,
163164
Key: nil,
164-
Value: GetEncodedBytes(topicRecord1),
165+
Value: utils.GetEncodedBytes(topicRecord1),
165166
Headers: []kafkaapi.RecordHeader{},
166167
},
167168
{
168169
Attributes: 0,
169170
TimestampDelta: 0,
170171
Key: nil,
171-
Value: GetEncodedBytes(partitionRecord1),
172+
Value: utils.GetEncodedBytes(partitionRecord1),
172173
Headers: []kafkaapi.RecordHeader{},
173174
},
174175
},
@@ -189,14 +190,14 @@ func writeClusterMetadata(path string, topic1Name string, topic1UUID string, top
189190
Attributes: 0,
190191
TimestampDelta: 0,
191192
Key: nil,
192-
Value: GetEncodedBytes(topicRecord2),
193+
Value: utils.GetEncodedBytes(topicRecord2),
193194
Headers: []kafkaapi.RecordHeader{},
194195
},
195196
{
196197
Attributes: 0,
197198
TimestampDelta: 0,
198199
Key: nil,
199-
Value: GetEncodedBytes(partitionRecord2),
200+
Value: utils.GetEncodedBytes(partitionRecord2),
200201
Headers: []kafkaapi.RecordHeader{},
201202
},
202203
},
@@ -217,21 +218,21 @@ func writeClusterMetadata(path string, topic1Name string, topic1UUID string, top
217218
Attributes: 0,
218219
TimestampDelta: 0,
219220
Key: nil,
220-
Value: GetEncodedBytes(topicRecord3),
221+
Value: utils.GetEncodedBytes(topicRecord3),
221222
Headers: []kafkaapi.RecordHeader{},
222223
},
223224
{
224225
Attributes: 0,
225226
TimestampDelta: 0,
226227
Key: nil,
227-
Value: GetEncodedBytes(partitionRecord3),
228+
Value: utils.GetEncodedBytes(partitionRecord3),
228229
Headers: []kafkaapi.RecordHeader{},
229230
},
230231
{
231232
Attributes: 0,
232233
TimestampDelta: 0,
233234
Key: nil,
234-
Value: GetEncodedBytes(partitionRecord4),
235+
Value: utils.GetEncodedBytes(partitionRecord4),
235236
Headers: []kafkaapi.RecordHeader{},
236237
},
237238
},

protocol/serializer/cluster_metadata_binspec.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
kafkaapi "github.com/codecrafters-io/kafka-tester/protocol/api"
88
"github.com/codecrafters-io/kafka-tester/protocol/common"
99
"github.com/codecrafters-io/kafka-tester/protocol/encoder"
10+
utils "github.com/codecrafters-io/kafka-tester/protocol/utils"
1011
"github.com/codecrafters-io/tester-utils/logger"
1112
)
1213

@@ -88,7 +89,7 @@ func writeClusterMetadataBinSpec(path string, directoryUUID string, logger *logg
8889
Attributes: 0,
8990
TimestampDelta: 0,
9091
Key: nil,
91-
Value: GetEncodedBytes(featureLevelRecord),
92+
Value: utils.GetEncodedBytes(featureLevelRecord),
9293
Headers: []kafkaapi.RecordHeader{},
9394
},
9495
},
@@ -109,21 +110,21 @@ func writeClusterMetadataBinSpec(path string, directoryUUID string, logger *logg
109110
Attributes: 0,
110111
TimestampDelta: 0,
111112
Key: nil,
112-
Value: GetEncodedBytes(topicRecord3),
113+
Value: utils.GetEncodedBytes(topicRecord3),
113114
Headers: []kafkaapi.RecordHeader{},
114115
},
115116
{
116117
Attributes: 0,
117118
TimestampDelta: 0,
118119
Key: nil,
119-
Value: GetEncodedBytes(partitionRecord3),
120+
Value: utils.GetEncodedBytes(partitionRecord3),
120121
Headers: []kafkaapi.RecordHeader{},
121122
},
122123
{
123124
Attributes: 0,
124125
TimestampDelta: 0,
125126
Key: nil,
126-
Value: GetEncodedBytes(partitionRecord4),
127+
Value: utils.GetEncodedBytes(partitionRecord4),
127128
Headers: []kafkaapi.RecordHeader{},
128129
},
129130
},

0 commit comments

Comments
 (0)