Add Codec toRawType (#921)

* Add Codec toType

* Revert i* & u* (toType, catered for in base)

* toType -> toRawType
This commit is contained in:
Jaco Greeff
2019-06-12 16:45:45 +12:00
committed by Ian He
parent 3b92be5284
commit b5d5084b55
34 changed files with 265 additions and 22 deletions
+6 -1
View File
@@ -70,6 +70,11 @@ export default abstract class AbstractArray<T extends Codec> extends Array<T> im
);
}
/**
* @description Returns the base runtime type name for this instance
*/
abstract toRawType (): string;
/**
* @description Returns the string representation of the value
*/
@@ -83,7 +88,7 @@ export default abstract class AbstractArray<T extends Codec> extends Array<T> im
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+6 -1
View File
@@ -114,6 +114,11 @@ export default abstract class AbstractInt extends BN implements Codec {
: this.toNumber();
}
/**
* @description Returns the base runtime type name for this instance
*/
abstract toRawType (): string;
/**
* @description Returns the string representation of the value
* @param base The base to use for the conversion
@@ -124,7 +129,7 @@ export default abstract class AbstractInt extends BN implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
abstract toU8a (isBare?: boolean): Uint8Array;
+8 -1
View File
@@ -126,6 +126,13 @@ export default class Compact extends Base<UInt | Moment> implements Codec {
return this.raw.toNumber();
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return `Compact<${this.raw.toRawType()}>`;
}
/**
* @description Returns the string representation of the value
*/
@@ -134,7 +141,7 @@ export default class Compact extends Base<UInt | Moment> implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+19 -1
View File
@@ -218,6 +218,24 @@ export default class Enum extends Base<Codec> implements Codec {
return this._index;
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
const kv = this._isBasic
? Object.keys(this._def).map((key) =>
`"${key}"`
)
: Object.entries(this._def).map(([key, Type]) =>
`"${key}":"${new Type().toRawType()}"`
);
const braces = this._isBasic
? '[]'
: '{}';
return `{"_enum":${braces[0]}${kv.join(',')}${braces[1]}}`;
}
/**
* @description Returns the string representation of the value
*/
@@ -228,7 +246,7 @@ export default class Enum extends Base<Codec> implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+8 -1
View File
@@ -42,7 +42,14 @@ export default class Int extends AbstractInt {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return `i${this._bitLength}`;
}
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+8 -1
View File
@@ -111,6 +111,13 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
return this.raw.toJSON();
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return `Option<${this.raw.toRawType()}>`;
}
/**
* @description Returns the string representation of the value
*/
@@ -119,7 +126,7 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+13 -1
View File
@@ -138,6 +138,18 @@ export default class CodecSet extends Set<string> implements Codec {
return this.strings;
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
const kv = Object.entries(this._setValues).map(([key, value]) =>
`"${key}":${value}` // JSON format
);
// FIXME We don't cater for this in createType as of yet
return `{"_set":{${kv.join(',')}}}`;
}
/**
* @description Returns the string representation of the value
*/
@@ -146,7 +158,7 @@ export default class CodecSet extends Set<string> implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+12 -1
View File
@@ -232,6 +232,17 @@ export default class Struct<
}, {} as any);
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
const kv = [...this.entries()].map(([key, value]) =>
`"${key}":"${value.toRawType()}"` // double-quotes, JSON
);
return `{${kv.join(',')}}`;
}
/**
* @description Returns the string representation of the value
*/
@@ -240,7 +251,7 @@ export default class Struct<
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+14 -1
View File
@@ -77,6 +77,19 @@ export default class Tuple extends AbstractArray<Codec> {
: Object.keys(this._Types);
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
const types = (
Array.isArray(this._Types)
? this._Types.map((Type) => new Type().toRawType())
: Object.keys(this._Types)
);
return `(${types.join(',')})`;
}
/**
* @description Returns the string representation of the value
*/
@@ -86,7 +99,7 @@ export default class Tuple extends AbstractArray<Codec> {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+8 -1
View File
@@ -88,6 +88,13 @@ export default class U8a extends Uint8Array implements Codec {
return this.toHex();
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return `Vec<u8>`;
}
/**
* @description Returns the string representation of the value
*/
@@ -96,7 +103,7 @@ export default class U8a extends Uint8Array implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+8
View File
@@ -52,4 +52,12 @@ export default class U8aFixed extends U8a {
bitLength () {
return this.length * 8;
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
// FIEXME We don't cater for this in createType
return `[u8;${this.length}]`;
}
}
+8 -1
View File
@@ -42,7 +42,14 @@ export default class UInt extends AbstractInt {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return `u${this._bitLength}`;
}
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+7
View File
@@ -84,4 +84,11 @@ export default class Vector<T extends Codec> extends AbstractArray<T> {
return -1;
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return `Vec<${new this._Type().toRawType()}>`;
}
}
+8 -1
View File
@@ -9,8 +9,15 @@ import AbstractArray from './AbstractArray';
* @name VectorAny
* @description
* This manages codec arrays, assuming that the inputs are already of type Codec. Unlike
* a vector, this cane be used to manage array-like structures with variable arguments of
* a vector, this can be used to manage array-like structures with variable arguments of
* any types
*/
export default class VectorAny<T extends Codec> extends AbstractArray<T> {
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
// FIXME This is basically an any type, cannot instantiate via createType
return 'Vec<Codec>';
}
}
@@ -60,6 +60,13 @@ export default class AccountId extends U8aFixed {
toString (): string {
return AccountId.encode(this);
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'AccountId';
}
}
/**
@@ -110,4 +110,11 @@ export default class AccountIndex extends U32 {
return encodeAddress(this.toU8a().subarray(0, length));
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'AccountIndex';
}
}
+8 -1
View File
@@ -121,7 +121,14 @@ export default class Address extends Base<AccountId | AccountIndex> implements C
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Address';
}
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+8 -1
View File
@@ -68,6 +68,13 @@ export default class Bool extends Boolean implements Codec {
return this.valueOf();
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'bool';
}
/**
* @description Returns the string representation of the value
*/
@@ -76,7 +83,7 @@ export default class Bool extends Boolean implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+8 -1
View File
@@ -70,7 +70,14 @@ export default class Bytes extends U8a {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Bytes';
}
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+6
View File
@@ -10,4 +10,10 @@ import U8a from '../codec/U8a';
* A raw data structure. It is just an encoding of a U8a, without any length encoding
*/
export default class Data extends U8a {
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Data';
}
}
+7
View File
@@ -16,4 +16,11 @@ export default class H160 extends U8aFixed {
constructor (value?: AnyU8a) {
super(value, 160);
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'H160';
}
}
+7
View File
@@ -16,4 +16,11 @@ export default class H256 extends U8aFixed {
constructor (value?: AnyU8a) {
super(value, 256);
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'H256';
}
}
+7
View File
@@ -15,4 +15,11 @@ export default class H512 extends U8aFixed {
constructor (value?: AnyU8a) {
super(value, 512);
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'H512';
}
}
+7
View File
@@ -226,4 +226,11 @@ export default class Method extends Struct implements IMethod {
get meta (): FunctionMetadataV4 {
return this._meta;
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Call';
}
}
+8 -1
View File
@@ -101,6 +101,13 @@ export default class Moment extends Date implements Codec {
return Math.ceil(this.getTime() / 1000);
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Moment';
}
/**
* @description Returns the string representation of the value
*/
@@ -110,7 +117,7 @@ export default class Moment extends Date implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+8 -1
View File
@@ -46,6 +46,13 @@ export default class Null implements Codec {
return null;
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Null';
}
/**
* @description Returns the string representation of the value
*/
@@ -54,7 +61,7 @@ export default class Null implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+7
View File
@@ -16,4 +16,11 @@ export default class Origin extends Null {
throw new Error('Origin should not be constructed, it is only a placeholder for compatibility');
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Origin'; // yes, we cannot instantiate, but for consistency
}
}
+8 -1
View File
@@ -91,6 +91,13 @@ export default class Text extends String implements Codec {
return this.toString();
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Text';
}
/**
* @description Returns the string representation of the value
*/
@@ -100,7 +107,7 @@ export default class Text extends String implements Codec {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+8 -1
View File
@@ -83,7 +83,7 @@ export default class Type extends Text {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
@@ -233,4 +233,11 @@ export default class Type extends Text {
return value;
};
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Type';
}
}
+6
View File
@@ -11,4 +11,10 @@ import U32 from './U32';
* data. It is a wrapper for [[U32]] as a WASM default (as generated by Rust bindings)
*/
export default class USize extends U32 {
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'usize';
}
}
+7
View File
@@ -61,6 +61,13 @@ export default class Json extends Map<string, any> implements Codec {
}, {} as any);
}
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string {
return 'Json';
}
/**
* @description Returns the string representation of the value
*/
+1 -1
View File
@@ -198,7 +198,7 @@ export default class Extrinsic extends Struct implements IExtrinsic {
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
@@ -154,7 +154,7 @@ export default class ExtrinsicSignature extends Struct implements IExtrinsicSign
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
+6 -1
View File
@@ -60,13 +60,18 @@ export interface Codec {
*/
toJSON (): any;
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string;
/**
* @description Returns the string representation of the value
*/
toString (): string;
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array;