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: Allow to decode t as opt t #716

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
2 changes: 1 addition & 1 deletion docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1>Agent-JS Changelog</h1>
<section>
<h2>Version x.x.x</h2>
<ul>
<li></li>
<li>feat: Add support for type coercion of `t` to `opt t` when decoding candid values.</li>
</ul>
<h2>Version 0.15.5</h2>
<ul>
Expand Down
17 changes: 16 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/,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The formatter insists on this change.

);
});

test('IDL encoding (array)', () => {
Expand Down Expand Up @@ -677,3 +679,16 @@ test('should decode matching optional fields if wire type contains additional fi
b: ['123'],
});
});

test('should coerce value to opt value', () => {
const encoded = IDL.encode([IDL.Text], ['hello']);
const value = IDL.decode([IDL.Opt(IDL.Text)], encoded)[0] as [string];
expect(value).toEqual(['hello']);
});

test('should not coerce nested opt opt', () => {
const encoded = IDL.encode([IDL.Text], ['hello']);
expect(() => IDL.decode([IDL.Opt(IDL.Opt(IDL.Text))], encoded)).toThrow(
'type mismatch: type on the wire text, expect type opt opt text',
);
});
17 changes: 16 additions & 1 deletion packages/candid/src/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,10 +914,25 @@ export class OptClass<T> extends ConstructType<[T] | []> {
typeTable.add(this, concat(opCode, buffer));
}

public checkType(t: Type): Type {
try {
return super.checkType(t);
} catch (e) {
// try to coerce t to opt t
if (!(t instanceof OptClass) && !(this._type instanceof OptClass)) {
if (this._type.checkType(t)) {
return t;
}
}
// rethrow if opt coercion is not applicable
throw e;
}
}

public decodeValue(b: Pipe, t: Type): [T] | [] {
const opt = this.checkType(t);
if (!(opt instanceof OptClass)) {
throw new Error('Not an option type');
return [this._type.decodeValue(b, opt)];
Copy link

@stopak stopak May 9, 2023

Choose a reason for hiding this comment

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

I did some tests, and this should be a little bit different

    const type = Rec()
    type._type = opt

    return [this._type.decodeValue(b, type)];

Otherwise this will throw another error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Could you provide a full test-case of an input the crashes with the current code but succeeds using your suggested change?
It would be helpful to add it to the actual test suite.

}
switch (safeReadUint8(b)) {
case 0:
Expand Down