-
Notifications
You must be signed in to change notification settings - Fork 97
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
base: main
Are you sure you want to change the base?
Conversation
size-limit report 📦
|
This is a strawman PR to implement coercion of `t` to `opt t` when decoding values.
a58d018
to
08aa25c
Compare
@@ -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/, |
There was a problem hiding this comment.
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.
This is something that I've also encountered lately, if possible it would be great to merge it- |
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)]; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Description
This is a strawman PR to implement coercion of
t
toopt t
when decoding values.The test suite still passes, but I'm not sure if I broke something else in the process.
Also it uses control flow by exception, which is rather ugly.
Fixes # (issue)
How Has This Been Tested?
Unit tests have been added to test the feature.
Checklist: