Skip to content

Comments

refactor: refactor IDL.js into TypeScript#209

Merged
hansl merged 8 commits intomasterfrom
hansl/userlib-refactor
Nov 28, 2019
Merged

refactor: refactor IDL.js into TypeScript#209
hansl merged 8 commits intomasterfrom
hansl/userlib-refactor

Conversation

@hansl
Copy link
Contributor

@hansl hansl commented Nov 26, 2019

Including (but not limited to): adding types everywhere, adding missing
types for some dependencies, add prettier and changing some lint rules,
fixing the tests too, remove code that is unused, removing babel as we
dont need it, and some other changes that are too much to list.

The tests all pass.

Including (but not limited to): adding types everywhere, adding missing
types for some dependencies, add prettier and changing some lint rules,
fixing the tests too, remove code that is unused, removing babel as we
dont need it, and some other changes that are too much to list.

The tests all pass.
@hansl hansl requested a review from a team as a code owner November 26, 2019 04:55
throw new Error("You have the implement the method validate.");
}

public encode(x: any): Buffer {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the idea that, for each class, we should always call encode and not encodeGo? If so, we should make encodeGo private.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we only validate encode type, not decode type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed validate() to covariant(), and we might add contravariant() at some point for decode.

Thanks for clarifying "Go". I'll update the code to make it private.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, can't. Updated the comment.

}

