Skip to content

Commit 2b1d0f7

Browse files
author
Akim
authored
fix(js-client): Improve logging of conversion API (#429)
* Improve logging of conversion API * Fix arg name
1 parent 00db991 commit 2b1d0f7

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

packages/core/js-client/src/api.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export const v5_callFunction = async (
102102
);
103103
}
104104

105-
return [argName, wrapJsFunction(arg, argType)];
105+
return [argName, wrapJsFunction(arg, argType, argName)];
106106
}
107107

108108
if (typeof arg === "function") {
@@ -114,7 +114,7 @@ export const v5_callFunction = async (
114114
);
115115
}
116116

117-
return [argName, js2aqua(arg, argType, { path: [def.functionName] })];
117+
return [argName, js2aqua(arg, argType, { path: [argName] })];
118118
},
119119
),
120120
);
@@ -141,7 +141,9 @@ export const v5_callFunction = async (
141141
result = null;
142142
}
143143

144-
return aqua2js(result, returnSchema);
144+
return aqua2js(result, returnSchema, {
145+
path: [`${def.functionName}ReturnValue`],
146+
});
145147
};
146148

147149
const getDefaultPeer = (): FluencePeer => {
@@ -215,7 +217,11 @@ export const v5_registerService = (
215217

216218
return [
217219
schemaKey,
218-
wrapJsFunction(serviceImplValue.bind(serviceImpl), schemaValue),
220+
wrapJsFunction(
221+
serviceImplValue.bind(serviceImpl),
222+
schemaValue,
223+
schemaKey,
224+
),
219225
] as const;
220226
}),
221227
);

packages/core/js-client/src/compilerSupport/__test__/conversion.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ describe("Conversion from aqua to typescript", () => {
200200
// arrange
201201

202202
// act
203-
const tsFromAqua = aqua2js(aqua, type);
203+
const tsFromAqua = aqua2js(aqua, type, { path: [] });
204204
const aquaFromTs = js2aqua(ts, type, { path: [] });
205205

206206
// assert
@@ -232,7 +232,7 @@ describe("Conversion corner cases", () => {
232232

233233
// act
234234
const aqua = js2aqua(valueInTs, type, { path: [] });
235-
const ts = aqua2js(valueInAqua, type);
235+
const ts = aqua2js(valueInAqua, type, { path: [] });
236236

237237
// assert
238238
expect(aqua).toStrictEqual({

packages/core/js-client/src/compilerSupport/conversions.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,17 @@ function isScalar(
9797
export function aqua2js(
9898
value: JSONValue,
9999
schema: NonArrowSimpleType,
100+
{ path }: ValidationContext,
100101
): JSONValue {
101102
if (schema.tag === "nil") {
102103
return null;
103104
} else if (schema.tag === "option") {
104105
if (!Array.isArray(value)) {
105-
throw new SchemaValidationError([], schema, "array", value);
106+
throw new SchemaValidationError(path, schema, "array", value);
106107
}
107108

108109
if ("0" in value) {
109-
return aqua2js(value[0], schema.type);
110+
return aqua2js(value[0], schema.type, { path: [...path, "?"] });
110111
} else {
111112
return null;
112113
}
@@ -121,16 +122,16 @@ export function aqua2js(
121122
throw new SchemaValidationError([], schema, "array", value);
122123
}
123124

124-
return value.map((y) => {
125-
return aqua2js(y, schema.type);
125+
return value.map((y, i) => {
126+
return aqua2js(y, schema.type, { path: [...path, `[${i}]`] });
126127
});
127128
} else if (schema.tag === "unlabeledProduct") {
128129
if (!Array.isArray(value)) {
129130
throw new SchemaValidationError([], schema, "array", value);
130131
}
131132

132-
return zip(value, schema.items).map(([v, s]) => {
133-
return aqua2js(v, s);
133+
return zip(value, schema.items).map(([v, s], i) => {
134+
return aqua2js(v, s, { path: [...path, `[${i}]`] });
134135
});
135136
} else if (["labeledProduct", "struct"].includes(schema.tag)) {
136137
if (typeof value !== "object" || value === null || Array.isArray(value)) {
@@ -145,7 +146,7 @@ export function aqua2js(
145146
v = null;
146147
}
147148

148-
const val = aqua2js(v, type);
149+
const val = aqua2js(v, type, { path: [...path, key] });
149150
return [key, val];
150151
}),
151152
);
@@ -167,7 +168,9 @@ export function js2aqua(
167168
return value;
168169
} else if (schema.tag === "option") {
169170
// option means 'type | null'
170-
return value === null ? [] : [js2aqua(value, schema.type, { path })];
171+
return value === null
172+
? []
173+
: [js2aqua(value, schema.type, { path: [...path, "?"] })];
171174
} else if (schema.tag === "topType") {
172175
// topType equals to 'any'
173176
return value;
@@ -221,6 +224,7 @@ export const wrapJsFunction = (
221224
schema:
222225
| ArrowWithoutCallbacks
223226
| ArrowType<LabeledProductType<SimpleTypes> | UnlabeledProductType>,
227+
funcName: string,
224228
): ServiceImpl[string] => {
225229
return async ({ args, context }) => {
226230
const schemaArgs =
@@ -236,8 +240,8 @@ export const wrapJsFunction = (
236240
);
237241
}
238242

239-
const jsArgs = zip(args, schemaArgs).map(([arg, schemaArg]) => {
240-
return aqua2js(arg, schemaArg);
243+
const jsArgs = zip(args, schemaArgs).map(([arg, schemaArg], i) => {
244+
return aqua2js(arg, schemaArg, { path: [`${funcName}Args`, `[${i}]`] });
241245
});
242246

243247
const returnTypeVoid =
@@ -256,6 +260,6 @@ export const wrapJsFunction = (
256260
result = null;
257261
}
258262

259-
return js2aqua(result, resultSchema, { path: [] });
263+
return js2aqua(result, resultSchema, { path: [`${funcName}ReturnValue`] });
260264
};
261265
};

0 commit comments

Comments
 (0)