Skip to content

Commit

Permalink
chore: attend pr review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Sep 19, 2023
1 parent 76aa3e2 commit 6a9af6d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
12 changes: 7 additions & 5 deletions src/artifacts.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,13 +30,13 @@ export class Class {

export class Template extends Class {
public argsCount: number;
public tests?: Map<string, string[] | string[][]>;
public tests?: Map<string, Args>;
constructor(specs: {
retrieve: Retrieval[],
aggregate?: Reducer,
tally?: Reducer,
},
tests?: Map<string, string[] | string[][]>
tests?: Map<string, Args>
) {
super({
retrieve: specs.retrieve,
Expand All @@ -46,16 +48,16 @@ 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)
testArgs = Object(tests)[test]
} 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`)
}
Expand Down
4 changes: 2 additions & 2 deletions src/retrievals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
28 changes: 10 additions & 18 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6a9af6d

Please sign in to comment.