/**
* Represents an IDL Tuple
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't really have IDL Tuple, I think all the comments should be "Represents JS Tuple in IDL"

test_(Result, { err: 'uhoh' }, '4449444c016b029cc20171e58eb402710100010475686f68', 'Result err');
expect(() => IDL.encode([Result], [{}])).toThrow(/Variant has no data/);
expect(() => IDL.encode([Result], [{ ok: 'ok', err: 'err' }])).toThrow(/Variant has extra key/);
// expect(() => IDL.decode([Result], Error('Call retailerQueryAll exception: Uncaught RuntimeError: memory access out of bounds')), 'Decode error').toThrow(/Uncaught RuntimeError/)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we disable this? If the bytes we are getting is an Error type, we will get a type error in the runtime?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a weird test... I updated it.

Copy link
Contributor

Choose a reason for hiding this comment

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

yep, but it's a real message we get from client while working on produceExchange...

@chenyan-dfinity
Copy link
Contributor

High-level question: Is the code for IDL.js visible to end users? If so, we should make the IDL spec public along with the release. One thing I want to prevent is that people starting to treat the JS implementation as the spec and port this particular library to other languages...

public encode(x: any): Buffer {
if (this.validate(x)) {
/** @internal */
public encode(x: T): Buffer {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest move encode to the public encode function, and do the covariant check there.

test_(Result, { err: 'uhoh' }, '4449444c016b029cc20171e58eb402710100010475686f68', 'Result err');
expect(() => IDL.encode([Result], [{}])).toThrow(/Variant has no data/);
expect(() => IDL.encode([Result], [{ ok: 'ok', err: 'err' }])).toThrow(/Variant has extra key/);
// expect(() => IDL.decode([Result], Error('Call retailerQueryAll exception: Uncaught RuntimeError: memory access out of bounds')), 'Decode error').toThrow(/Uncaught RuntimeError/)
Copy link
Contributor

Choose a reason for hiding this comment

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

yep, but it's a real message we get from client while working on produceExchange...

Hans Larsen and others added 3 commits November 26, 2019 16:12
Copy link
Contributor

@chenyan-dfinity chenyan-dfinity left a comment

Choose a reason for hiding this comment

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

mostly LG! just some nit.

Buffer.concat([leb.encode(i), x.encodeType(typeTable)]),
),
components.map((x, i) => {
return Buffer.concat([leb.encode(i), x.encodeType(typeTable)]);
Copy link
Contributor

Choose a reason for hiding this comment

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

a bit weird to have return here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a function, so it's necessary. When I move the line length to 100 it will be refactored above to a single line and will be easier to read.

Copy link
Contributor

Choose a reason for hiding this comment

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

it doesn't have to be a function.

public buildTypeTable(typeTable: TypeTable) {
super.buildTypeTable(typeTable);
public _buildTypeTableImpl(typeTable: TypeTable) {
super._buildTypeTableImpl(typeTable);
Copy link
Contributor

Choose a reason for hiding this comment

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

this._type.buildTypeTable(typeTable);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the ContainerClass. It was meh.

@@ -1,26 +0,0 @@
import { Buffer } from "buffer/";
Copy link
Contributor

Choose a reason for hiding this comment

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

may want to resurrect this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will be generated from the .ts file. No need for type definitions if you have a .ts file.

*/
export class ArrClass<T> extends ContainerClass<T, T[]> {
public covariant(x: any): x is T[] {
return Array.isArray(x) && x.every((v) => super.covariant(v));
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to check that all values have the same type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do they have to, though? As far as I know they just need to be covariant (e.g. an array of Int can have Nats in it).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most covariant arrays allow covariant item type, even if they differ.

* @param {Array<Type>} [argTypes] - argument types
* @param {Array<Type>} [retTypes] - return types
*/
export class FuncClass extends Type<any> {
Copy link
Contributor

Choose a reason for hiding this comment

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

FuncClass should not extends Type. It's not really encode/decode-able. We also get rid of the functions for covariant, encodeValue, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I think I needed it at some point to satisfies ActorInterface.

* @returns {Buffer} serialised value
*/
export function encode(argTypes: Array<Type<any>>, args: any[]) {
if (args.length !== argTypes.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

args.length >= argTypes.length

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean <? This is covariant.

@hansl hansl merged commit 18e7ab0 into master Nov 28, 2019
@hansl hansl deleted the hansl/userlib-refactor branch November 28, 2019 03:19
dfinity-bot added a commit that referenced this pull request Jul 8, 2020
## Changelog for common:
Branch: master
Commits: [dfinity-lab/common@05c44edd...0030194f](https://github.com/dfinity-lab/common/compare/05c44eddb864464c06c27295460eabcf923bc2e9...0030194fc2454c8a4e42bff87aa82f7f8ed96433)

* [`2a5d4183`](https://github.com/dfinity-lab/common/commit/2a5d4183065f7811edce500e9d20fd5a4fcb10df) niv nixpkgs: update e79d1e83 -> 5a462920
* [`413ce2b3`](https://github.com/dfinity-lab/common/commit/413ce2b3b176863ff33c4fdf276842d7f94bbb5b) rust: 1.41.1 -> 1.43
* [`899366be`](https://github.com/dfinity-lab/common/commit/899366be97d4c79fdcf4230164a7ffca9ed4e309) niv nixpkgs: update 5a462920 -> 05a32d8e
* [`f2c71849`](https://github.com/dfinity-lab/common/commit/f2c7184973a5bd3d4dacfb0c1245d07ef8e39139) Add support for incremental Rust workspaces ([dfinity-lab/common⁠#199](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/199))
* [`f484c11e`](https://github.com/dfinity-lab/common/commit/f484c11e040c2a91204cfddc8ddaca1c5e51979d) Revert "Add support for incremental Rust workspaces ([dfinity-lab/common⁠#199](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/199))" ([dfinity-lab/common⁠#204](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/204))
* [`74c64048`](https://github.com/dfinity-lab/common/commit/74c640486d2e34d3cc63df1a20e4346e07a21bbf) gitSource.nix: Use gitMinimal
* [`fa568e26`](https://github.com/dfinity-lab/common/commit/fa568e267a189ee51721af6d7d08eb2af8d54de4) disable parallel rustc build ([dfinity-lab/common⁠#206](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/206))
* [`90627152`](https://github.com/dfinity-lab/common/commit/9062715209261439648898b03f7652cd3f138399) [INF-1316] Support for overriding cargoBuild ([dfinity-lab/common⁠#207](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/207))
* [`e2d48d84`](https://github.com/dfinity-lab/common/commit/e2d48d8404ccce8252d9a78d76282fc9f1bd026b) nixpkgs: Backports patches for static builds of Haskell ([dfinity-lab/common⁠#208](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/208))
* [`45bbce1c`](https://github.com/dfinity-lab/common/commit/45bbce1cfb34c6b723bbe3f89466a63ae66bfb77) rust: Add release build with symbols ([dfinity-lab/common⁠#209](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/209))
* [`c9a4327d`](https://github.com/dfinity-lab/common/commit/c9a4327df3040921fa27a7b48c6dc924b13a423b) ci: introduce the all-systems-go aggregate job
* [`e8689e50`](https://github.com/dfinity-lab/common/commit/e8689e50dfff33663f498df2baf994eeee39bb7d) Disable selectors2 tests on Darwin
* [`eb8225ea`](https://github.com/dfinity-lab/common/commit/eb8225ea65dfa4bcc688a3d58f7c9633affd9234) nix/overlays/ci.nix: increase schedulingPriority of all-systems-go
* [`dac9442c`](https://github.com/dfinity-lab/common/commit/dac9442c1110253f3386208790757ec11ff4605c) [INF-589] Incremental build support ([dfinity-lab/common⁠#213](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/213))
* [`ebaf4cf7`](https://github.com/dfinity-lab/common/commit/ebaf4cf726e75f2037570f162f79b103a3931bbd) make rustWorkspace overridable
* [`a1fb28aa`](https://github.com/dfinity-lab/common/commit/a1fb28aa2d8ddb6ac730264dc4273c4643b818f7) allow crates-io references for existing naersk builds
* [`724330e8`](https://github.com/dfinity-lab/common/commit/724330e8e6d8db0fdc6d8b835ed7b107333fed44) Update niv-updater-action to v6
dfinity-bot added a commit that referenced this pull request Jul 15, 2020
## Changelog for common:
Branch: master
Commits: [dfinity-lab/common@05c44edd...9162ebde](https://github.com/dfinity-lab/common/compare/05c44eddb864464c06c27295460eabcf923bc2e9...9162ebdee5e914c0a7d22c71596d1760230156df)

* [`2a5d4183`](https://github.com/dfinity-lab/common/commit/2a5d4183065f7811edce500e9d20fd5a4fcb10df) niv nixpkgs: update e79d1e83 -> 5a462920
* [`413ce2b3`](https://github.com/dfinity-lab/common/commit/413ce2b3b176863ff33c4fdf276842d7f94bbb5b) rust: 1.41.1 -> 1.43
* [`899366be`](https://github.com/dfinity-lab/common/commit/899366be97d4c79fdcf4230164a7ffca9ed4e309) niv nixpkgs: update 5a462920 -> 05a32d8e
* [`f2c71849`](https://github.com/dfinity-lab/common/commit/f2c7184973a5bd3d4dacfb0c1245d07ef8e39139) Add support for incremental Rust workspaces ([dfinity-lab/common⁠#199](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/199))
* [`f484c11e`](https://github.com/dfinity-lab/common/commit/f484c11e040c2a91204cfddc8ddaca1c5e51979d) Revert "Add support for incremental Rust workspaces ([dfinity-lab/common⁠#199](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/199))" ([dfinity-lab/common⁠#204](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/204))
* [`74c64048`](https://github.com/dfinity-lab/common/commit/74c640486d2e34d3cc63df1a20e4346e07a21bbf) gitSource.nix: Use gitMinimal
* [`fa568e26`](https://github.com/dfinity-lab/common/commit/fa568e267a189ee51721af6d7d08eb2af8d54de4) disable parallel rustc build ([dfinity-lab/common⁠#206](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/206))
* [`90627152`](https://github.com/dfinity-lab/common/commit/9062715209261439648898b03f7652cd3f138399) [INF-1316] Support for overriding cargoBuild ([dfinity-lab/common⁠#207](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/207))
* [`e2d48d84`](https://github.com/dfinity-lab/common/commit/e2d48d8404ccce8252d9a78d76282fc9f1bd026b) nixpkgs: Backports patches for static builds of Haskell ([dfinity-lab/common⁠#208](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/208))
* [`45bbce1c`](https://github.com/dfinity-lab/common/commit/45bbce1cfb34c6b723bbe3f89466a63ae66bfb77) rust: Add release build with symbols ([dfinity-lab/common⁠#209](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/209))
* [`c9a4327d`](https://github.com/dfinity-lab/common/commit/c9a4327df3040921fa27a7b48c6dc924b13a423b) ci: introduce the all-systems-go aggregate job
* [`e8689e50`](https://github.com/dfinity-lab/common/commit/e8689e50dfff33663f498df2baf994eeee39bb7d) Disable selectors2 tests on Darwin
* [`eb8225ea`](https://github.com/dfinity-lab/common/commit/eb8225ea65dfa4bcc688a3d58f7c9633affd9234) nix/overlays/ci.nix: increase schedulingPriority of all-systems-go
* [`dac9442c`](https://github.com/dfinity-lab/common/commit/dac9442c1110253f3386208790757ec11ff4605c) [INF-589] Incremental build support ([dfinity-lab/common⁠#213](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/213))
* [`ebaf4cf7`](https://github.com/dfinity-lab/common/commit/ebaf4cf726e75f2037570f162f79b103a3931bbd) make rustWorkspace overridable
* [`a1fb28aa`](https://github.com/dfinity-lab/common/commit/a1fb28aa2d8ddb6ac730264dc4273c4643b818f7) allow crates-io references for existing naersk builds
* [`724330e8`](https://github.com/dfinity-lab/common/commit/724330e8e6d8db0fdc6d8b835ed7b107333fed44) Update niv-updater-action to v6
* [`b48fa5d6`](https://github.com/dfinity-lab/common/commit/b48fa5d64ae8aecd4b900f4e04054c942f839e38) Fix typo in README
* [`a63e292f`](https://github.com/dfinity-lab/common/commit/a63e292f45cf2d5238b04fb2a2bc173ea44ba21b) Add a replaceStdenv function ([dfinity-lab/common⁠#202](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/202))
* [`7d3742a2`](https://github.com/dfinity-lab/common/commit/7d3742a2efb70206dac5dcfba78f7102cc2e0b2a) niv cargo2nix: update b9ef68fe -> 31d34998
* [`8431c1e6`](https://github.com/dfinity-lab/common/commit/8431c1e66239f7bc14a0f113c95692648bb211d2) patch ruamel
* [`9162ebde`](https://github.com/dfinity-lab/common/commit/9162ebdee5e914c0a7d22c71596d1760230156df) Migrate back to OpenSSL ([dfinity-lab/common⁠#219](http://r.duckduckgo.com/l/?uddg=https://github.com/dfinity-lab/common/issues/219))
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.

2 participants