fix(js-client): Improve logging of conversion API (#429)

* Improve logging of conversion API

* Fix arg name
This commit is contained in:
Akim 2024-02-09 07:49:33 +07:00 committed by GitHub
parent 00db991332
commit 2b1d0f7f05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 17 deletions

View File

@ -102,7 +102,7 @@ export const v5_callFunction = async (
); );
} }
return [argName, wrapJsFunction(arg, argType)]; return [argName, wrapJsFunction(arg, argType, argName)];
} }
if (typeof arg === "function") { if (typeof arg === "function") {
@ -114,7 +114,7 @@ export const v5_callFunction = async (
); );
} }
return [argName, js2aqua(arg, argType, { path: [def.functionName] })]; return [argName, js2aqua(arg, argType, { path: [argName] })];
}, },
), ),
); );
@ -141,7 +141,9 @@ export const v5_callFunction = async (
result = null; result = null;
} }
return aqua2js(result, returnSchema); return aqua2js(result, returnSchema, {
path: [`${def.functionName}ReturnValue`],
});
}; };
const getDefaultPeer = (): FluencePeer => { const getDefaultPeer = (): FluencePeer => {
@ -215,7 +217,11 @@ export const v5_registerService = (
return [ return [
schemaKey, schemaKey,
wrapJsFunction(serviceImplValue.bind(serviceImpl), schemaValue), wrapJsFunction(
serviceImplValue.bind(serviceImpl),
schemaValue,
schemaKey,
),
] as const; ] as const;
}), }),
); );

View File

@ -200,7 +200,7 @@ describe("Conversion from aqua to typescript", () => {
// arrange // arrange
// act // act
const tsFromAqua = aqua2js(aqua, type); const tsFromAqua = aqua2js(aqua, type, { path: [] });
const aquaFromTs = js2aqua(ts, type, { path: [] }); const aquaFromTs = js2aqua(ts, type, { path: [] });
// assert // assert
@ -232,7 +232,7 @@ describe("Conversion corner cases", () => {
// act // act
const aqua = js2aqua(valueInTs, type, { path: [] }); const aqua = js2aqua(valueInTs, type, { path: [] });
const ts = aqua2js(valueInAqua, type); const ts = aqua2js(valueInAqua, type, { path: [] });
// assert // assert
expect(aqua).toStrictEqual({ expect(aqua).toStrictEqual({

View File

@ -97,16 +97,17 @@ function isScalar(
export function aqua2js( export function aqua2js(
value: JSONValue, value: JSONValue,
schema: NonArrowSimpleType, schema: NonArrowSimpleType,
{ path }: ValidationContext,
): JSONValue { ): JSONValue {
if (schema.tag === "nil") { if (schema.tag === "nil") {
return null; return null;
} else if (schema.tag === "option") { } else if (schema.tag === "option") {
if (!Array.isArray(value)) { if (!Array.isArray(value)) {
throw new SchemaValidationError([], schema, "array", value); throw new SchemaValidationError(path, schema, "array", value);
} }
if ("0" in value) { if ("0" in value) {
return aqua2js(value[0], schema.type); return aqua2js(value[0], schema.type, { path: [...path, "?"] });
} else { } else {
return null; return null;
} }
@ -121,16 +122,16 @@ export function aqua2js(
throw new SchemaValidationError([], schema, "array", value); throw new SchemaValidationError([], schema, "array", value);
} }
return value.map((y) => { return value.map((y, i) => {
return aqua2js(y, schema.type); return aqua2js(y, schema.type, { path: [...path, `[${i}]`] });
}); });
} else if (schema.tag === "unlabeledProduct") { } else if (schema.tag === "unlabeledProduct") {
if (!Array.isArray(value)) { if (!Array.isArray(value)) {
throw new SchemaValidationError([], schema, "array", value); throw new SchemaValidationError([], schema, "array", value);
} }
return zip(value, schema.items).map(([v, s]) => { return zip(value, schema.items).map(([v, s], i) => {
return aqua2js(v, s); return aqua2js(v, s, { path: [...path, `[${i}]`] });
}); });
} else if (["labeledProduct", "struct"].includes(schema.tag)) { } else if (["labeledProduct", "struct"].includes(schema.tag)) {
if (typeof value !== "object" || value === null || Array.isArray(value)) { if (typeof value !== "object" || value === null || Array.isArray(value)) {
@ -145,7 +146,7 @@ export function aqua2js(
v = null; v = null;
} }
const val = aqua2js(v, type); const val = aqua2js(v, type, { path: [...path, key] });
return [key, val]; return [key, val];
}), }),
); );
@ -167,7 +168,9 @@ export function js2aqua(
return value; return value;
} else if (schema.tag === "option") { } else if (schema.tag === "option") {
// option means 'type | null' // option means 'type | null'
return value === null ? [] : [js2aqua(value, schema.type, { path })]; return value === null
? []
: [js2aqua(value, schema.type, { path: [...path, "?"] })];
} else if (schema.tag === "topType") { } else if (schema.tag === "topType") {
// topType equals to 'any' // topType equals to 'any'
return value; return value;
@ -221,6 +224,7 @@ export const wrapJsFunction = (
schema: schema:
| ArrowWithoutCallbacks | ArrowWithoutCallbacks
| ArrowType<LabeledProductType<SimpleTypes> | UnlabeledProductType>, | ArrowType<LabeledProductType<SimpleTypes> | UnlabeledProductType>,
funcName: string,
): ServiceImpl[string] => { ): ServiceImpl[string] => {
return async ({ args, context }) => { return async ({ args, context }) => {
const schemaArgs = const schemaArgs =
@ -236,8 +240,8 @@ export const wrapJsFunction = (
); );
} }
const jsArgs = zip(args, schemaArgs).map(([arg, schemaArg]) => { const jsArgs = zip(args, schemaArgs).map(([arg, schemaArg], i) => {
return aqua2js(arg, schemaArg); return aqua2js(arg, schemaArg, { path: [`${funcName}Args`, `[${i}]`] });
}); });
const returnTypeVoid = const returnTypeVoid =
@ -256,6 +260,6 @@ export const wrapJsFunction = (
result = null; result = null;
} }
return js2aqua(result, resultSchema, { path: [] }); return js2aqua(result, resultSchema, { path: [`${funcName}ReturnValue`] });
}; };
}; };