Skip to content

Commit b2ab5bf

Browse files
Copilotstreamich
andcommitted
style: fix code formatting with prettier
Co-authored-by: streamich <[email protected]>
1 parent d6842c4 commit b2ab5bf

File tree

7 files changed

+59
-61
lines changed

7 files changed

+59
-61
lines changed

src/avro/AvroEncoder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,20 @@ export class AvroEncoder implements BinaryJsonEncoder {
121121
const writer = this.writer;
122122
const maxSize = str.length * 4; // Max UTF-8 bytes for string
123123
writer.ensureCapacity(5 + maxSize); // 5 bytes max for varint length
124-
124+
125125
// Reserve space for length (we'll come back to fill this)
126126
const lengthOffset = writer.x;
127127
writer.x += 5; // Max varint size
128-
128+
129129
// Write the string and get actual byte count
130130
const bytesWritten = writer.utf8(str);
131131
const endPos = writer.x;
132-
132+
133133
// Go back to encode the actual length
134134
writer.x = lengthOffset;
135135
this.writeVarIntUnsigned(bytesWritten);
136136
const actualLengthSize = writer.x - lengthOffset;
137-
137+
138138
// If we reserved more space than needed, shift the string data
139139
if (actualLengthSize < 5) {
140140
const stringStart = lengthOffset + 5;
@@ -266,7 +266,7 @@ export class AvroEncoder implements BinaryJsonEncoder {
266266
let n = value;
267267
const mask = BigInt(0x7f);
268268
const shift = BigInt(7);
269-
269+
270270
while (n >= BigInt(0x80)) {
271271
writer.u8(Number((n & mask) | BigInt(0x80)));
272272
n >>= shift;
@@ -287,4 +287,4 @@ export class AvroEncoder implements BinaryJsonEncoder {
287287
private encodeZigZag64(value: bigint): bigint {
288288
return (value << BigInt(1)) ^ (value >> BigInt(63));
289289
}
290-
}
290+
}

src/avro/AvroSchemaEncoder.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@ export class AvroSchemaEncoder {
3434
public encode(value: unknown, schema: AvroSchema, selectedIndex?: number): Uint8Array {
3535
this.writer.reset();
3636
this.namedSchemas.clear();
37-
37+
3838
// Validate schema first
3939
if (!this.validator.validateSchema(schema)) {
4040
throw new Error('Invalid Avro schema');
4141
}
42-
42+
4343
// Validate value against schema
4444
if (!this.validator.validateValue(value, schema)) {
4545
throw new Error('Value does not conform to schema');
4646
}
47-
47+
4848
this.collectNamedSchemas(schema);
49-
49+
5050
if (Array.isArray(schema) && selectedIndex !== undefined) {
5151
this.writeUnion(value, schema, selectedIndex);
5252
} else {
5353
this.writeValue(value, schema);
5454
}
55-
55+
5656
return this.writer.flush();
5757
}
5858

@@ -130,7 +130,7 @@ export class AvroSchemaEncoder {
130130
if (typeof schema === 'object' && schema.type !== 'record') {
131131
throw new Error('Schema is not a record schema');
132132
}
133-
133+
134134
const recordSchema = this.resolveSchema(schema) as AvroRecordSchema;
135135
if (recordSchema.type !== 'record') {
136136
throw new Error('Schema is not a record schema');
@@ -156,7 +156,7 @@ export class AvroSchemaEncoder {
156156
if (typeof schema === 'object' && schema.type !== 'enum') {
157157
throw new Error('Schema is not an enum schema');
158158
}
159-
159+
160160
const enumSchema = this.resolveSchema(schema) as AvroEnumSchema;
161161
if (enumSchema.type !== 'enum') {
162162
throw new Error('Schema is not an enum schema');
@@ -166,7 +166,7 @@ export class AvroSchemaEncoder {
166166
if (index === -1) {
167167
throw new Error(`Invalid enum value: ${value}`);
168168
}
169-
169+
170170
this.writeVarIntSigned(this.encodeZigZag32(index));
171171
}
172172

@@ -177,21 +177,21 @@ export class AvroSchemaEncoder {
177177
if (typeof schema === 'object' && schema.type !== 'array') {
178178
throw new Error('Schema is not an array schema');
179179
}
180-
180+
181181
const arraySchema = this.resolveSchema(schema) as AvroArraySchema;
182182
if (arraySchema.type !== 'array') {
183183
throw new Error('Schema is not an array schema');
184184
}
185185

186186
// Write array length
187187
this.writeVarIntUnsigned(value.length);
188-
188+
189189
// Write array items
190190
const length = value.length;
191191
for (let i = 0; i < length; i++) {
192192
this.writeValue(value[i], arraySchema.items);
193193
}
194-
194+
195195
// Write end-of-array marker
196196
this.writeVarIntUnsigned(0);
197197
}
@@ -203,25 +203,25 @@ export class AvroSchemaEncoder {
203203
if (typeof schema === 'object' && schema.type !== 'map') {
204204
throw new Error('Schema is not a map schema');
205205
}
206-
206+
207207
const mapSchema = this.resolveSchema(schema) as AvroMapSchema;
208208
if (mapSchema.type !== 'map') {
209209
throw new Error('Schema is not a map schema');
210210
}
211211

212212
const entries = Object.entries(value);
213-
213+
214214
// Write map length
215215
this.writeVarIntUnsigned(entries.length);
216-
216+
217217
// Write map entries
218218
const length = entries.length;
219219
for (let i = 0; i < length; i++) {
220220
const entry = entries[i];
221221
this.encoder.writeStr(entry[0]);
222222
this.writeValue(entry[1], mapSchema.values);
223223
}
224-
224+
225225
// Write end-of-map marker
226226
this.writeVarIntUnsigned(0);
227227
}
@@ -237,7 +237,7 @@ export class AvroSchemaEncoder {
237237
let index = selectedIndex;
238238
if (index === undefined) {
239239
// Find the first matching schema in the union
240-
index = schema.findIndex(subSchema => this.validator.validateValue(value, subSchema));
240+
index = schema.findIndex((subSchema) => this.validator.validateValue(value, subSchema));
241241
if (index === -1) {
242242
throw new Error('Value does not match any schema in the union');
243243
}
@@ -249,7 +249,7 @@ export class AvroSchemaEncoder {
249249

250250
// Write union index
251251
this.writeVarIntSigned(this.encodeZigZag32(index));
252-
252+
253253
// Write the value according to the selected schema
254254
this.writeValue(value, schema[index]);
255255
}
@@ -261,7 +261,7 @@ export class AvroSchemaEncoder {
261261
if (typeof schema === 'object' && schema.type !== 'fixed') {
262262
throw new Error('Schema is not a fixed schema');
263263
}
264-
264+
265265
const fixedSchema = this.resolveSchema(schema) as AvroFixedSchema;
266266
if (fixedSchema.type !== 'fixed') {
267267
throw new Error('Schema is not a fixed schema');
@@ -270,7 +270,7 @@ export class AvroSchemaEncoder {
270270
if (value.length !== fixedSchema.size) {
271271
throw new Error(`Fixed value length ${value.length} does not match schema size ${fixedSchema.size}`);
272272
}
273-
273+
274274
this.writer.buf(value, value.length);
275275
}
276276

@@ -279,12 +279,13 @@ export class AvroSchemaEncoder {
279279
*/
280280
public writeNumber(value: number, schema: AvroSchema): void {
281281
const resolvedSchema = this.resolveSchema(schema);
282-
const schemaType = typeof resolvedSchema === 'string'
283-
? resolvedSchema
284-
: Array.isArray(resolvedSchema)
285-
? 'union'
286-
: resolvedSchema.type;
287-
282+
const schemaType =
283+
typeof resolvedSchema === 'string'
284+
? resolvedSchema
285+
: Array.isArray(resolvedSchema)
286+
? 'union'
287+
: resolvedSchema.type;
288+
288289
switch (schemaType) {
289290
case 'int':
290291
this.writeInt(value, schema);
@@ -369,12 +370,13 @@ export class AvroSchemaEncoder {
369370

370371
private validateSchemaType(schema: AvroSchema, expectedType: string): void {
371372
const resolvedSchema = this.resolveSchema(schema);
372-
const actualType = typeof resolvedSchema === 'string'
373-
? resolvedSchema
374-
: Array.isArray(resolvedSchema)
375-
? 'union'
376-
: resolvedSchema.type;
377-
373+
const actualType =
374+
typeof resolvedSchema === 'string'
375+
? resolvedSchema
376+
: Array.isArray(resolvedSchema)
377+
? 'union'
378+
: resolvedSchema.type;
379+
378380
if (actualType !== expectedType) {
379381
throw new Error(`Expected schema type ${expectedType}, got ${actualType}`);
380382
}
@@ -399,7 +401,7 @@ export class AvroSchemaEncoder {
399401
const recordSchema = schema as AvroRecordSchema;
400402
const recordFullName = this.getFullName(recordSchema.name, recordSchema.namespace);
401403
this.namedSchemas.set(recordFullName, recordSchema);
402-
recordSchema.fields.forEach(field => this.collectNamedSchemas(field.type));
404+
recordSchema.fields.forEach((field) => this.collectNamedSchemas(field.type));
403405
break;
404406
case 'enum':
405407
const enumSchema = schema as AvroEnumSchema;
@@ -457,4 +459,4 @@ export class AvroSchemaEncoder {
457459
private encodeZigZag32(value: number): number {
458460
return (value << 1) ^ (value >> 31);
459461
}
460-
}
462+
}

src/avro/AvroSchemaValidator.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ export class AvroSchemaValidator {
9797
private validateUnionSchema(schema: AvroUnionSchema): boolean {
9898
if (schema.length === 0) return false;
9999
const typeSet = new Set<string>();
100-
100+
101101
for (const subSchema of schema) {
102102
if (!this.validateSchemaInternal(subSchema)) return false;
103-
103+
104104
// Union types must be unique
105105
const typeName = this.getSchemaTypeName(subSchema);
106106
if (typeSet.has(typeName)) return false;
107107
typeSet.add(typeName);
108108
}
109-
109+
110110
return true;
111111
}
112112

@@ -144,7 +144,7 @@ export class AvroSchemaValidator {
144144

145145
private validateRecordSchema(schema: AvroRecordSchema): boolean {
146146
if (schema.type !== 'record' || !schema.name || !Array.isArray(schema.fields)) return false;
147-
147+
148148
const fullName = this.getFullName(schema.name, schema.namespace);
149149
if (this.namedSchemas.has(fullName)) return false;
150150
this.namedSchemas.set(fullName, schema);
@@ -160,16 +160,12 @@ export class AvroSchemaValidator {
160160
}
161161

162162
private validateRecordField(field: AvroRecordField): boolean {
163-
return (
164-
typeof field.name === 'string' &&
165-
field.name.length > 0 &&
166-
this.validateSchemaInternal(field.type)
167-
);
163+
return typeof field.name === 'string' && field.name.length > 0 && this.validateSchemaInternal(field.type);
168164
}
169165

170166
private validateEnumSchema(schema: AvroEnumSchema): boolean {
171167
if (schema.type !== 'enum' || !schema.name || !Array.isArray(schema.symbols)) return false;
172-
168+
173169
const fullName = this.getFullName(schema.name, schema.namespace);
174170
if (this.namedSchemas.has(fullName)) return false;
175171
this.namedSchemas.set(fullName, schema);
@@ -198,7 +194,7 @@ export class AvroSchemaValidator {
198194
private validateFixedSchema(schema: AvroFixedSchema): boolean {
199195
if (schema.type !== 'fixed' || !schema.name || typeof schema.size !== 'number') return false;
200196
if (schema.size < 0) return false;
201-
197+
202198
const fullName = this.getFullName(schema.name, schema.namespace);
203199
if (this.namedSchemas.has(fullName)) return false;
204200
this.namedSchemas.set(fullName, schema);
@@ -213,7 +209,7 @@ export class AvroSchemaValidator {
213209

214210
if (Array.isArray(schema)) {
215211
// Union - value must match one of the schemas
216-
return schema.some(subSchema => this.validateValueAgainstSchema(value, subSchema));
212+
return schema.some((subSchema) => this.validateValueAgainstSchema(value, subSchema));
217213
}
218214

219215
if (typeof schema === 'object' && schema !== null) {
@@ -294,13 +290,13 @@ export class AvroSchemaValidator {
294290

295291
private validateValueAgainstArray(value: unknown, schema: AvroArraySchema): boolean {
296292
if (!Array.isArray(value)) return false;
297-
return value.every(item => this.validateValueAgainstSchema(item, schema.items));
293+
return value.every((item) => this.validateValueAgainstSchema(item, schema.items));
298294
}
299295

300296
private validateValueAgainstMap(value: unknown, schema: AvroMapSchema): boolean {
301297
if (typeof value !== 'object' || value === null) return false;
302298
const obj = value as Record<string, unknown>;
303-
return Object.values(obj).every(val => this.validateValueAgainstSchema(val, schema.values));
299+
return Object.values(obj).every((val) => this.validateValueAgainstSchema(val, schema.values));
304300
}
305301

306302
private validateValueAgainstFixed(value: unknown, schema: AvroFixedSchema): boolean {
@@ -316,4 +312,4 @@ export class AvroSchemaValidator {
316312
private getFullName(name: string, namespace?: string): string {
317313
return namespace ? `${namespace}.${name}` : name;
318314
}
319-
}
315+
}

src/avro/__tests__/AvroEncoder.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ describe('AvroEncoder', () => {
196196
expect(result).toEqual(new Uint8Array([84])); // 42 zigzag encoded
197197

198198
writer.reset();
199-
199+
200200
// Integer outside int range
201201
encoder.writeNumber(3000000000);
202202
result = writer.flush();
203203
expect(result.length).toBeGreaterThan(1);
204204

205205
writer.reset();
206-
206+
207207
// Float
208208
encoder.writeNumber(3.14);
209209
result = writer.flush();
@@ -284,4 +284,4 @@ describe('AvroEncoder', () => {
284284
expect(result.length).toBe(8);
285285
});
286286
});
287-
});
287+
});

src/avro/__tests__/AvroSchemaEncoder.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describe('AvroSchemaEncoder', () => {
249249

250250
test('encodes union value with explicit index', () => {
251251
const schema: AvroUnionSchema = ['null', 'string'];
252-
252+
253253
const result = encoder.encode('hello', schema, 1);
254254
expect(result[0]).toBe(2); // index 1 zigzag encoded is 2
255255
});
@@ -403,4 +403,4 @@ describe('AvroSchemaEncoder', () => {
403403
expect(result.length).toBeGreaterThan(0);
404404
});
405405
});
406-
});
406+
});

src/avro/__tests__/AvroSchemaValidator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,4 @@ describe('AvroSchemaValidator', () => {
357357
expect(validator.validateValue(new Uint8Array([1, 2, 3, 4, 5]), schema)).toBe(false);
358358
});
359359
});
360-
});
360+
});

src/avro/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './types';
22
export * from './AvroSchemaValidator';
33
export * from './AvroEncoder';
4-
export * from './AvroSchemaEncoder';
4+
export * from './AvroSchemaEncoder';

0 commit comments

Comments
 (0)