forked from jlandersen/vscode-kafka
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Declare key/value format for CONSUMER in kafka file
Fixes jlandersen#112 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
bf47c3d
commit dc97dd0
Showing
9 changed files
with
220 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
export type MessageFormat = "none" | "double" | "float" | "integer" | "long" | "short"; | ||
|
||
export type SerializationdResult = any | Error; | ||
|
||
class SerializationException extends Error { } | ||
|
||
// ---------------- Deserializers ---------------- | ||
|
||
interface Deserializer { | ||
deserialize(data: Buffer): any; | ||
} | ||
|
||
const deserializerRegistry: Map<MessageFormat, Deserializer> = new Map(); | ||
|
||
export function deserialize(data: Buffer | null, format?: MessageFormat): SerializationdResult | null { | ||
if (data === null || !format || format === "none") { | ||
return null; | ||
} | ||
try { | ||
const deserializer = getDeserializer(format); | ||
if (!deserializer) { | ||
throw new SerializationException(`Cannot find a deserializer for ${format} format.`); | ||
} | ||
return deserializer.deserialize(data); | ||
} | ||
catch (e) { | ||
return e; | ||
} | ||
} | ||
|
||
function getDeserializer(format: MessageFormat): Deserializer | undefined { | ||
return deserializerRegistry.get(format); | ||
} | ||
|
||
class DoubleDeserializer implements Deserializer { | ||
|
||
deserialize(data: Buffer | null): any { | ||
if (data === null) { | ||
return null; | ||
} | ||
if (data.length !== 8) { | ||
throw new SerializationException("Size of data received by Deserializer is not 8"); | ||
} | ||
return data.readDoubleBE(0); | ||
} | ||
} | ||
|
||
class FloatDeserializer implements Deserializer { | ||
|
||
deserialize(data: Buffer | null): any { | ||
if (data === null) { | ||
return null; | ||
} | ||
if (data.length !== 4) { | ||
throw new SerializationException("Size of data received by Deserializer is not 4"); | ||
} | ||
return data.readFloatBE(0); | ||
} | ||
} | ||
|
||
class IntegerDeserializer implements Deserializer { | ||
|
||
deserialize(data: Buffer | null): any { | ||
if (data === null) { | ||
return null; | ||
} | ||
if (data.length !== 4) { | ||
throw new Error("Size of data received by IntegerDeserializer is not 4"); | ||
} | ||
return data.readUInt32BE(0); | ||
} | ||
} | ||
|
||
class LongDeserializer implements Deserializer { | ||
|
||
deserialize(data: Buffer | null): any { | ||
if (data === null) { | ||
return null; | ||
} | ||
if (data.length !== 8) { | ||
throw new SerializationException("Size of data received by LongDeserializer is not 8"); | ||
} | ||
/*let value = 0; | ||
for (const b of data) { | ||
value <<= 8; | ||
value |= b & 0xFF; | ||
} | ||
return value;*/ | ||
return data.readBigUInt64BE(0); | ||
} | ||
} | ||
|
||
class ShortDeserializer implements Deserializer { | ||
|
||
deserialize(data: Buffer | null): any { | ||
if (data === null) { | ||
return null; | ||
} | ||
if (data.length !== 2) { | ||
throw new SerializationException("Size of data received by ShortDeserializer is not 2"); | ||
} | ||
return data.readUInt16BE(0); | ||
} | ||
} | ||
|
||
deserializerRegistry.set("double", new DoubleDeserializer()); | ||
deserializerRegistry.set("float", new FloatDeserializer()); | ||
deserializerRegistry.set("integer", new IntegerDeserializer()); | ||
deserializerRegistry.set("long", new LongDeserializer()); | ||
deserializerRegistry.set("short", new ShortDeserializer()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as assert from "assert"; | ||
import { deserialize } from "../../../client/serialization"; | ||
|
||
suite("Deserializer Test Suite", () => { | ||
|
||
test("Integer deserializer", () => { | ||
|
||
assert.deepStrictEqual( | ||
deserialize(Buffer.from([0, 0, 0, 123]), "integer"), | ||
123 | ||
); | ||
}); | ||
|
||
/*test("Long deserializer", () => { | ||
assert.deepStrictEqual( | ||
deserialize(Buffer.from([0, 0, 0, 123, 0, 0, 0, 123]), "long"), | ||
123 | ||
); | ||
});*/ | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters