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 all 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
164 changes: 162 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,27 +60,44 @@ export type PrimitiveArrayDeserializationInfo = {
export type StructDeserializationInfo = HeaderOptions & {
type: "struct";
fields: FieldDeserializationInfo[];
definition: IDLStructDefinition;
achim-k marked this conversation as resolved.
Show resolved Hide resolved
/** optional allows for defaultValues to be calculated lazily */
defaultValue?: Record<string, unknown>;
};

export type UnionDeserializationInfo = HeaderOptions & {
type: "union";
switchTypeDeser: Deserializer;
switchTypeLength: number;
definition: IDLUnionDefinition;
/** optional allows for defaultValues to be calculated lazily */
defaultValue?: Record<string, unknown>;
};

export type ComplexDeserializationInfo = StructDeserializationInfo | UnionDeserializationInfo;
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
/** optional allows for defaultValues to be calculated lazily */
defaultValue?: unknown;
};

export class DeserializationInfoCache {
Expand Down Expand Up @@ -131,6 +157,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 +197,8 @@ export class DeserializationInfoCache {
isArray,
arrayLengths,
definitionId: getDefinitionId(definition),
isOptional: isOptional(definition),
isComplex: true,
};
}

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

/** Returns default value for given FieldDeserializationInfo.
* If defaultValue is not defined on FieldDeserializationInfo, it will be calculated and set.
*/
public getFieldDefault(deserInfo: FieldDeserializationInfo): unknown {
achim-k marked this conversation as resolved.
Show resolved Hide resolved
if (deserInfo.defaultValue != undefined) {
return deserInfo.defaultValue;
}
const { isArray, arrayLengths, type, isComplex } = deserInfo;

if (isArray === true && arrayLengths == undefined) {
deserInfo.defaultValue = [];
return deserInfo.defaultValue;
}
let defaultValueGetter;
if (isComplex) {
defaultValueGetter = () => {
return this.#getComplexDeserInfoDefault(
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;
deserInfo.defaultValue = needsNestedArray
? makeNestedArray(defaultValueGetter, arrayLengths, 0)
: defaultValueGetter();
return deserInfo.defaultValue;
}

/** Computes and sets the default value on the complex deserialization info */
#getComplexDeserInfoDefault(deserInfo: ComplexDeserializationInfo): 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
Comment on lines +286 to +287
Copy link
Contributor

Choose a reason for hiding this comment

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

in what cases would this be bad?

Copy link
Collaborator Author

@snosenzo snosenzo Apr 9, 2024

Choose a reason for hiding this comment

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

If a message contains several instances of a single default value, and the user of the library mutates a single one of those values, then all of the instances will be changed because they point to the same reference. This also includes default values across all messages read by an instance of this parser.
We don't do this in studio, and it is an added cost to instantiate a new instance of this value each time, but I think that's what users of this library will expect and it will save time fighting against potential errors that could come from manipulating (accidentally or otherwise) these objects sharing the same reference.

if (deserInfo.defaultValue != undefined && typeof structuredClone !== "undefined") {
return structuredClone(deserInfo.defaultValue);
}
deserInfo.defaultValue = {};
const defaultMessage = deserInfo.defaultValue;
if (deserInfo.type === "union") {
const { definition: unionDef } = deserInfo;
const { switchType } = unionDef;

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") {
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;
}
}

function typeToByteLength(type: string): number | undefined {
Expand Down Expand Up @@ -269,6 +382,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 +435,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