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

Intelligent constructor #217

Merged
merged 30 commits into from
Oct 9, 2018
Merged

Intelligent constructor #217

merged 30 commits into from
Oct 9, 2018

Conversation

amaury1093
Copy link
Contributor

@amaury1093 amaury1093 commented Oct 5, 2018

Effort to remove all fromJSON and fromU8a from types, and initialize them solely via constructor. Their constructor should be intelligent enough to handle all kinds of inputs (incl. of course JSON and Uint8Array inputs).

For consistency, I propose that this decoding/input handling be done in the static method decode. The constructor will just call this decode method.

Related #161.

Done in this PR:

  • Struct
  • Text
  • Vector
  • Uint
  • Enum
  • Option
  • Ua8
  • EnumType
  • Extrinsic

@amaury1093 amaury1093 added WIP Work in Progress @types labels Oct 5, 2018
@jacogr
Copy link
Member

jacogr commented Oct 5, 2018

On-board with the static decode, we have been doing this de-facto anyway. (It helps to build in some hoops before passing stuff to super)

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 85.341% when pulling bb0e0e1 on am-only-constructor into d4990f8 on master.

} else if (input instanceof Uint8Array) {
const [offset, length] = Compact.decode(input);
return u8aToUtf8(input.subarray(offset, offset + length.toNumber()));
} else if (Array.isArray(input)) {
Copy link
Member

Choose a reason for hiding this comment

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

I would propose removing Array until needed. As it stands we don't have anything coming in anymore as [...]

: value[key]
);
if (value instanceof Uint8Array) {
const [compactLength, typeLength] = Compact.decode(value.subarray(currentIndex));
Copy link
Member

Choose a reason for hiding this comment

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

The length needs to be decoded by the child itself, not this one.

@@ -43,30 +46,54 @@ export default class Struct <
}, {} as E);
}

static decode <S, V, T> (Types: S, value: V | Array<any>, isTuple: boolean): T {
static decode<S, V, T> (Types: S, value: V | Array<any> | AnyU8a, isTuple: boolean): T {
Copy link
Member

Choose a reason for hiding this comment

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

Erk, the isTuple is a hold-over, I don't believe is is used anymore, Tuples do their own thing. (It was pre-changes when stuff still came in as Array<number>)

@jacogr jacogr removed WIP Work in Progress @types labels Oct 9, 2018
Copy link
Member

@jacogr jacogr left a comment

Choose a reason for hiding this comment

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

LGTM, will pull it and see if all hell breaks loose :)

@jacogr jacogr merged commit c026ecb into master Oct 9, 2018
@jacogr jacogr deleted the am-only-constructor branch October 9, 2018 13:59
@polkadot-js-bot
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Jun 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants