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

Conversation

frederikrothenberger
Copy link
Contributor

@frederikrothenberger frederikrothenberger commented Apr 25, 2023

Description

This is a strawman PR to implement coercion of t to opt 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:

  • My changes follow the guidelines in CONTRIBUTING.md.
  • The title of this PR complies with Conventional Commits.
  • I have edited the CHANGELOG accordingly.
  • I have made corresponding changes to the documentation.

@frederikrothenberger frederikrothenberger marked this pull request as draft April 25, 2023 13:38
@github-actions
Copy link
Contributor

github-actions bot commented Apr 25, 2023

size-limit report 📦

Path Size
@dfinity/agent 88.88 KB (+0.05% 🔺)
@dfinity/candid 15.5 KB (+0.29% 🔺)
@dfinity/principal 6.77 KB (0%)
@dfinity/auth-client 95.04 KB (0%)
@dfinity/assets 91.57 KB (+0.05% 🔺)
@dfinity/identity 92.01 KB (0%)
@dfinity/identity-secp256k1 230.2 KB (+0.02% 🔺)

@frederikrothenberger frederikrothenberger changed the title Allow to decode t as opt t feat: Allow to decode t as opt t Apr 25, 2023
This is a strawman PR to implement coercion of
`t` to `opt t` when decoding values.
@@ -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.

@stopak
Copy link

stopak commented May 9, 2023

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)];
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.

@krpeacock krpeacock self-assigned this May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants