Skip to content

Commit

Permalink
- feedback
Browse files Browse the repository at this point in the history
- update for cdr 3.0.0
  • Loading branch information
snosenzo committed Aug 11, 2023
1 parent 37f2013 commit 19746b0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/omgidl-serialization/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"typescript": "4.9.5"
},
"dependencies": {
"@foxglove/cdr": "2.1.0",
"@foxglove/cdr": "3.0.0",
"@foxglove/message-definition": "^0.2.0"
}
}
43 changes: 28 additions & 15 deletions packages/omgidl-serialization/src/MessageReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import {

export type Deserializer = (
reader: CdrReader,
/**
* Optional length only applied for string types as character length.
* Prevents reader from reading sequence length again if already read via header.
*/
length?: number,
) => boolean | number | bigint | string;

export type ArrayDeserializer = (
reader: CdrReader,
count: number,
Expand Down Expand Up @@ -93,7 +98,7 @@ export class MessageReader<T = unknown> {
let fieldLength = field.arrayLength;
if (readMemberHeader) {
const { id, objectSize: objectSizeBytes } = reader.emHeader();
const itemSize = typeToByteLength[field.type];
const itemSize = typeToByteLength(field.type);
if (itemSize != undefined) {
fieldLength ??= Math.ceil(objectSizeBytes / itemSize);
}
Expand Down Expand Up @@ -147,20 +152,28 @@ export class MessageReader<T = unknown> {
}
}

const typeToByteLength: Record<string, number> = {
bool: 1,
int8: 1,
uint8: 1,
int16: 2,
uint16: 2,
int32: 4,
uint32: 4,
int64: 8,
uint64: 8,
float32: 4,
float64: 8,
string: 1,
};
function typeToByteLength(type: string): number | undefined {
switch (type) {
case "bool":
case "int8":
case "uint8":
case "string":
return 1;
case "int16":
case "uint16":
return 2;
case "int32":
case "uint32":
case "float32":
return 4;
case "int64":
case "uint64":
case "float64":
return 8;
default:
return undefined;
}
}

const deserializers = new Map<string, Deserializer>([
["bool", (reader) => Boolean(reader.int8())],
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,10 @@ __metadata:
languageName: node
linkType: hard

"@foxglove/cdr@npm:2.1.0":
version: 2.1.0
resolution: "@foxglove/cdr@npm:2.1.0"
checksum: 153632a8911a76d047d896f0a17e12b5a6779465a35790c2fe349efc0eb8a618c877d015a066d8b13a804f0c72131a7e9ca0bf48ed116aa3095a3d7252ab1062
"@foxglove/cdr@npm:3.0.0":
version: 3.0.0
resolution: "@foxglove/cdr@npm:3.0.0"
checksum: 3ad9b7f92b842aaf8e6f968d0deb060e8cd5b63ee536fce34563bca66d4cb8f488cb03d3b77481a68bbbcbd5fb11e49181575585644046f38401a678218fd589
languageName: node
linkType: hard

Expand Down Expand Up @@ -534,7 +534,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@foxglove/omgidl-serialization@workspace:packages/omgidl-serialization"
dependencies:
"@foxglove/cdr": 2.1.0
"@foxglove/cdr": 3.0.0
"@foxglove/message-definition": ^0.2.0
"@foxglove/omgidl-parser": "workspace:*"
"@sounisi5011/jest-binary-data-matchers": 1.2.1
Expand Down

0 comments on commit 19746b0

Please sign in to comment.