More private field -> #field adjustments (#1945)

* More private field -> #field adjustments

* Work-around for set.add failures

* Remove FIXME, it is done
This commit is contained in:
Jaco Greeff
2020-02-25 16:53:04 +01:00
committed by GitHub
parent 6943c9b36d
commit 6be6b027cb
5 changed files with 125 additions and 122 deletions
+10 -10
View File
@@ -66,7 +66,10 @@ function decodeBTreeMapFromMap<K extends Codec = Codec, V extends Codec = Codec>
* @param jsonMap
* @internal
*/
function decodeBTreeMap<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, value: Uint8Array | string | Map<any, any>): Map<K, V> {
function decodeBTreeMap<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, keyType: Constructor<K> | keyof InterfaceTypes, valType: Constructor<V> | keyof InterfaceTypes, value: Uint8Array | string | Map<any, any>): Map<K, V> {
const KeyClass = typeToConstructor(registry, keyType);
const ValClass = typeToConstructor(registry, valType);
if (!value) {
return new Map<K, V>();
} else if (isHex(value)) {
@@ -83,19 +86,16 @@ function decodeBTreeMap<K extends Codec = Codec, V extends Codec = Codec> (regis
export default class BTreeMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements Codec {
public readonly registry: Registry;
protected _KeyClass: Constructor<K>;
readonly #KeyClass: Constructor<K>;
protected _ValClass: Constructor<V>;
readonly #ValClass: Constructor<V>;
constructor (registry: Registry, keyType: Constructor<K> | keyof InterfaceTypes, valType: Constructor<V> | keyof InterfaceTypes, rawValue: any) {
const KeyClass = typeToConstructor(registry, keyType);
const ValClass = typeToConstructor(registry, valType);
super(decodeBTreeMap(registry, KeyClass, ValClass, rawValue));
super(decodeBTreeMap(registry, keyType, valType, rawValue));
this.registry = registry;
this._KeyClass = KeyClass;
this._ValClass = ValClass;
this.#KeyClass = typeToConstructor(registry, keyType);
this.#ValClass = typeToConstructor(registry, valType);
}
public static with<K extends Codec, V extends Codec> (keyType: Constructor<K> | keyof InterfaceTypes, valType: Constructor<V> | keyof InterfaceTypes): Constructor<BTreeMap<K, V>> {
@@ -177,7 +177,7 @@ export default class BTreeMap<K extends Codec = Codec, V extends Codec = Codec>
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `BTreeMap<${new this._KeyClass(this.registry).toRawType()},${new this._ValClass(this.registry).toRawType()}>`;
return `BTreeMap<${new this.#KeyClass(this.registry).toRawType()},${new this.#ValClass(this.registry).toRawType()}>`;
}
/**
+74 -72
View File
@@ -12,84 +12,86 @@ import Compact from './Compact';
import Raw from './Raw';
import { compareSet, decodeU8a, typeToConstructor } from './utils';
/** @internal */
function decodeBTreeSetFromU8a<V extends Codec = Codec> (registry: Registry, ValClass: Constructor<V>, u8a: Uint8Array): Set<V> {
const output = new Set<V>();
const [offset, length] = Compact.decodeU8a(u8a);
const types = [];
for (let i = 0; i < length.toNumber(); i++) {
types.push(ValClass);
}
const values = decodeU8a(registry, u8a.subarray(offset), types);
for (let i = 0; i < values.length; i++) {
output.add(values[i] as V);
}
return output;
}
/** @internal */
function decodeBTreeSetFromSet<V extends Codec = Codec> (registry: Registry, ValClass: Constructor<V>, value: Set<any>): Set<V> {
const output = new Set<V>();
value.forEach((v: any) => {
let val;
try {
val = (v instanceof ValClass) ? v : new ValClass(registry, v);
} catch (error) {
console.error('Failed to decode BTreeSet key or value:', error.message);
throw error;
}
output.add(val);
});
return output;
}
/**
* Decode input to pass into constructor.
*
* @param ValClass - Type of the map value
* @param value - Value to decode, one of:
* - null
* - undefined
* - hex
* - Uint8Array
* - Set<any>, where both key and value types are either
* constructors or decodeable values for their types.
* @param jsonSet
* @internal
*/
function decodeBTreeSet<V extends Codec = Codec> (registry: Registry, valType: Constructor<V> | keyof InterfaceTypes, value: Uint8Array | string | Set<any>): Set<V> {
if (!value) {
return new Set<V>();
}
const ValClass = typeToConstructor(registry, valType);
if (isHex(value)) {
return decodeBTreeSet(registry, ValClass, hexToU8a(value));
} else if (isU8a(value)) {
return decodeBTreeSetFromU8a<V>(registry, ValClass, u8aToU8a(value));
} else if (value instanceof Set) {
return decodeBTreeSetFromSet<V>(registry, ValClass, value);
}
throw new Error('BTreeSet: cannot decode type');
}
export default class BTreeSet<V extends Codec = Codec> extends Set<V> implements Codec {
public readonly registry: Registry;
private readonly _ValClass: Constructor<V>;
readonly #ValClass: Constructor<V>;
constructor (registry: Registry, valType: Constructor<V> | keyof InterfaceTypes, rawValue: any) {
const ValClass = typeToConstructor(registry, valType);
super(BTreeSet.decodeBTreeSet(registry, ValClass, rawValue));
super(decodeBTreeSet(registry, valType, rawValue));
this.registry = registry;
this._ValClass = ValClass;
}
/**
* Decode input to pass into constructor.
*
* @param ValClass - Type of the map value
* @param value - Value to decode, one of:
* - null
* - undefined
* - hex
* - Uint8Array
* - Set<any>, where both key and value types are either
* constructors or decodeable values for their types.
* @param jsonSet
* @internal
*/
private static decodeBTreeSet<V extends Codec = Codec> (registry: Registry, ValClass: Constructor<V>, value: Uint8Array | string | Set<any>): Set<V> {
if (!value) {
return new Set<V>();
} else if (isHex(value)) {
return BTreeSet.decodeBTreeSet(registry, ValClass, hexToU8a(value));
} else if (isU8a(value)) {
return BTreeSet.decodeBTreeSetFromU8a<V>(registry, ValClass, u8aToU8a(value));
} else if (value instanceof Set) {
return BTreeSet.decodeBTreeSetFromSet<V>(registry, ValClass, value);
}
throw new Error('BTreeSet: cannot decode type');
}
/** @internal */
private static decodeBTreeSetFromU8a<V extends Codec = Codec> (registry: Registry, ValClass: Constructor<V>, u8a: Uint8Array): Set<V> {
const output = new Set<V>();
const [offset, length] = Compact.decodeU8a(u8a);
const types = [];
for (let i = 0; i < length.toNumber(); i++) {
types.push(ValClass);
}
const values = decodeU8a(registry, u8a.subarray(offset), types);
for (let i = 0; i < values.length; i++) {
output.add(values[i] as V);
}
return output;
}
/** @internal */
private static decodeBTreeSetFromSet<V extends Codec = Codec> (registry: Registry, ValClass: Constructor<V>, value: Set<any>): Set<V> {
const output = new Set<V>();
value.forEach((v: any) => {
let val;
try {
val = (v instanceof ValClass) ? v : new ValClass(registry, v);
} catch (error) {
console.error('Failed to decode BTreeSet key or value:', error.message);
throw error;
}
output.add(val);
});
return output;
this.#ValClass = typeToConstructor(registry, valType);
}
public static with<V extends Codec> (valType: Constructor<V> | keyof InterfaceTypes): Constructor<BTreeSet<V>> {
@@ -171,7 +173,7 @@ export default class BTreeSet<V extends Codec = Codec> extends Set<V> implements
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `BTreeSet<${new this._ValClass(this.registry).toRawType()}>`;
return `BTreeSet<${new this.#ValClass(this.registry).toRawType()}>`;
}
/**
+12 -10
View File
@@ -18,11 +18,15 @@ function decodeOptionU8a (registry: Registry, Type: Constructor, value: Uint8Arr
}
/** @internal */
function decodeOption (registry: Registry, Type: Constructor, value?: any): Codec {
function decodeOption (registry: Registry, typeName: Constructor | keyof InterfaceTypes, value?: any): Codec {
if (isNull(value) || isUndefined(value) || value instanceof Null) {
return new Null(registry);
}
const Type = typeToConstructor(registry, typeName);
// eslint-disable-next-line @typescript-eslint/no-use-before-define
} else if (value instanceof Option) {
if (value instanceof Option) {
return decodeOption(registry, Type, value.value);
} else if (value instanceof Type) {
// don't re-create, use as it (which also caters for derived types)
@@ -45,14 +49,12 @@ function decodeOption (registry: Registry, Type: Constructor, value?: any): Code
* with a value if/as required/found.
*/
export default class Option<T extends Codec> extends Base<T> {
private _Type: Constructor<T>;
readonly #Type: Constructor<T>;
constructor (registry: Registry, Type: Constructor<T> | keyof InterfaceTypes, value?: any) {
const Clazz = typeToConstructor(registry, Type);
constructor (registry: Registry, typeName: Constructor<T> | keyof InterfaceTypes, value?: any) {
super(registry, decodeOption(registry, typeName, value));
super(registry, decodeOption(registry, Clazz, value));
this._Type = Clazz;
this.#Type = typeToConstructor(registry, typeName);
}
public static with<O extends Codec> (Type: Constructor<O> | keyof InterfaceTypes): Constructor<Option<O>> {
@@ -125,7 +127,7 @@ export default class Option<T extends Codec> extends Base<T> {
* @description Returns the base runtime type name for this instance
*/
public toRawType (isBare?: boolean): string {
const wrapped = new this._Type(this.registry).toRawType();
const wrapped = new this.#Type(this.registry).toRawType();
return isBare
? wrapped
@@ -179,6 +181,6 @@ export default class Option<T extends Codec> extends Base<T> {
public unwrapOrDefault (): T {
return this.isSome
? this.unwrap()
: new this._Type(this.registry);
: new this.#Type(this.registry);
}
}
+17 -17
View File
@@ -50,7 +50,11 @@ function decodeSetNumber (setValues: SetValues, _value: BN | number): string[] {
}
/** @internal */
function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8Array | BN | number | string = 0, byteLength: number): string[] {
function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8Array | BN | number | string = 0, bitLength: number): string[] {
assert(bitLength % 8 === 0, `Expected valid bitLength, power of 8, found ${bitLength}`);
const byteLength = bitLength / 8;
if (isString(value)) {
return decodeSet(setValues, u8aToU8a(value), byteLength);
} else if (isU8a(value)) {
@@ -78,20 +82,16 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
export default class CodecSet extends Set<string> implements Codec {
public readonly registry: Registry;
protected _byteLength: number;
readonly #allowed: SetValues;
protected _setValues: SetValues;
readonly #byteLength: number;
constructor (registry: Registry, setValues: SetValues, value?: string[] | Set<string> | Uint8Array | BN | number | string, bitLength = 8) {
assert(bitLength % 8 === 0, `Expected valid bitLength, power of 8, found ${bitLength}`);
const byteLength = bitLength / 8;
super(decodeSet(setValues, value, byteLength));
super(decodeSet(setValues, value, bitLength));
this.registry = registry;
this._byteLength = byteLength;
this._setValues = setValues;
this.#allowed = setValues;
this.#byteLength = bitLength / 8;
}
public static with (values: SetValues, bitLength?: number): Constructor<CodecSet> {
@@ -119,7 +119,7 @@ export default class CodecSet extends Set<string> implements Codec {
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this._byteLength;
return this.#byteLength;
}
/**
@@ -147,16 +147,17 @@ export default class CodecSet extends Set<string> implements Codec {
* @description The encoded value for the set members
*/
public get valueEncoded (): BN {
return encodeSet(this._setValues, this.strings);
return encodeSet(this.#allowed, this.strings);
}
/**
* @description adds a value to the Set (extended to allow for validity checking)
*/
public add (key: string): this {
public add = (key: string): this => {
// ^^^ add = () property done to assign this instance's this, otherwise Set.add creates "some" chaos
// we have the isUndefined(this._setValues) in here as well, add is used internally
// in the Set constructor (so it is undefined at this point, and should allow)
assert(isUndefined(this._setValues) || !isUndefined(this._setValues[key]), `Set: Invalid key '${key}' on add`);
assert(isUndefined(this.#allowed) || !isUndefined(this.#allowed[key]), `Set: Invalid key '${key}' on add`);
super.add(key);
@@ -211,8 +212,7 @@ export default class CodecSet extends Set<string> implements Codec {
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
// FIXME We don't cater for this in createType as of yet
return JSON.stringify({ _set: this._setValues });
return JSON.stringify({ _set: this.#allowed });
}
/**
@@ -228,6 +228,6 @@ export default class CodecSet extends Set<string> implements Codec {
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toU8a (isBare?: boolean): Uint8Array {
return bnToU8a(this.valueEncoded, { isLe: false, bitLength: this._byteLength * 8 });
return bnToU8a(this.valueEncoded, { isLe: false, bitLength: this.#byteLength * 8 });
}
}
+12 -13
View File
@@ -105,19 +105,18 @@ export default class Struct<
E extends { [K in keyof S]: string } = { [K in keyof S]: string }> extends Map<keyof S, Codec> implements Codec {
public readonly registry: Registry;
protected _jsonMap: Map<keyof S, string>;
readonly #jsonMap: Map<keyof S, string>;
protected _Types: ConstructorDef;
readonly #Types: ConstructorDef;
constructor (registry: Registry, Types: S, value: V | Map<any, any> | any[] | string = {} as V, jsonMap: Map<keyof S, string> = new Map()) {
const Clazzes = mapToTypeMap(registry, Types);
const decoded: T = decodeStruct(registry, Clazzes, value, jsonMap);
super(Object.entries(decoded));
super(Object.entries(
decodeStruct(registry, mapToTypeMap(registry, Types), value, jsonMap)
) as [keyof S, Codec][]);
this.registry = registry;
this._jsonMap = jsonMap;
this._Types = Clazzes;
this.#jsonMap = jsonMap;
this.#Types = mapToTypeMap(registry, Types);
}
public static with<S extends TypesDef> (Types: S): Constructor<Struct<S>> {
@@ -144,7 +143,7 @@ export default class Struct<
* @description The available keys for this enum
*/
public get defKeys (): string[] {
return Object.keys(this._Types);
return Object.keys(this.#Types);
}
/**
@@ -167,7 +166,7 @@ export default class Struct<
*/
public get Type (): E {
return (Object
.entries(this._Types) as [keyof S, Constructor][])
.entries(this.#Types) as [keyof S, Constructor][])
.reduce((result: E, [key, Type]): E => {
(result as any)[key] = new Type(this.registry).toRawType();
@@ -234,7 +233,7 @@ export default class Struct<
*/
public toHuman (isExtended?: boolean): AnyJson {
return [...this.keys()].reduce((json, key): any => {
const jsonKey = this._jsonMap.get(key) || key;
const jsonKey = this.#jsonMap.get(key) || key;
const value = this.get(key);
json[jsonKey] = value && value.toHuman(isExtended);
@@ -250,7 +249,7 @@ export default class Struct<
// FIXME the return type string is only used by Extrinsic (extends Struct),
// but its toJSON is the hex value
return [...this.keys()].reduce((json, key): any => {
const jsonKey = this._jsonMap.get(key) || key;
const jsonKey = this.#jsonMap.get(key) || key;
const value = this.get(key);
json[jsonKey] = value && value.toJSON();
@@ -272,7 +271,7 @@ export default class Struct<
*/
public toRawType (): string {
return JSON.stringify(
Struct.typesToMap(this.registry, this._Types)
Struct.typesToMap(this.registry, this.#Types)
);
}