From 6a9af6d22cddf4792dedc4fabd95aab034b2f103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Tue, 19 Sep 2023 10:35:05 +0200 Subject: [PATCH] chore: attend pr review comments --- src/artifacts.ts | 12 +++++++----- src/retrievals.ts | 4 ++-- src/types.ts | 28 ++++++++++------------------ src/utils/index.js | 2 +- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/artifacts.ts b/src/artifacts.ts index 4f34e02..da8b435 100644 --- a/src/artifacts.ts +++ b/src/artifacts.ts @@ -1,6 +1,8 @@ import { Class as Retrieval } from "./retrievals" import { Class as Reducer, Mode } from "./reducers" +export type Args = string[] | string[][]; + export interface Specs { retrieve: Retrieval[]; aggregate: Reducer; @@ -28,13 +30,13 @@ export class Class { export class Template extends Class { public argsCount: number; - public tests?: Map; + public tests?: Map; constructor(specs: { retrieve: Retrieval[], aggregate?: Reducer, tally?: Reducer, }, - tests?: Map + tests?: Map ) { super({ retrieve: specs.retrieve, @@ -46,8 +48,8 @@ export class Template extends Class { throw EvalError("\x1b[1;33mTemplate: cannot build w/ unparameterized retrievals\x1b[0m") } if (tests) { - Object.keys(tests).map(test => { - let testArgs: string[] | string[][] | undefined = Object(tests)[test] + Object.keys(tests).forEach(test => { + let testArgs: Args | undefined = Object(tests)[test] if (Array.isArray(testArgs) && testArgs.length > 0) { if (!Array.isArray(testArgs[0])) { Object(tests)[test] = Array(specs.retrieve.length).fill(testArgs) @@ -55,7 +57,7 @@ export class Template extends Class { } else if (testArgs?.length != specs.retrieve.length) { throw EvalError(`\x1b[1;33mTemplate: arguments mismatch in test \x1b[1;31m'${test}'\x1b[1;33m: ${testArgs?.length} tuples given vs. ${specs.retrieve.length} expected\x1b[0m`) } - testArgs?.map((subargs, index)=> { + testArgs?.forEach((subargs, index)=> { if (subargs.length < specs.retrieve[index].argsCount) { throw EvalError(`\x1b[1;33mTemplate: arguments mismatch in test \x1b[1;31m'${test}'\x1b[1;33m: \x1b[1;37mRetrieval #${index}\x1b[1;33m: ${subargs?.length} parameters given vs. ${specs.retrieve[index].argsCount} expected\x1b[0m`) } diff --git a/src/retrievals.ts b/src/retrievals.ts index 64cc0a7..c2dd0d7 100644 --- a/src/retrievals.ts +++ b/src/retrievals.ts @@ -75,13 +75,13 @@ export class Class { * @returns An array with as many retrievals as spawning values were specified. */ public spawn(argIndex: number, values: string[]): Class[] { - let spawned: Class[] = [] + const spawned: Class[] = [] if (this.argsCount == 0) { throw new EvalError(`\x1b[1;33mRetrieval: cannot spawn over unparameterized retrieval\x1b[0m`); } else if (argIndex > this.argsCount) { throw new EvalError(`\x1b[1;33mRetrieval: spawning parameter index out of range: ${argIndex} > ${this.argsCount}\x1b[0m`); } - values.map(value => { + values.forEach(value => { let headers: any if (this.headers) { this.headers?.map(header => { diff --git a/src/types.ts b/src/types.ts index ea58236..cd4847a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -49,25 +49,17 @@ export class RadonType { * @param argValue Value used to replace given argument. */ public _spliceWildcards(argIndex: number, argValue: string): RadonType { + const RadonClass = [ + RadonArray, + RadonBoolean, + RadonBytes, + RadonFloat, + RadonInteger, + RadonMap, + RadonString + ].find(RadonClass => this instanceof RadonClass) || RadonType; const argsCount: number = this._countArgs() - let spliced: RadonType - if (this instanceof RadonArray) { - spliced = new RadonArray(this._prev?._spliceWildcards(argIndex, argValue), this._key) - } else if (this instanceof RadonBoolean) { - spliced = new RadonBoolean(this._prev?._spliceWildcards(argIndex, argValue), this._key) - } else if (this instanceof RadonBytes) { - spliced = new RadonBytes(this._prev?._spliceWildcards(argIndex, argValue), this._key) - } else if (this instanceof RadonFloat) { - spliced = new RadonFloat(this._prev?._spliceWildcards(argIndex, argValue), this._key) - } else if (this instanceof RadonInteger) { - spliced = new RadonInteger(this._prev?._spliceWildcards(argIndex, argValue), this._key) - } else if (this instanceof RadonMap) { - spliced = new RadonMap(this._prev?._spliceWildcards(argIndex, argValue), this._key) - } else if (this instanceof RadonString) { - spliced = new RadonString(this._prev?._spliceWildcards(argIndex, argValue), this._key) - } else { - spliced = new RadonType(this._prev?._spliceWildcards(argIndex, argValue), this._key) - } + const spliced = new RadonClass(this._prev?._spliceWildcards(argIndex, argValue), this._key); spliced._set( utils.spliceWildcards(this._bytecode, argIndex, argValue, argsCount), this._method, diff --git a/src/utils/index.js b/src/utils/index.js index 9cebcf6..5cb912f 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -515,7 +515,7 @@ function web3Encode(T) { T.headers || "", web3Encode(T.script) || "0x80" ]; - } else if (T instanceof Witnet.Types.Script) { + } else if (T instanceof Witnet.Types.RadonType) { return cbor.encode(T._encodeArray()) } return T;