Skip to content

Commit

Permalink
defaults for non-optional mutable members (#156)
Browse files Browse the repository at this point in the history
### Public-Facing Changes
<!-- describe any changes to the public interface or APIs, or write
"None" -->
- non-optional fields for mutable structs and unions now receive default
values

### Description

Most of this is done in the DeserializationInfoCache since we generally
want to cache default values for complex types.
Primitive fields receive default values per spec. Default values for
fixed size arrays are given
Default unions use a default case if one exists, otherwise they use the
case selected by the default value of the switch case type.
Default structs are filled with non-optional members' default values.

Reference: https://www.omg.org/spec/DDS-XTypes/1.3/PDF
7.2.2.4.4.4.7

![image](https://github.com/foxglove/omgidl/assets/10187776/28be56ff-69f5-4fcd-bbce-d504a3099858)

<!-- Link relevant Github issues. Use `Fixes #1234` to auto-close the
issue after merging. -->
FG-6554
  • Loading branch information
snosenzo authored Apr 10, 2024
1 parent 22ab1b9 commit 52adfbd
Show file tree
Hide file tree
Showing 5 changed files with 454 additions and 35 deletions.
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;
/** 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;
/** 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 {
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
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>([
["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,
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

0 comments on commit 52adfbd

Please sign in to comment.