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

feat: Optional fields are optional #668

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
29 changes: 28 additions & 1 deletion packages/candid/src/idl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ test('IDL encoding (arraybuffer)', () => {
IDL.encode([IDL.Vec(IDL.Nat8)], [new Uint8Array()]);
IDL.encode([IDL.Vec(IDL.Nat8)], [new Uint8Array(100).fill(42)]);
IDL.encode([IDL.Vec(IDL.Nat16)], [new Uint16Array(200).fill(42)]);
expect(() => IDL.encode([IDL.Vec(IDL.Int8)], [new Uint16Array(10).fill(420)])).toThrow(/Invalid vec int8 argument/);
expect(() => IDL.encode([IDL.Vec(IDL.Int8)], [new Uint16Array(10).fill(420)])).toThrow(
/Invalid vec int8 argument/,
);
});

test('IDL encoding (array)', () => {
Expand Down Expand Up @@ -641,6 +643,31 @@ test('decode / encode unknown nested record', () => {
expect(decodedValue2).toEqual(value);
});

test.only('should allow records with missing optional fields', () => {
const ExtendedRecord = IDL.Record({
foo: IDL.Int32,
bar: IDL.Bool,
bool: IDL.Opt(IDL.Bool),
int: IDL.Opt(IDL.Int),
nat: IDL.Opt(IDL.Nat),
nat32: IDL.Opt(IDL.Nat32),
nat64: IDL.Opt(IDL.Nat64),
float32: IDL.Opt(IDL.Float32),
float64: IDL.Opt(IDL.Float64),
text: IDL.Opt(IDL.Text),
null: IDL.Opt(IDL.Null),
reserved: IDL.Opt(IDL.Reserved),
principal: IDL.Opt(IDL.Principal),
nested: IDL.Opt(IDL.Opt(IDL.Nat)),
});

const encoded = IDL.encode([ExtendedRecord], [{ foo: 42, bar: true }]);

expect(toHexString(encoded)).toMatchInlineSnapshot(
`"4449444c0d6e7c6e7d6e686e7e6e7f6e716e706e016e796e786e736e726c0ed3e3aa027e868eb70275ef99c00200e199cf0201ae9db1900102aa88ee88040387bdbac80404ad99e7e70405a8ed97f50406f7ddd8f80607c0e7a6b40908dfeca6b40909bba2a2d00e0adaa7a2d00e0b010c012a000000000000000000000000000000"`,
);
});

test('should correctly decode expected optional fields with lower hash than required fields', () => {
const HttpResponse = IDL.Record({
body: IDL.Text,
Expand Down
14 changes: 10 additions & 4 deletions packages/candid/src/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ const magicNumber = 'DIDL';
const toReadableString_max = 400; // will not display arguments after 400chars. Makes sure 2mb blobs don't get inside the error

function zipWith<TX, TY, TR>(xs: TX[], ys: TY[], f: (a: TX, b: TY) => TR): TR[] {
return xs.map((x, i) => f(x, ys[i]));
return xs.map((x, i) => {
return f(x, ys[i]);
});
}

/**
Expand Down Expand Up @@ -978,9 +980,13 @@ export class RecordClass extends ConstructType<Record<string, any>> {
if (
typeof x === 'object' &&
this._fields.every(([k, t]) => {
// eslint-disable-next-line
if (!x.hasOwnProperty(k)) {
throw new Error(`Record is missing key "${k}".`);
if (!(k in x)) {
// Allow missing optional fields.
if (t instanceof OptClass) {
x[k] = [];
} else {
throw new Error(`Record is missing key "${k}".`);
}
}
try {
return t.covariant(x[k]);
Expand Down