Don't map empty tuples to Null (#1725)
* Don't map empty tuples to Null * Remove unused var * Regen with tuple Result * Generated types lint fixes
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { Option, Raw, Vec } from '@polkadot/types/codec';
|
||||
import { Bytes, Null, bool, u32, u64 } from '@polkadot/types';
|
||||
import { Bytes, bool, u32, u64 } from '@polkadot/types';
|
||||
import { AccountId, AccountIndex, Balance, BalanceOf, BlockNumber, Hash, Index, KeyTypeId, Moment, Perbill, ValidatorId, Weight } from '@polkadot/types/interfaces/runtime';
|
||||
import { UncleEntryItem } from '@polkadot/types/interfaces/authorship';
|
||||
import { BabeAuthorityWeight, MaybeVrf } from '@polkadot/types/interfaces/babe';
|
||||
@@ -39,7 +39,7 @@ declare module './types' {
|
||||
digest: StorageEntryExact<ApiType, () => Observable<DigestOf>> & QueryableStorageEntry<ApiType>;
|
||||
events: StorageEntryExact<ApiType, () => Observable<Vec<EventRecord>>> & QueryableStorageEntry<ApiType>;
|
||||
eventCount: StorageEntryExact<ApiType, () => Observable<EventIndex>> & QueryableStorageEntry<ApiType>;
|
||||
eventTopics: StorageEntryExact<ApiType, (key1: Null, key2: Hash | Uint8Array | string) => Observable<Vec<ITuple<[BlockNumber, EventIndex]>>>> & QueryableStorageEntry<ApiType>;
|
||||
eventTopics: StorageEntryExact<ApiType, (key1: ITuple<[]>, key2: Hash | Uint8Array | string) => Observable<Vec<ITuple<[BlockNumber, EventIndex]>>>> & QueryableStorageEntry<ApiType>;
|
||||
};
|
||||
babe: {
|
||||
[index: string]: QueryableStorageEntry<ApiType>;
|
||||
|
||||
@@ -1495,7 +1495,7 @@
|
||||
"name": "ForcingNewEra",
|
||||
"modifier": "optional",
|
||||
"type": {
|
||||
"Plain": "Null"
|
||||
"Plain": "()"
|
||||
},
|
||||
"fallback": "0x00",
|
||||
"documentation": [
|
||||
|
||||
@@ -977,7 +977,7 @@
|
||||
"name": "ForcingNewEra",
|
||||
"modifier": "optional",
|
||||
"type": {
|
||||
"Plain": "Null"
|
||||
"Plain": "()"
|
||||
},
|
||||
"fallback": "0x00",
|
||||
"documentation": [
|
||||
|
||||
@@ -161,7 +161,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
|
||||
@@ -161,7 +161,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
@@ -330,7 +330,7 @@
|
||||
{
|
||||
"name": "BatchExecuted",
|
||||
"args": [
|
||||
"Vec<Result<Null,DispatchError>>"
|
||||
"Vec<Result<(),DispatchError>>"
|
||||
],
|
||||
"documentation": []
|
||||
}
|
||||
|
||||
@@ -1004,7 +1004,7 @@
|
||||
"name": "ForcingNewEra",
|
||||
"modifier": "optional",
|
||||
"type": {
|
||||
"Plain": "Null"
|
||||
"Plain": "()"
|
||||
},
|
||||
"fallback": "0x00",
|
||||
"documentation": [
|
||||
|
||||
@@ -1063,7 +1063,7 @@
|
||||
"name": "ForcingNewEra",
|
||||
"modifier": "optional",
|
||||
"type": {
|
||||
"Plain": "Null"
|
||||
"Plain": "()"
|
||||
},
|
||||
"fallback": "0x00",
|
||||
"documentation": [
|
||||
|
||||
@@ -161,7 +161,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2_256"
|
||||
@@ -1121,7 +1121,7 @@
|
||||
"name": "ForcingNewEra",
|
||||
"modifier": "optional",
|
||||
"type": {
|
||||
"Plain": "Null"
|
||||
"Plain": "()"
|
||||
},
|
||||
"fallback": "0x00",
|
||||
"documentation": [
|
||||
|
||||
@@ -161,7 +161,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
|
||||
@@ -161,7 +161,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
|
||||
@@ -161,7 +161,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
|
||||
@@ -184,7 +184,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
|
||||
@@ -185,7 +185,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
|
||||
@@ -185,7 +185,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
@@ -332,7 +332,7 @@
|
||||
{
|
||||
"name": "BatchExecuted",
|
||||
"args": [
|
||||
"Vec<Result<Null,DispatchError>>"
|
||||
"Vec<Result<(),DispatchError>>"
|
||||
],
|
||||
"documentation": []
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
@@ -330,7 +330,7 @@
|
||||
{
|
||||
"name": "BatchExecuted",
|
||||
"args": [
|
||||
"Vec<Result<Null,DispatchError>>"
|
||||
"Vec<Result<(),DispatchError>>"
|
||||
],
|
||||
"documentation": []
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@
|
||||
"type": {
|
||||
"DoubleMap": {
|
||||
"hasher": "blake2256",
|
||||
"key1": "Null",
|
||||
"key1": "()",
|
||||
"key2": "Hash",
|
||||
"value": "Vec<(BlockNumber,EventIndex)>",
|
||||
"key2Hasher": "blake2256"
|
||||
@@ -325,7 +325,7 @@
|
||||
{
|
||||
"name": "BatchExecuted",
|
||||
"args": [
|
||||
"Vec<Result<Null,DispatchError>>"
|
||||
"Vec<Result<(),DispatchError>>"
|
||||
],
|
||||
"documentation": []
|
||||
}
|
||||
|
||||
@@ -45,6 +45,6 @@ describe('Result', (): void => {
|
||||
});
|
||||
|
||||
it('returns a proper raw typedef rom a built-in', (): void => {
|
||||
expect(createType(registry, 'DispatchResult').toRawType()).toEqual('Result<Null,{"_enum":{"Other":"Null","CannotLookup":"Null","BadOrigin":"Null","Module":"{\\"index\\":\\"u8\\",\\"error\\":\\"u8\\"}"}}>');
|
||||
expect(createType(registry, 'DispatchResult').toRawType()).toEqual('Result<(),{"_enum":{"Other":"Null","CannotLookup":"Null","BadOrigin":"Null","Module":"{\\"index\\":\\"u8\\",\\"error\\":\\"u8\\"}"}}>');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,12 +19,13 @@ describe('getTypeDef', (): void => {
|
||||
).toThrow(/Unable to find closing matching/);
|
||||
});
|
||||
|
||||
it('maps empty tuples to Null', (): void => {
|
||||
it('maps empty tuples to empty tuple', (): void => {
|
||||
expect(
|
||||
getTypeDef('()')
|
||||
).toEqual({
|
||||
info: TypeDefInfo.Plain,
|
||||
type: 'Null'
|
||||
info: TypeDefInfo.Tuple,
|
||||
sub: [],
|
||||
type: '()'
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -80,10 +80,12 @@ function _decodeFixedVec (value: TypeDef, type: string, _: string): TypeDef {
|
||||
|
||||
// decode a tuple
|
||||
function _decodeTuple (value: TypeDef, _: string, subType: string): TypeDef {
|
||||
value.sub = typeSplit(subType).map((inner): TypeDef =>
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
getTypeDef(inner)
|
||||
);
|
||||
value.sub = subType.length === 0
|
||||
? []
|
||||
: typeSplit(subType).map((inner): TypeDef =>
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
getTypeDef(inner)
|
||||
);
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -128,7 +130,6 @@ export function getTypeDef (_type: string, { name, displayName }: TypeDefOptions
|
||||
// create the type via Type, allowing types to be sanitized
|
||||
const type = sanitize(_type);
|
||||
const value: TypeDef = { info: TypeDefInfo.Plain, displayName, name, type };
|
||||
|
||||
const nested = nestedExtraction.find((nested): boolean =>
|
||||
hasWrapper(type, nested)
|
||||
);
|
||||
|
||||
@@ -24,8 +24,6 @@ const mappings: Mapper[] = [
|
||||
_removeGenerics(),
|
||||
// alias String -> Text (compat with jsonrpc methods)
|
||||
_alias('String', 'Text'),
|
||||
// alias () -> Null
|
||||
_alias('\\(\\)', 'Null'),
|
||||
// alias Vec<u8> -> Bytes
|
||||
_alias('Vec<u8>', 'Bytes'),
|
||||
// alias &[u8] -> Bytes
|
||||
@@ -100,7 +98,7 @@ function _cleanupCompact (): Mapper {
|
||||
|
||||
function _flattenSingleTuple (): Mapper {
|
||||
return (value: string): string => {
|
||||
return value.replace(/\(([^,]*)\)/, '$1');
|
||||
return value.replace(/\(([^,]+)\)/, '$1');
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ export default {
|
||||
|
||||
// Metadata v4
|
||||
// :: consensus
|
||||
InherentOfflineReport: 'Null', // this is '()' originally (we don't handle that)
|
||||
InherentOfflineReport: '()',
|
||||
SessionKey: 'AccountId',
|
||||
|
||||
// Metadata v5
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
// Auto-generated via `yarn build:interfaces`, do not edit
|
||||
/* eslint-disable @typescript-eslint/no-empty-interface */
|
||||
|
||||
import { ITuple } from '@polkadot/types/types';
|
||||
import { Enum, Struct } from '@polkadot/types/codec';
|
||||
import { Bytes, Null, i8, u32, u64 } from '@polkadot/types/primitive';
|
||||
import { Bytes, i8, u32, u64 } from '@polkadot/types/primitive';
|
||||
import { AccountId, Balance } from '@polkadot/types/interfaces/runtime';
|
||||
|
||||
/** Struct */
|
||||
@@ -19,8 +20,8 @@ export interface Amount extends Balance {}
|
||||
/** u32 */
|
||||
export interface AssetOf extends u32 {}
|
||||
|
||||
/** Null */
|
||||
export interface InherentOfflineReport extends Null {}
|
||||
/** ITuple<[]> */
|
||||
export interface InherentOfflineReport extends ITuple<[]> {}
|
||||
|
||||
/** i8 */
|
||||
export interface LockPeriods extends i8 {}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// Auto-generated via `yarn build:interfaces`, do not edit
|
||||
/* eslint-disable @typescript-eslint/no-empty-interface */
|
||||
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
import { Codec, ITuple } from '@polkadot/types/types';
|
||||
import { Enum, Option, Result, Struct, Vec } from '@polkadot/types/codec';
|
||||
import { Bytes, GenericEvent, Null, Text, u32, u8 } from '@polkadot/types/primitive';
|
||||
import { Bytes, GenericEvent, Text, u32, u8 } from '@polkadot/types/primitive';
|
||||
import { Digest, Hash } from '@polkadot/types/interfaces/runtime';
|
||||
|
||||
/** Digest */
|
||||
@@ -39,27 +39,31 @@ export interface DispatchErrorTo198 extends Struct {
|
||||
readonly error: u8;
|
||||
}
|
||||
|
||||
/** Result<Null, DispatchError> */
|
||||
export interface DispatchResult extends Result<Null, DispatchError> {
|
||||
/** Result<ITuple<[]>, DispatchError> */
|
||||
export interface DispatchResult extends Result<ITuple<[]>, DispatchError> {
|
||||
/** Error:: (DispatchError) */
|
||||
readonly isError: boolean;
|
||||
/** DispatchError */
|
||||
readonly asError: DispatchError;
|
||||
/** Ok:: */
|
||||
/** Ok:: (()) */
|
||||
readonly isOk: boolean;
|
||||
/** ITuple<[]> */
|
||||
readonly asOk: ITuple<[]>;
|
||||
}
|
||||
|
||||
/** DispatchResult */
|
||||
export interface DispatchResultOf extends DispatchResult {}
|
||||
|
||||
/** Result<Null, Text> */
|
||||
export interface DispatchResultTo198 extends Result<Null, Text> {
|
||||
/** Result<ITuple<[]>, Text> */
|
||||
export interface DispatchResultTo198 extends Result<ITuple<[]>, Text> {
|
||||
/** Error:: (Text) */
|
||||
readonly isError: boolean;
|
||||
/** Text */
|
||||
readonly asError: Text;
|
||||
/** Ok:: */
|
||||
/** Ok:: (()) */
|
||||
readonly isOk: boolean;
|
||||
/** ITuple<[]> */
|
||||
readonly asOk: ITuple<[]>;
|
||||
}
|
||||
|
||||
/** GenericEvent */
|
||||
|
||||
@@ -65,10 +65,10 @@ describe('Type', (): void => {
|
||||
).toEqual('(Bytes,(Balance,Balance),Vec<AccountId>)');
|
||||
});
|
||||
|
||||
it('changes () -> Null', (): void => {
|
||||
it('changes () -> ()', (): void => {
|
||||
expect(
|
||||
new Type(registry, '()').toString()
|
||||
).toEqual('Null');
|
||||
).toEqual('()');
|
||||
});
|
||||
|
||||
it('has a length for the type', (): void => {
|
||||
|
||||
@@ -19,6 +19,7 @@ interface Imports extends TypeImports {
|
||||
// helper to generate a `readonly <Name>: <Type>;` getter
|
||||
export function createGetter (definitions: object, name = '', type: string, imports: TypeImports, doc?: string): string {
|
||||
setImports(definitions, imports, [type]);
|
||||
|
||||
return ` /** ${doc || type} */\n readonly ${name}: ${type};\n`;
|
||||
}
|
||||
|
||||
@@ -61,14 +62,16 @@ function tsEnum (definitions: object, { name: enumName, sub }: TypeDef, imports:
|
||||
return exportInterface(enumName, 'Enum', keys.join(''));
|
||||
}
|
||||
|
||||
function tsResultGetter (definitions: object, resultName = '', getter: 'Ok' | 'Error', { info, name = '', type }: TypeDef, imports: TypeImports): string {
|
||||
function tsResultGetter (definitions: object, resultName = '', getter: 'Ok' | 'Error', def: TypeDef, imports: TypeImports): string {
|
||||
const { info, name = '', type } = def;
|
||||
const [resultType, asGetter] = type === 'Null'
|
||||
? ['', '']
|
||||
: [`(${type})`, createGetter(definitions, `as${getter}`, type, imports)];
|
||||
: [`(${type})`, createGetter(definitions, `as${getter}`, info === TypeDefInfo.Tuple ? formatType(definitions, def, imports) : type, imports)];
|
||||
const isGetter = createGetter(definitions, `is${getter}`, 'boolean', imports, `${getter}:: ${name}${resultType}`);
|
||||
|
||||
switch (info) {
|
||||
case TypeDefInfo.Plain:
|
||||
case TypeDefInfo.Tuple:
|
||||
case TypeDefInfo.Vec:
|
||||
return `${isGetter}${asGetter}`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user