Skip to content

Commit ee47f59

Browse files
authored
Merge pull request #1684 from polywrap/kris/fix-map-arg-parse
Fix: Map type argument parsing for `WrapError`
2 parents f71e155 + cba19e9 commit ee47f59

7 files changed

Lines changed: 90 additions & 20 deletions

File tree

packages/cli/src/commands/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import {
1616
parseManifestFileOption,
1717
parseLogFileOption,
1818
parseWrapperEnvsOption,
19-
typesHandler,
2019
} from "../lib";
2120
import { createLogger } from "./utils/createLogger";
2221

22+
import { typesHandler } from "@polywrap/core-js";
2323
import path from "path";
2424
import yaml from "yaml";
2525
import fs from "fs";

packages/cli/src/lib/helpers/workflow-validator.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Logger } from "../logging";
33
import { intlMsg } from "../intl";
44
import { Status, WorkflowOutput } from "../workflow";
55

6+
import { typesHandler } from "@polywrap/core-js";
67
import path from "path";
78
import fs from "fs";
89
import os from "os";
@@ -14,20 +15,6 @@ export function cueExists(logger: Logger): boolean {
1415
return stdout ? stdout.startsWith("cue version ") : false;
1516
}
1617

17-
export const typesHandler = (_: unknown, value: unknown): unknown => {
18-
if (value instanceof Map) {
19-
return Array.from(value).reduce(
20-
(obj: Record<string, unknown>, [key, value]) => {
21-
obj[key] = value;
22-
return obj;
23-
},
24-
{}
25-
);
26-
}
27-
28-
return value;
29-
};
30-
3118
export function validateOutput(
3219
output: WorkflowOutput,
3320
validateScriptPath: string,

packages/cli/src/lib/workflow/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { intlMsg } from "../intl";
22
import { WorkflowOutput } from "./types";
3-
import { typesHandler } from "../helpers";
43

4+
import { typesHandler } from "@polywrap/core-js";
55
import path from "path";
66
import fs from "fs";
77
import { WorkflowJobs } from "@polywrap/polywrap-manifest-types-js";
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { typesHandler } from "../utils";
2+
3+
describe('typesHandler', () => {
4+
it('should return the original value if it is not a Map', () => {
5+
const value = { a: 1 };
6+
expect(typesHandler(undefined, value)).toEqual(value);
7+
});
8+
9+
it('should return an empty object if the Map keys are not of type string', () => {
10+
const value = new Map([[1, 'one'], [2, 'two']]);
11+
expect(typesHandler(undefined, value)).toEqual({});
12+
});
13+
14+
it('should convert empty Map into empty object', () => {
15+
const value = new Map();
16+
const expected = {};
17+
expect(typesHandler(undefined, value)).toEqual(expected);
18+
});
19+
20+
it('should convert Map object with string keys into object with key-value pairs', () => {
21+
const value = new Map([['a', 1], ['b', 2]]);
22+
const expected = { a: 1, b: 2 };
23+
expect(typesHandler(undefined, value)).toEqual(expected);
24+
});
25+
26+
it('should correctly stringify a Map object with string keys', () => {
27+
const myObject = {
28+
myMap: new Map([['a', 1], ['b', 2]])
29+
};
30+
const expected = '{\n "myMap": {\n "a": 1,\n "b": 2\n }\n}';
31+
32+
const stringified = JSON.stringify(myObject, typesHandler, 2);
33+
expect(stringified).toEqual(expected);
34+
});
35+
36+
it('should correctly stringify a Map object with non-string values', () => {
37+
const myObject = {
38+
myMap: new Map([['a', { foo: 'bar' }]])
39+
};
40+
const expected = '{\n "myMap": {\n "a": {\n "foo": "bar"\n }\n }\n}';
41+
42+
const stringified = JSON.stringify(myObject, typesHandler, 2);
43+
44+
expect(stringified).toEqual(expected);
45+
});
46+
47+
it('should correctly stringify a non-Map object', () => {
48+
const myObject = {
49+
foo: 'bar',
50+
baz: 42
51+
};
52+
const expected = '{\n "foo": "bar",\n "baz": 42\n}';
53+
54+
const stringified = JSON.stringify(myObject, typesHandler, 2);
55+
expect(stringified).toEqual(expected);
56+
});
57+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./combinePaths";
22
export * from "./getEnvFromUriHistory";
33
export * from "./is-buffer";
4+
export * from "./typesHandler";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Converts a Map object with string keys into an object with key-value pairs
3+
* that can be stringified using JSON.stringify. Returns an empty object if the
4+
* keys are not of type string. Returns the original value if it is not a Map.
5+
*
6+
* @param _ Unused.
7+
* @param value The value from the object to be stringified.
8+
* @returns The converted object or the original value if it is not a Map
9+
* or if the Map's keys are not of type string.
10+
*/
11+
export const typesHandler = (_: unknown, value: unknown): unknown => {
12+
if (value instanceof Map) {
13+
const obj: Record<string, unknown> = {};
14+
const firstKey = value.keys().next().value;
15+
if (typeof firstKey === "string") {
16+
for (const [k, v] of value.entries()) {
17+
obj[k] = v;
18+
}
19+
}
20+
return obj;
21+
}
22+
23+
return value;
24+
};

packages/js/wasm/src/WasmWrapper.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
WrapError,
2020
WrapErrorCode,
2121
ErrorSource,
22+
typesHandler,
2223
} from "@polywrap/core-js";
2324
import { Result, ResultErr, ResultOk } from "@polywrap/result";
2425

@@ -141,7 +142,7 @@ export class WasmWrapper implements Wrapper {
141142
code: WrapErrorCode.WRAPPER_READ_FAIL,
142143
uri: options.uri.uri,
143144
method,
144-
args: JSON.stringify(WasmWrapper._decodeArgs(args), null, 2),
145+
args: JSON.stringify(WasmWrapper._decodeArgs(args), typesHandler, 2),
145146
});
146147
return ResultErr(error);
147148
}
@@ -174,7 +175,7 @@ export class WasmWrapper implements Wrapper {
174175
code: WrapErrorCode.WRAPPER_INVOKE_ABORTED,
175176
uri: options.uri.uri,
176177
method,
177-
args: JSON.stringify(WasmWrapper._decodeArgs(args), null, 2),
178+
args: JSON.stringify(WasmWrapper._decodeArgs(args), typesHandler, 2),
178179
source,
179180
innerError: prev,
180181
});
@@ -185,7 +186,7 @@ export class WasmWrapper implements Wrapper {
185186
code: WrapErrorCode.WRAPPER_INTERNAL_ERROR,
186187
uri: options.uri.uri,
187188
method,
188-
args: JSON.stringify(WasmWrapper._decodeArgs(args), null, 2),
189+
args: JSON.stringify(WasmWrapper._decodeArgs(args), typesHandler, 2),
189190
});
190191
};
191192

@@ -222,7 +223,7 @@ export class WasmWrapper implements Wrapper {
222223
code: WrapErrorCode.WRAPPER_INVOKE_FAIL,
223224
uri: options.uri.uri,
224225
method,
225-
args: JSON.stringify(WasmWrapper._decodeArgs(args), null, 2),
226+
args: JSON.stringify(WasmWrapper._decodeArgs(args), typesHandler, 2),
226227
});
227228
return ResultErr(error);
228229
}

0 commit comments

Comments
 (0)