-
Notifications
You must be signed in to change notification settings - Fork 116
Joystream types - refactorization and upgrade #1124
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c7c531d
Joystream types upgrade and refactorization
e934a77
Update root resolutions
3ad8b4f
Temporary disable storage node postinstall build
62acd71
Minor fixes
ecea87a
Tooling for api.createType augmentation
ed11564
Merge branch 'iznik' into joystream-types-upgrade
74d34aa
Remove types/yarn.lock
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ | |
| hiring/schemas/role.schema.json | ||
| lib/ | ||
| build/ | ||
| src/definitions | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,73 @@ | ||
| import { Option, Struct, Enum } from '@polkadot/types/codec' | ||
| import { Text, bool as Bool } from '@polkadot/types' | ||
| import { Codec } from '@polkadot/types/types' | ||
| import { Struct } from '@polkadot/types/codec' | ||
| import { Codec, Constructor, Registry } from '@polkadot/types/types' | ||
|
|
||
| export class JoyStruct< | ||
| T extends { | ||
| [K: string]: Codec | ||
| } | ||
| > extends Struct { | ||
| getField<C extends Codec>(name: keyof T): C { | ||
| return super.get(name as string) as C | ||
| } | ||
|
|
||
| getString(name: keyof T): string { | ||
| return this.getField<Text>(name).toString() | ||
| } | ||
| export interface ExtendedStruct<FieldTypes extends Record<string, Constructor>> extends Struct<FieldTypes> { | ||
| getField<FieldKey extends keyof FieldTypes>(key: FieldKey): InstanceType<FieldTypes[FieldKey]> | ||
| getString<FieldKey extends keyof FieldTypes>(key: FieldKey): string | ||
| cloneValues(): { [k in keyof FieldTypes]: FieldTypes[k] } | ||
| } | ||
|
|
||
| getBoolean(name: keyof T): boolean { | ||
| return this.getField<Bool>(name).valueOf() | ||
| } | ||
| // Those getters are automatically added via Object.defineProperty when using Struct.with | ||
| export type ExtendedStructGetters<FieldTypes extends Record<string, Constructor>> = { | ||
| [k in keyof FieldTypes]: InstanceType<FieldTypes[k]> | ||
| } | ||
| // More rich TypeScript definition of the Struct (includes automatically created getters) | ||
| export type ExtendedStructDecorated<FieldTypes extends Record<string, Constructor>> = ExtendedStructGetters< | ||
| FieldTypes | ||
| > & | ||
| ExtendedStruct<FieldTypes> | ||
|
|
||
| getEnumAsString<EnumValue extends string>(name: keyof T): EnumValue { | ||
| return this.getField<Enum>(name).toString() as EnumValue | ||
| } | ||
| export interface StructConstructor< | ||
| FieldTypes extends Record<string, Constructor>, | ||
| StructType extends Struct<FieldTypes> | ||
| > { | ||
| new (registry: Registry, value?: { [k in keyof FieldTypes]: InstanceType<FieldTypes[k]> }): StructType | ||
| } | ||
|
|
||
| unwrapOrUndefined<C extends Codec>(name: keyof T): C | undefined { | ||
| return this.getField<Option<C>>(name).unwrapOr(undefined) | ||
| } | ||
| export type ExtendedStructConstructor<FieldTypes extends Record<string, Constructor>> = StructConstructor< | ||
| FieldTypes, | ||
| ExtendedStruct<FieldTypes> | ||
| > | ||
|
|
||
| getOptionalString(name: keyof T): string | undefined { | ||
| const text = this.unwrapOrUndefined<Text>(name) | ||
| return text ? text.toString() : undefined | ||
| } | ||
| export type ExtendedStructDecoratedConstructor<FieldTypes extends Record<string, Constructor>> = StructConstructor< | ||
| FieldTypes, | ||
| ExtendedStructDecorated<FieldTypes> | ||
| > | ||
|
|
||
| cloneValues(): T { | ||
| const objectClone = {} as { [K: string]: Codec } | ||
| // Helper for creating extended Struct type with TS-compatible interface | ||
| // It's called JoyStructCustom, because eventually we'd want to migrate all structs to JoyStructDecorated, | ||
| // but the latter won't allow specifying getters that return different type than the original field type. | ||
| // (ie. by using getString() instead of getField()) | ||
| export function JoyStructCustom<FieldTypes extends Record<string, Constructor>>( | ||
| fields: FieldTypes | ||
| ): ExtendedStructConstructor<FieldTypes> { | ||
| return class JoyStructObject extends Struct.with(fields) { | ||
| constructor(registry: Registry, value?: { [k in keyof FieldTypes]: InstanceType<FieldTypes[k]> }) { | ||
| super(registry, value) | ||
| } | ||
| getField<FieldKey extends keyof FieldTypes>(key: FieldKey): InstanceType<FieldTypes[FieldKey]> { | ||
| return this.get(key as string) as InstanceType<FieldTypes[FieldKey]> | ||
| } | ||
| getString<FieldKey extends keyof FieldTypes>(key: FieldKey): string { | ||
| return this.getField(key).toString() | ||
| } | ||
| // TODO: Check why would this ever be needed | ||
| cloneValues(): { [k in keyof FieldTypes]: FieldTypes[k] } { | ||
| const objectClone = {} as Partial<{ [k in keyof FieldTypes]: Codec }> | ||
|
|
||
| super.forEach((v, k) => { | ||
| objectClone[k] = v // shallow copy acceptable ? | ||
| }) | ||
| super.forEach((v, k) => { | ||
| objectClone[k] = v // shallow copy acceptable ? | ||
| }) | ||
|
|
||
| return objectClone as T | ||
| return (objectClone as unknown) as { [k in keyof FieldTypes]: FieldTypes[k] } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // JoyStruct enriched with typescript definitions for getters automatically added by polkadot-js | ||
| export function JoyStructDecorated<FieldTypes extends Record<string, Constructor>>( | ||
| fields: FieldTypes | ||
| ): ExtendedStructDecoratedConstructor<FieldTypes> { | ||
| // We need to cast here because there's no way to make TS aware of getters added with Object.defineProperty | ||
| return JoyStructCustom(fields) as ExtendedStructDecoratedConstructor<FieldTypes> | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 would suggest
getFieldAsStringas a better nameThere 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.
not worth spending more time on this however now, there is more important work.