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

Allow unions to be read when there is no matching case or default for the specified discriminator value #206

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
81 changes: 81 additions & 0 deletions packages/omgidl-serialization/src/MessageReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,87 @@ module builtin_interfaces {
},
});
});

it("Reads mutable union with default case with id where discriminator case does not exist", () => {
const msgDef = `
@mutable
union ColorOrGray switch (uint8) {
case 0:
@id(100)
uint8 rgb[3];
default:
@id(200)
uint8 gray;
};
@mutable
struct Fence {
@id(5) ColorOrGray color;
};
`;

const writer = new CdrWriter({ kind: EncapsulationKind.PL_CDR_LE });
writer.emHeader(true, 5, 6); // writes emHeader for color field

writer.emHeader(true, 1, 1); // emHeader for discriminator (switch type)
writer.uint8(0x09); // then writes uint8 case for gray

writer.emHeader(true, 200, 1); // emHeader for field (gray)
writer.uint8(55); // then writes uint8

writer.sentinelHeader(); // end union
writer.sentinelHeader(); // end struct

const rootDef = "Fence";
const reader = new MessageReader(rootDef, parseIDL(msgDef));

const msgout = reader.readMessage(writer.data);

expect(msgout).toEqual({
color: {
[UNION_DISCRIMINATOR_PROPERTY_KEY]: 9,
gray: 55,
},
});
});
it("Reads mutable union field with with id where discriminator case does not exist and there is no default", () => {
const msgDef = `
@mutable
union ColorOrGray switch (uint8) {
case 0:
@id(100)
uint8 rgb[3];
case 3:
@id(200)
uint8 gray;
};
@mutable
struct Fence {
@id(5) ColorOrGray color;
};
`;

const writer = new CdrWriter({ kind: EncapsulationKind.PL_CDR_LE });
writer.emHeader(true, 5, 6); // writes emHeader for color field

writer.emHeader(true, 1, 1); // emHeader for discriminator (switch type)
writer.uint8(0x09); // then writes uint8 case for gray

// absent value because discriminator doesn't exist

writer.sentinelHeader(); // end union
writer.sentinelHeader(); // end struct

const rootDef = "Fence";
const reader = new MessageReader(rootDef, parseIDL(msgDef));

const msgout = reader.readMessage(writer.data);

expect(msgout).toEqual({
color: {
[UNION_DISCRIMINATOR_PROPERTY_KEY]: 9,
},
});
});
it("Reads array from mutable union field", () => {
const msgDef = `
@mutable
Expand Down
21 changes: 14 additions & 7 deletions packages/omgidl-serialization/src/MessageReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,24 @@ export class MessageReader<T = unknown> {
}
const discriminatorValue = deserInfo.switchTypeDeser(reader) as number | boolean;

// Discriminator case determination: Section 7.4.1.4.4.4.2 of https://www.omg.org/spec/IDL/4.2/PDF
// get case for switchtype value based on matching predicate
const caseDefType = getCaseForDiscriminator(deserInfo.definition, discriminatorValue);
let caseDefType = getCaseForDiscriminator(deserInfo.definition, discriminatorValue);
// If no case is found, use the default case
if (!caseDefType) {
throw new Error(
`No matching case found in ${
deserInfo.definition.name ?? ""
} for discriminator value ${discriminatorValue.toString()}`,
);
caseDefType = deserInfo.definition.defaultCase;
}

const fieldDeserInfo = this.deserializationInfoCache.buildFieldDeserInfo(caseDefType);
const fieldDeserInfo = caseDefType
? this.deserializationInfoCache.buildFieldDeserInfo(caseDefType)
: undefined;

// if no matching case and no default case, only return discriminator value
if (!fieldDeserInfo || !caseDefType) {
return {
[UNION_DISCRIMINATOR_PROPERTY_KEY]: discriminatorValue,
};
}

return {
[UNION_DISCRIMINATOR_PROPERTY_KEY]: discriminatorValue,
Expand Down