This commit is contained in:
Akim Mamedov
2023-11-17 03:22:21 +07:00
parent 1669750e0a
commit 05bec9db21
5 changed files with 40 additions and 23 deletions

View File

@ -25,6 +25,7 @@ import type {
import { CallAquaFunctionConfig } from "./compilerSupport/callFunction.js";
import {
aqua2ts,
SchemaValidationError,
ts2aqua,
wrapFunction,
} from "./compilerSupport/conversions.js";
@ -37,10 +38,11 @@ const isAquaConfig = (
config: JSONValue | ServiceImpl[string] | undefined,
): config is CallAquaFunctionConfig => {
return (
typeof config === "object" &&
config !== null &&
!Array.isArray(config) &&
["undefined", "number"].includes(typeof config["ttl"])
config === undefined ||
(typeof config === "object" &&
config !== null &&
!Array.isArray(config) &&
["undefined", "number"].includes(typeof config["ttl"]))
);
};
@ -60,14 +62,17 @@ export const v5_callFunction = async (
def: FunctionCallDef,
script: string,
): Promise<unknown> => {
const [peer, ...rest] = args;
const [peerOrArg, ...rest] = args;
if (!(peer instanceof FluencePeer)) {
await v5_callFunction([getDefaultPeer(), ...rest], def, script);
if (!(peerOrArg instanceof FluencePeer)) {
await v5_callFunction([getDefaultPeer(), peerOrArg, ...rest], def, script);
return;
}
const argNames = Object.keys(def.arrow);
const argNames = Object.keys(
def.arrow.domain.tag === "nil" ? [] : def.arrow.domain.fields,
);
const schemaArgCount = argNames.length;
type FunctionArg = SimpleTypes | ArrowWithoutCallbacks;
@ -88,7 +93,12 @@ export const v5_callFunction = async (
if (argSchema.tag === "arrow") {
if (typeof arg !== "function") {
throw new Error("Argument and schema don't match");
throw new SchemaValidationError(
[argNames[i]],
argSchema,
"function",
arg,
);
}
const wrappedFunction = wrapFunction(arg, argSchema);
@ -97,7 +107,12 @@ export const v5_callFunction = async (
}
if (typeof arg === "function") {
throw new Error("Argument and schema don't match");
throw new SchemaValidationError(
[argNames[i]],
argSchema,
"non-function value",
arg,
);
}
return [
@ -111,7 +126,7 @@ export const v5_callFunction = async (
def.arrow.codomain.tag === "nil" || def.arrow.codomain.items.length === 0;
const params = {
peer,
peer: peerOrArg,
args: callArgs,
config,
};

View File

@ -28,12 +28,12 @@ import { ParticleContext } from "../jsServiceHost/interfaces.js";
import { ServiceImpl } from "./types.js";
class SchemaValidationError extends Error {
export class SchemaValidationError extends Error {
constructor(
public path: string[],
schema: NonArrowSimpleType,
schema: NonArrowSimpleType | ArrowWithoutCallbacks,
expected: string,
provided: JSONValue,
provided: JSONValue | ServiceImpl[string],
) {
const given =
provided === null