Lazy decoration (part 5, augmentObject) (#4145)
* Lazy decoration (part 5, augmentObject) * hasOwnProperty update * Fix comments * loop indexes * Extract lazySection * Use hasOwnProperty on is/as getters * defineProperty helper * Update defineProperty & use for loops * skip/adjust for some keys
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
// Copyright 2017-2021 @polkadot/api authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { lazyMethod } from '@polkadot/types';
|
||||
import { logger } from '@polkadot/util';
|
||||
|
||||
type StringsStrings = [string[], string[]];
|
||||
|
||||
const l = logger('api/augment');
|
||||
|
||||
function clearObject (obj: Record<string, unknown>): void {
|
||||
const keys = Object.keys(obj);
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
delete obj[keys[i]];
|
||||
}
|
||||
}
|
||||
|
||||
function logLength (type: 'added' | 'removed', values: string[], and: string[] = []): string {
|
||||
return values.length
|
||||
? ` ${values.length} ${type}${and.length ? ' and' : ''}`
|
||||
@@ -34,7 +43,7 @@ function findSectionIncludes (a: string[], b: string[]): string[] {
|
||||
return a.filter((s) => b.includes(s));
|
||||
}
|
||||
|
||||
function extractSections (src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>): StringsStrings {
|
||||
function extractSections (src: Record<string, Record<string, unknown>>, dst: Record<string, Record<string, unknown>>): StringsStrings {
|
||||
const srcSections = Object.keys(src);
|
||||
const dstSections = Object.keys(dst);
|
||||
|
||||
@@ -44,7 +53,7 @@ function extractSections (src: Record<string, Record<string, any>>, dst: Record<
|
||||
];
|
||||
}
|
||||
|
||||
function findMethodExcludes (src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>): string[] {
|
||||
function findMethodExcludes (src: Record<string, Record<string, unknown>>, dst: Record<string, Record<string, unknown>>): string[] {
|
||||
const srcSections = Object.keys(src);
|
||||
const dstSections = findSectionIncludes(Object.keys(dst), srcSections);
|
||||
const excludes: string[] = [];
|
||||
@@ -64,27 +73,48 @@ function findMethodExcludes (src: Record<string, Record<string, any>>, dst: Reco
|
||||
return excludes;
|
||||
}
|
||||
|
||||
function extractMethods (src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>): StringsStrings {
|
||||
function extractMethods (src: Record<string, Record<string, unknown>>, dst: Record<string, Record<string, unknown>>): StringsStrings {
|
||||
return [
|
||||
findMethodExcludes(dst, src),
|
||||
findMethodExcludes(src, dst)
|
||||
];
|
||||
}
|
||||
|
||||
function lazySection (section: string, src: Record<string, Record<string, unknown>>, dst: Record<string, Record<string, unknown>>): void {
|
||||
// NOTE: Unlike the other lazy decorations, here we actually do decorate the
|
||||
// sections, just the internal form is not available (i.e. we skip lazyMethods)
|
||||
// The initial issue is that we have a confict between "this already exists" and
|
||||
// "this needs to start clean", so for a first round we skip...
|
||||
if (!dst[section]) {
|
||||
dst[section] = {};
|
||||
}
|
||||
|
||||
const creator = (method: string) => src[section][method];
|
||||
const methods = Object.keys(src[section]);
|
||||
|
||||
for (let i = 0; i < methods.length; i++) {
|
||||
const method = methods[i];
|
||||
|
||||
// We use hasOwnproperty here to only check for the existence of the key,
|
||||
// instead of reading dst[section][method] which will evaluate when already
|
||||
// set as a lazy value previously
|
||||
if (!Object.prototype.hasOwnProperty.call(dst[section], method)) {
|
||||
lazyMethod(dst[section], method, creator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a decorated api section (e.g. api.tx) and augment it with the details. It does not override what is
|
||||
* already available, but rather just adds new missing ites into the result object.
|
||||
* @description Takes a decorated api section (e.g. api.tx) and augment it with the details. It does not override what is
|
||||
* already available, but rather just adds new missing items into the result object.
|
||||
* @internal
|
||||
*/
|
||||
export function augmentObject (prefix: string | null, src: Record<string, Record<string, unknown>>, dst: Record<string, Record<string, unknown>>, fromEmpty = false): Record<string, Record<string, any>> {
|
||||
if (fromEmpty) {
|
||||
const dstKeys = Object.keys(dst);
|
||||
|
||||
for (let k = 0; k < dstKeys.length; k++) {
|
||||
delete dst[dstKeys[k]];
|
||||
}
|
||||
}
|
||||
fromEmpty && clearObject(dst);
|
||||
|
||||
// NOTE: This part is slightly problematic since it will get the
|
||||
// values for at least all the sections and the names of the methods
|
||||
// (Since methods won't be decorated before lazy, this _may_ be ok)
|
||||
if (prefix && Object.keys(dst).length) {
|
||||
warn(prefix, 'modules', extractSections(src, dst));
|
||||
warn(prefix, 'calls', extractMethods(src, dst));
|
||||
@@ -92,23 +122,8 @@ export function augmentObject (prefix: string | null, src: Record<string, Record
|
||||
|
||||
const srcKeys = Object.keys(src);
|
||||
|
||||
for (let s = 0; s < srcKeys.length; s++) {
|
||||
const section = srcKeys[s];
|
||||
|
||||
if (!dst[section]) {
|
||||
dst[section] = {};
|
||||
}
|
||||
|
||||
const methods = Object.keys(src[section]);
|
||||
|
||||
for (let m = 0; m < methods.length; m++) {
|
||||
const method = methods[m];
|
||||
|
||||
// TODO When it does match, check the actual details and warn when there are differences
|
||||
if (!dst[section][method]) {
|
||||
dst[section][method] = src[section][method];
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < srcKeys.length; i++) {
|
||||
lazySection(srcKeys[i], src, dst);
|
||||
}
|
||||
|
||||
return dst;
|
||||
|
||||
@@ -9,7 +9,7 @@ import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, isUndefin
|
||||
|
||||
import { Null } from '../primitive/Null';
|
||||
import { Struct } from './Struct';
|
||||
import { mapToTypeMap } from './utils';
|
||||
import { defineProperty, mapToTypeMap } from './utils';
|
||||
|
||||
// export interface, this is used in Enum.with, so required as public by TS
|
||||
export interface EnumConstructor<T = Codec> {
|
||||
@@ -202,27 +202,21 @@ export class Enum implements IEnum {
|
||||
constructor (registry: Registry, value?: unknown, index?: number) {
|
||||
super(registry, Types, value, index);
|
||||
|
||||
Object.keys(this.#def).forEach((_key): void => {
|
||||
const name = stringUpperFirst(stringCamelCase(_key.replace(' ', '_')));
|
||||
const keys = Object.keys(this.#def);
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i];
|
||||
const name = stringUpperFirst(stringCamelCase(key.replace(' ', '_')));
|
||||
const askey = `as${name}`;
|
||||
const iskey = `is${name}`;
|
||||
|
||||
isUndefined(this[iskey as keyof this]) &&
|
||||
Object.defineProperty(this, iskey, {
|
||||
enumerable: true,
|
||||
get: () => this.type === _key
|
||||
});
|
||||
defineProperty(this, iskey, () => this.type === key);
|
||||
defineProperty(this, askey, (): Codec => {
|
||||
assert(this[iskey as keyof this], () => `Cannot convert '${this.type}' via ${askey}`);
|
||||
|
||||
isUndefined(this[askey as keyof this]) &&
|
||||
Object.defineProperty(this, askey, {
|
||||
enumerable: true,
|
||||
get: (): Codec => {
|
||||
assert(this[iskey as keyof this], () => `Cannot convert '${this.type}' via ${askey}`);
|
||||
|
||||
return this.value;
|
||||
}
|
||||
});
|
||||
});
|
||||
return this.value;
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -281,7 +275,7 @@ export class Enum implements IEnum {
|
||||
* @deprecated use isNone
|
||||
*/
|
||||
public get isNull (): boolean {
|
||||
return this.isNone;
|
||||
return this.#raw instanceof Null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,9 +5,9 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyJson, Codec, Registry } from '../types';
|
||||
|
||||
import { isFunction, isUndefined, stringify } from '@polkadot/util';
|
||||
import { isFunction, stringify } from '@polkadot/util';
|
||||
|
||||
import { compareMap } from './utils';
|
||||
import { compareMap, defineProperty } from './utils';
|
||||
|
||||
/** @internal */
|
||||
function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
|
||||
@@ -34,13 +34,11 @@ export class Json extends Map<string, any> implements Codec {
|
||||
|
||||
this.registry = registry;
|
||||
|
||||
decoded.forEach(([key]): void => {
|
||||
isUndefined(this[key as keyof this]) &&
|
||||
Object.defineProperty(this, key, {
|
||||
enumerable: true,
|
||||
get: (): Codec | undefined => this.get(key) as Codec
|
||||
});
|
||||
});
|
||||
for (let i = 0; i < decoded.length; i++) {
|
||||
const [key] = decoded[i];
|
||||
|
||||
defineProperty(this, key, () => this.get(key) as Codec);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { Constructor, ISet, Registry } from '../types';
|
||||
|
||||
import { assert, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, stringCamelCase, stringify, stringUpperFirst, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { compareArray } from './utils';
|
||||
import { compareArray, defineProperty } from './utils';
|
||||
|
||||
type SetValues = Record<string, number | BN>;
|
||||
|
||||
@@ -108,15 +108,13 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
constructor (registry: Registry, value?: unknown) {
|
||||
super(registry, values, value as undefined, bitLength);
|
||||
|
||||
Object.keys(values).forEach((_key): void => {
|
||||
const iskey = `is${stringUpperFirst(stringCamelCase(_key))}`;
|
||||
const keys = Object.keys(values);
|
||||
|
||||
isUndefined(this[iskey as keyof this]) &&
|
||||
Object.defineProperty(this, iskey, {
|
||||
enumerable: true,
|
||||
get: () => this.strings.includes(_key)
|
||||
});
|
||||
});
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i];
|
||||
|
||||
defineProperty(this, `is${stringUpperFirst(stringCamelCase(key))}`, () => this.strings.includes(key));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { AnyJson, BareOpts, Codec, Constructor, ConstructorDef, IStruct, Re
|
||||
|
||||
import { assert, hexToU8a, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, stringCamelCase, stringify, u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { compareMap, decodeU8a, mapToTypeMap } from './utils';
|
||||
import { compareMap, decodeU8a, defineProperty, mapToTypeMap } from './utils';
|
||||
|
||||
type TypesDef<T = Codec> = Record<string, string | Constructor<T>>;
|
||||
|
||||
@@ -147,13 +147,13 @@ export class Struct<
|
||||
constructor (registry: Registry, value?: unknown) {
|
||||
super(registry, Types, value as HexString, jsonMap);
|
||||
|
||||
Object.keys(Types).forEach((key): void => {
|
||||
isUndefined(this[key as keyof this]) &&
|
||||
Object.defineProperty(this, key, {
|
||||
enumerable: true,
|
||||
get: (): Codec | undefined => this.get(key as keyof S)
|
||||
});
|
||||
});
|
||||
const keys = Object.keys(Types);
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i];
|
||||
|
||||
defineProperty(this, key, () => this.get(key as keyof S));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isUndefined } from '@polkadot/util';
|
||||
|
||||
const SKIP_KEYS = ['hash'];
|
||||
|
||||
export function defineProperty (that: object, key: string, get: () => unknown): void {
|
||||
if (SKIP_KEYS.includes(key)) {
|
||||
return defineProperty(that, `_${key}`, get);
|
||||
}
|
||||
|
||||
// We use an isUndefined check here, not hasOwwnProperty since there are set in derived
|
||||
// classes (inside with) and _Own_ properties refers to the class only, not parents
|
||||
if (isUndefined((that as Record<string, unknown>)[key])) {
|
||||
Object.defineProperty(that, key, { enumerable: true, get });
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ export { compareArray } from './compareArray';
|
||||
export { compareMap } from './compareMap';
|
||||
export { compareSet } from './compareSet';
|
||||
export { decodeU8a } from './decodeU8a';
|
||||
export { defineProperty } from './defineProperty';
|
||||
export { mapToTypeMap } from './mapToTypeMap';
|
||||
export { typeToConstructor } from './typeToConstructor';
|
||||
export { sortAsc, sortSet, sortMap } from './sortValues';
|
||||
|
||||
@@ -7,7 +7,7 @@ import { GenericVote } from '.';
|
||||
describe('GenericVote', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
describe('construction', (): void => {
|
||||
describe.only('construction', (): void => {
|
||||
it('constructs via boolean true', (): void => {
|
||||
expect(new GenericVote(registry, true).toU8a()).toEqual(new Uint8Array([128]));
|
||||
expect(new GenericVote(registry, true).isAye).toBe(true);
|
||||
@@ -58,7 +58,7 @@ describe('GenericVote', (): void => {
|
||||
expect(new GenericVote(registry, new Uint8Array([1])).isNay).toBe(true);
|
||||
});
|
||||
|
||||
it('constructs via Uint8Array (aye)', (): void => {
|
||||
it.only('constructs via Uint8Array (aye)', (): void => {
|
||||
const test = new GenericVote(registry, new Uint8Array([0b10000010]));
|
||||
|
||||
expect(test.isNay).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user