Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defaults for non-optional mutable members #156

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 125 additions & 1 deletion packages/omgidl-serialization/src/DeserializationInfoCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { IDLMessageDefinition } from "@foxglove/omgidl-parser";
import { IDLMessageDefinition, IDLMessageDefinitionField } from "@foxglove/omgidl-parser";

import {
DeserializationInfoCache,
FieldDeserializationInfo,
PRIMITIVE_ARRAY_DESERIALIZERS,
PRIMITIVE_DESERIALIZERS,
} from "./DeserializationInfoCache";
import { UNION_DISCRIMINATOR_PROPERTY_KEY } from "./constants";

const TRANSFORM_DEFINITION: IDLMessageDefinition = {
name: "geometry_msgs::msg::Transform",
Expand Down Expand Up @@ -51,6 +53,16 @@ const FLOAT64_PRIMITIVE_DESER_INFO = {
},
};

function makeFieldDeserFromComplexDef(
complexDefinition: IDLMessageDefinition,
deserializationInfoCache: DeserializationInfoCache,
): FieldDeserializationInfo {
return deserializationInfoCache.buildFieldDeserInfo({
name: `${complexDefinition.name ?? ""}_field`,
type: complexDefinition.name ?? "",
isComplex: true,
} as IDLMessageDefinitionField);
}
describe("DeserializationInfoCache", () => {
it("creates deserialization info for struct with primitive fields", () => {
const deserializationInfoCache = new DeserializationInfoCache([TIME_DEFINITION]);
Expand Down Expand Up @@ -239,7 +251,119 @@ describe("DeserializationInfoCache", () => {
},
});
});
it("creates default value for struct with primitive fields", () => {
const deserializationInfoCache = new DeserializationInfoCache([TIME_DEFINITION]);
const fieldDeserInfo = makeFieldDeserFromComplexDef(TIME_DEFINITION, deserializationInfoCache);
expect(deserializationInfoCache.getFieldDefault(fieldDeserInfo)).toMatchObject({
sec: 0,
nanosec: 0,
});
});

it("creates default value for struct with complex fields", () => {
const deserializationInfoCache = new DeserializationInfoCache([
TRANSFORM_DEFINITION,
VECTOR_DEFINITION,
QUATERNION_DEFINITION,
]);
const fieldDeserInfo = makeFieldDeserFromComplexDef(
TRANSFORM_DEFINITION,
deserializationInfoCache,
);
expect(deserializationInfoCache.getFieldDefault(fieldDeserInfo)).toMatchObject({
translation: { x: 0, y: 0, z: 0 },
rotation: { x: 0, y: 0, z: 0, w: 0 },
});
});

it("creates default value for primitive field", () => {
const deserializationInfoCache = new DeserializationInfoCache([]);
const fieldDeserInfo = deserializationInfoCache.buildFieldDeserInfo({
name: "some_field_name",
type: "float64",
});
expect(deserializationInfoCache.getFieldDefault(fieldDeserInfo)).toEqual(0);
});
it("creates correct default for a complex array field", () => {
const deserializationInfoCache = new DeserializationInfoCache([VECTOR_DEFINITION]);
const vectorFieldDeserInfo = deserializationInfoCache.buildFieldDeserInfo({
isComplex: true,
isArray: true,
name: "vectors",
type: "geometry_msgs::msg::Vector3",
});
expect(deserializationInfoCache.getFieldDefault(vectorFieldDeserInfo)).toEqual([]);
});
it("creates correct default for a union field with a default case", () => {
const unionDefinition: IDLMessageDefinition = {
name: "test::Union",
aggregatedKind: "union",
switchType: "uint32",
cases: [
{
predicates: [0],
type: { name: "a", type: "int32", isComplex: false },
},
{
predicates: [1],
type: { name: "b", type: "float64", isComplex: false },
},
],
defaultCase: { name: "c", type: "string", isComplex: false },
};
const deserializationInfoCache = new DeserializationInfoCache([unionDefinition]);
const fieldDeserInfo = makeFieldDeserFromComplexDef(unionDefinition, deserializationInfoCache);
expect(deserializationInfoCache.getFieldDefault(fieldDeserInfo)).toEqual({
[UNION_DISCRIMINATOR_PROPERTY_KEY]: undefined,
c: "",
});
});
it("creates correct default for a union field without a default case", () => {
const unionDefinition: IDLMessageDefinition = {
name: "test::Union",
aggregatedKind: "union",
switchType: "uint32",
cases: [
{
predicates: [1],
type: { name: "a", type: "uint8", isComplex: false },
},
{
predicates: [0], // default case because default value for switch case is 0
type: { name: "b", type: "float64", isComplex: false },
},
],
};
const deserializationInfoCache = new DeserializationInfoCache([unionDefinition]);
const fieldDeserInfo = makeFieldDeserFromComplexDef(unionDefinition, deserializationInfoCache);
expect(deserializationInfoCache.getFieldDefault(fieldDeserInfo)).toEqual({
[UNION_DISCRIMINATOR_PROPERTY_KEY]: 0,
b: 0,
});
});
it("creates correct default for a struct field with optional and non-optional members", () => {
const unionDefinition: IDLMessageDefinition = {
name: "test::Message",
aggregatedKind: "struct",
definitions: [
{ name: "a", type: "uint32", isComplex: false },
{
name: "b",
type: "uint32",
isComplex: false,
annotations: {
optional: { type: "no-params", name: "optional" },
},
},
],
};
const deserializationInfoCache = new DeserializationInfoCache([unionDefinition]);
const fieldDeserInfo = makeFieldDeserFromComplexDef(unionDefinition, deserializationInfoCache);
expect(deserializationInfoCache.getFieldDefault(fieldDeserInfo)).toEqual({
a: 0,
b: undefined,
});
});
it("throws if required type definitions are not found", () => {
const deserializationInfoCache = new DeserializationInfoCache([TIME_DEFINITION]);
expect(() =>
Expand Down
162 changes: 160 additions & 2 deletions packages/omgidl-serialization/src/DeserializationInfoCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ import { CdrReader } from "@foxglove/cdr";
import {
IDLMessageDefinition,
IDLMessageDefinitionField,
IDLStructDefinition,
IDLUnionDefinition,
} from "@foxglove/omgidl-parser";

import { UNION_DISCRIMINATOR_PROPERTY_KEY } from "./constants";
import {
DEFAULT_BOOLEAN_VALUE,
DEFAULT_BYTE_VALUE,
DEFAULT_NUMERICAL_VALUE,
DEFAULT_STRING_VALUE,
} from "./defaultValues";

export type Deserializer = (
reader: CdrReader,
/**
Expand Down Expand Up @@ -51,6 +60,7 @@ export type PrimitiveArrayDeserializationInfo = {
export type StructDeserializationInfo = HeaderOptions & {
type: "struct";
fields: FieldDeserializationInfo[];
definition: IDLStructDefinition;
achim-k marked this conversation as resolved.
Show resolved Hide resolved
};

export type UnionDeserializationInfo = HeaderOptions & {
Expand All @@ -65,13 +75,23 @@ export type PrimitiveTypeDeserInfo =
| PrimitiveDeserializationInfo
| PrimitiveArrayDeserializationInfo;

export type FieldDeserializationInfo = {
export type FieldDeserializationInfo<
DeserInfo extends ComplexDeserializationInfo | PrimitiveTypeDeserInfo =
| ComplexDeserializationInfo
| PrimitiveTypeDeserInfo,
> = {
name: string;
type: string;
typeDeserInfo: ComplexDeserializationInfo | PrimitiveTypeDeserInfo;
typeDeserInfo: DeserInfo;
isArray?: boolean;
arrayLengths?: number[];
definitionId?: number;
/** Optional fields show undefined if not present in the message.
* Non-optional fields show a default value per the spec:
* https://www.omg.org/spec/DDS-XTypes/1.3/PDF 7.2.2.4.4.4.7
*/
isOptional: boolean;
isComplex: DeserInfo extends ComplexDeserializationInfo ? true : false;
snosenzo marked this conversation as resolved.
Show resolved Hide resolved
achim-k marked this conversation as resolved.
Show resolved Hide resolved
};

export class DeserializationInfoCache {
Expand Down Expand Up @@ -131,6 +151,7 @@ export class DeserializationInfoCache {
const deserInfo: StructDeserializationInfo = {
type: "struct",
...getHeaderNeeds(definition),
definition,
fields: definition.definitions.reduce(
(fieldsAccum, fieldDef) =>
fieldDef.isConstant === true
Expand Down Expand Up @@ -170,6 +191,8 @@ export class DeserializationInfoCache {
isArray,
arrayLengths,
definitionId: getDefinitionId(definition),
isOptional: isOptional(definition),
isComplex: true,
};
}

Expand Down Expand Up @@ -209,11 +232,99 @@ export class DeserializationInfoCache {
name,
type,
typeDeserInfo: fieldDeserInfo,
isComplex: false,
isArray,
arrayLengths,
definitionId: getDefinitionId(definition),
isOptional: isOptional(definition),
};
}

public getFieldDefault(deserInfo: FieldDeserializationInfo): unknown {
achim-k marked this conversation as resolved.
Show resolved Hide resolved
const { isArray, arrayLengths, type, isComplex } = deserInfo;

if (isArray === true && arrayLengths == undefined) {
return [];
}
let defaultValueGetter;
if (isComplex) {
defaultValueGetter = this.#createOrGetComplexDefaultValueGetter(
deserInfo.typeDeserInfo as ComplexDeserializationInfo,
);
} else {
// fixed length arrays are filled with default values
defaultValueGetter = PRIMITIVE_DEFAULT_VALUE_GETTERS.get(type);
if (!defaultValueGetter) {
throw new Error(`Failed to find default value getter for type ${type}`);
}
}
// Used for fixed length arrays that may be nested
const needsNestedArray = isArray === true && arrayLengths != undefined;
return needsNestedArray
? makeNestedArray(defaultValueGetter, arrayLengths, 0)
: defaultValueGetter();
}

#complexDefaultValueGetters = new Map<string, () => Record<string, unknown>>();

#createOrGetComplexDefaultValueGetter = (
deserInfo: ComplexDeserializationInfo,
): (() => Record<string, unknown>) => {
const mapKey = deserInfo.definition.name ?? "";

if (this.#complexDefaultValueGetters.has(mapKey)) {
return this.#complexDefaultValueGetters.get(mapKey)!;
}
let defaultMessage: Record<string, unknown> | undefined = undefined;
const defaultValueGetter: () => Record<string, unknown> = () => {
// if `structuredClone` is part of the environment, use it to clone the default message
// want to avoid defaultValues having references to the same object
if (defaultMessage != undefined && typeof structuredClone !== "undefined") {
return structuredClone(defaultMessage);
}
if (deserInfo.type === "union") {
const { definition: unionDef } = deserInfo;
const { switchType } = unionDef;

defaultMessage = {};
let defaultCase: IDLMessageDefinitionField | undefined = unionDef.defaultCase;
// use existing default case if there is one
if (!defaultCase) {
// choose default based off of default value of switch case type
const switchTypeDefaultGetter = PRIMITIVE_DEFAULT_VALUE_GETTERS.get(switchType);
if (switchTypeDefaultGetter == undefined) {
throw new Error(`Failed to find default value getter for type ${switchType}`);
}
const switchValue = switchTypeDefaultGetter() as number | boolean;

defaultCase = unionDef.cases.find((c) => c.predicates.includes(switchValue))?.type;
if (!defaultCase) {
throw new Error(`Failed to find default case for union ${unionDef.name ?? ""}`);
}
defaultMessage[UNION_DISCRIMINATOR_PROPERTY_KEY] = switchValue;
} else {
// default exists, default value of switch case type is not needed
defaultMessage[UNION_DISCRIMINATOR_PROPERTY_KEY] = undefined;
}
const defaultCaseDeserInfo = this.buildFieldDeserInfo(defaultCase);
defaultMessage[defaultCaseDeserInfo.name] = this.getFieldDefault(defaultCaseDeserInfo);
} else if (deserInfo.type === "struct") {
defaultMessage = {};
for (const field of deserInfo.fields) {
if (!field.isOptional) {
defaultMessage[field.name] = this.getFieldDefault(field);
}
}
}

if (defaultMessage == undefined) {
throw new Error(`Unrecognized complex type ${deserInfo.type as string}`);
}
return defaultMessage;
};
this.#complexDefaultValueGetters.set(deserInfo.definition.name ?? "", defaultValueGetter);
return defaultValueGetter;
};
}

function typeToByteLength(type: string): number | undefined {
Expand Down Expand Up @@ -269,6 +380,43 @@ export const PRIMITIVE_ARRAY_DESERIALIZERS = new Map<string, ArrayDeserializer>(
["string", readStringArray],
]);

export const PRIMITIVE_DEFAULT_VALUE_GETTERS = new Map<string, () => unknown>([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can that be a <string, unknown> map instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can, but because this is how the default values are accessed by complex types as well I think it's preferable to keep them consistent.

["bool", () => DEFAULT_BOOLEAN_VALUE],
["int8", () => DEFAULT_BYTE_VALUE],
["uint8", () => DEFAULT_BYTE_VALUE],
["int16", () => DEFAULT_NUMERICAL_VALUE],
["uint16", () => DEFAULT_NUMERICAL_VALUE],
["int32", () => DEFAULT_NUMERICAL_VALUE],
["uint32", () => DEFAULT_NUMERICAL_VALUE],
["int64", () => DEFAULT_NUMERICAL_VALUE],
["uint64", () => DEFAULT_NUMERICAL_VALUE],
["float32", () => DEFAULT_NUMERICAL_VALUE],
["float64", () => DEFAULT_NUMERICAL_VALUE],
["string", () => DEFAULT_STRING_VALUE],
]);

export function makeNestedArray(
getValue: () => unknown,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just pass value: unknown here instead of a getter function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use getValue instead of value here because I want to prevent assigning all of the values to the same reference if the value is not a primitive.

arrayLengths: number[],
depth: number,
): unknown[] {
if (depth > arrayLengths.length - 1 || depth < 0) {
throw Error(`Invalid depth ${depth} for array of length ${arrayLengths.length}`);
}

const array = [];

for (let i = 0; i < arrayLengths[depth]!; i++) {
if (depth === arrayLengths.length - 1) {
array.push(getValue());
} else {
array.push(makeNestedArray(getValue, arrayLengths, depth + 1));
}
}

return array;
}

function readBoolArray(reader: CdrReader, count: number): boolean[] {
const array = new Array<boolean>(count);
for (let i = 0; i < count; i++) {
Expand All @@ -285,6 +433,16 @@ function readStringArray(reader: CdrReader, count: number): string[] {
return array;
}

function isOptional(definition: IDLMessageDefinitionField): boolean {
const { annotations } = definition;

if (!annotations) {
return false;
}

return "optional" in annotations;
}

function getHeaderNeeds(definition: IDLMessageDefinition): {
usesDelimiterHeader: boolean;
usesMemberHeader: boolean;
Expand Down
Loading