Skip to content

Commit 68862c1

Browse files
committed
fix: node service eval value
1 parent 09dd6b5 commit 68862c1

File tree

8 files changed

+119
-62
lines changed

8 files changed

+119
-62
lines changed

server/node-service/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ TODO
4949
package-lock.json
5050

5151
op.mjs
52+
.vscode

server/node-service/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dev": "nodemon --files src/server.ts",
1111
"start": "node ./build/src/server.js",
1212
"test": "jest",
13-
"build": "rm -rf build/ && tsc && cp -r src/static build/src/static"
13+
"build": "rm -rf build/ && yarn test && tsc && cp -r src/static build/src/static"
1414
},
1515
"devDependencies": {
1616
"@types/jest": "^29.2.4",
@@ -44,7 +44,7 @@
4444
"loglevel": "^1.8.1",
4545
"morgan": "^1.10.0",
4646
"openapi-types": "^12.1.0",
47-
"openblocks-core": "^0.0.5",
47+
"openblocks-core": "^0.0.7",
4848
"openblocks-sdk": "^0.0.35",
4949
"stylis": "^4.1.3",
5050
"swagger-client": "^3.18.5",

server/node-service/src/common/util.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { relaxedJSONToJSON } from "openblocks-core";
21
import yaml from "yaml";
32
import fs from "fs";
43

@@ -42,35 +41,6 @@ export function toBoolean(value: any): boolean {
4241
return !!value;
4342
}
4443

45-
export function toJsonValue(value: any): any {
46-
if (typeof value !== "string") {
47-
return value;
48-
}
49-
try {
50-
const json = relaxedJSONToJSON(value, true);
51-
return JSON.parse(json);
52-
} catch (e) {
53-
console.info("invalid json input:", value);
54-
return {};
55-
}
56-
}
57-
58-
export function toStringOrJson(value: any): any {
59-
if (typeof value !== "string") {
60-
return value;
61-
}
62-
try {
63-
const json = relaxedJSONToJSON(value, true);
64-
return JSON.parse(json);
65-
} catch (e) {
66-
if (typeof value === "string") {
67-
return value;
68-
}
69-
console.info("invalid json input:", value);
70-
return {};
71-
}
72-
}
73-
7444
export function readYaml<T = any>(path: string): T {
7545
try {
7646
const yamlContent = fs.readFileSync(path, "utf-8");

server/node-service/src/controllers/plugins.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export async function listPlugins(req: Request, res: Response) {
1111
}
1212
const ctx = pluginServices.getPluginContext(req);
1313
const result = pluginServices.listPlugins(ctx, ids as string[]);
14+
console.info("plugins: ", result);
1415
return res.status(200).json(result);
1516
}
1617

server/node-service/src/plugins/firebase/run.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { runFirebasePlugin } from "./run";
22

33
const privateKey = process.env["GOOGLE_PRIVATE_KEY"] || "";
44

5-
test("realtime database", async () => {
5+
test.skip("realtime database", async () => {
66
const res = await runFirebasePlugin(
77
{ actionName: "RTDB.QueryDatabase", databaseRef: "/hello" },
88
{

server/node-service/src/services/plugin.test.ts

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,87 @@ test("eval code to value", async () => {
6565
value: 0,
6666
},
6767
]);
68-
expect(a).toBe("0");
68+
expect(a).toBe(0);
6969

7070
let b = evalCodeToValue("{{a}}", [
7171
{
7272
key: "a",
7373
value: { name: "Tom" },
7474
},
7575
]);
76-
expect(b).toBe(JSON.stringify({ name: "Tom" }));
76+
expect(b).toEqual({ name: "Tom" });
77+
78+
let c = evalCodeToValue("Hello: {{a}}", [
79+
{
80+
key: "a",
81+
value: { name: "Tom" },
82+
},
83+
]);
84+
expect(c).toBe("Hello: " + String({ name: "Tom" }));
7785
});
7886

7987
test("eval to value", async () => {
8088
let value;
8189

90+
// boolean input
8291
value = await evalToValue({ key: "a", type: "booleanInput" }, "1", [], {});
8392
expect(value).toBe(true);
84-
8593
value = await evalToValue(
8694
{ key: "a", type: "booleanInput" },
8795
"{{a}}",
8896
[{ key: "a", value: 1 }],
8997
{}
9098
);
9199
expect(value).toBe(true);
100+
value = await evalToValue(
101+
{ key: "a", type: "booleanInput" },
102+
"{{a}}",
103+
[{ key: "a", value: 0 }],
104+
{}
105+
);
106+
expect(value).toBe(false);
107+
value = await evalToValue({ key: "a", type: "booleanInput" }, "{{a}}", [], {});
108+
expect(value).toBe(false);
109+
110+
// test input
111+
value = await evalToValue(
112+
{ key: "a", type: "textInput" },
113+
"{{hello}}: {{name}}: {{data}}",
114+
[
115+
{ key: "hello", value: "world" },
116+
{ key: "name", value: "Tom" },
117+
{ key: "data", value: { n: 1 } },
118+
],
119+
{}
120+
);
121+
expect(value).toBe(`world: Tom: ${String({ n: 1 })}`);
122+
value = await evalToValue({ key: "a", type: "textInput" }, "{{hello}}: {{name}}", [], {});
123+
expect(value).toBe(": ");
124+
value = await evalToValue(
125+
{ key: "a", type: "textInput" },
126+
"{{hello}}",
127+
[{ key: "hello", value: { n: 1 } }],
128+
{}
129+
);
130+
expect(value).toBe(JSON.stringify({ n: 1 }));
92131

132+
// checkbox
93133
value = await evalToValue({ key: "a", type: "checkbox" }, "any-value", [], {});
94134
expect(value).toBe(true);
135+
value = await evalToValue({ key: "a", type: "checkbox" }, "", [], {});
136+
expect(value).toBe(false);
137+
value = await evalToValue({ key: "a", type: "checkbox" }, "{{a}}", [], {});
138+
expect(value).toBe(false);
139+
value = await evalToValue({ key: "a", type: "checkbox" }, "{{a}}", [{ key: "a", value: 1 }], {});
140+
expect(value).toBe(true);
141+
value = await evalToValue({ key: "a", type: "checkbox" }, "{{a}}", [{ key: "a", value: 0 }], {});
142+
expect(value).toBe(false);
95143

144+
// file
96145
value = await evalToValue({ key: "a", type: "file" }, "some-base64", [], {});
97146
expect(value).toBe("some-base64");
98-
99147
value = await evalToValue({ key: "a", type: "file" }, "{name: 1.png, type: image/png}", [], {});
100148
expect(value).toEqual({ name: "1.png", type: "image/png" });
101-
102149
value = await evalToValue(
103150
{ key: "a", type: "file" },
104151
"{name: 1.png, type: {{type}}, data: {{data}}}",
@@ -110,14 +157,40 @@ test("eval to value", async () => {
110157
);
111158
expect(value).toEqual({ name: "1.png", type: "image/png", data: "/** some \nvalue\n" });
112159

113-
value = await evalToValue({ key: "a", type: "jsonInput" }, "{hello}", [], {});
160+
// json
161+
value = await evalToValue({ key: "a", type: "jsonInput" }, "some-string", [], {});
162+
expect(value).toEqual("some-string");
163+
value = await evalToValue({ key: "a", type: "jsonInput" }, "{}", [], {});
164+
expect(value).toEqual({});
165+
value = await evalToValue(
166+
{ key: "a", type: "jsonInput" },
167+
"{a: {{b}}}",
168+
[{ key: "b", value: "1" }],
169+
{}
170+
);
171+
expect(value).toEqual({ a: "1" });
172+
value = await evalToValue(
173+
{ key: "a", type: "jsonInput" },
174+
"{a: {{b}}}",
175+
[{ key: "b", value: 1 }],
176+
{}
177+
);
178+
expect(value).toEqual({ a: 1 });
179+
value = await evalToValue({ key: "a", type: "jsonInput" }, "{a: {{b}}}", [], {});
114180
expect(value).toEqual({});
115181

182+
// number
116183
value = await evalToValue({ key: "a", type: "numberInput" }, "invalid number", [], {});
117184
expect(value).toBe(0);
118-
119185
value = await evalToValue({ key: "a", type: "numberInput" }, "1", [], {});
120186
expect(value).toBe(1);
187+
value = await evalToValue(
188+
{ key: "a", type: "numberInput" },
189+
"{{a}}1",
190+
[{ key: "a", value: 1 }],
191+
{}
192+
);
193+
expect(value).toBe(11);
121194
});
122195

123196
test("list plugins", () => {

server/node-service/src/services/plugin.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from "lodash";
2-
import { toString, toNumber, toBoolean, toJsonValue, toStringOrJson } from "../common/util";
3-
import { getDynamicStringSegments, isDynamicSegment } from "openblocks-core";
2+
import { toString, toNumber, toBoolean } from "../common/util";
3+
import { getDynamicStringSegments, isDynamicSegment, RelaxedJsonParser } from "openblocks-core";
44
import jsonPath from "jsonpath";
55
import plugins from "../plugins";
66
import {
@@ -21,7 +21,21 @@ function isReadOnlyArray(obj: any): obj is readonly any[] {
2121

2222
type EvalContext = { key: string; value: any }[];
2323

24-
export function evalCodeToValue(dsl: string, context: EvalContext) {
24+
function evalDynamicSegment(segment: string, context: Record<string, any>) {
25+
if (!isDynamicSegment(segment)) {
26+
return segment;
27+
}
28+
const segmentRaw = segment.slice(2, -2);
29+
return context[segmentRaw];
30+
}
31+
32+
class CodeValueParser extends RelaxedJsonParser {
33+
evalDynamicSegment(segment: string): any {
34+
return evalDynamicSegment(segment, this.context);
35+
}
36+
}
37+
38+
export function evalCodeToValue(dsl: string, context: EvalContext, isJson: boolean = false) {
2539
if (!dsl) {
2640
return undefined;
2741
}
@@ -31,17 +45,17 @@ export function evalCodeToValue(dsl: string, context: EvalContext) {
3145
}
3246

3347
const contextMap = Object.fromEntries(context.map((i) => [i.key, i.value]));
48+
if (isJson) {
49+
const parser = new CodeValueParser(dsl, contextMap);
50+
const value = parser.parse().value;
51+
return value;
52+
}
3453

35-
// copy from string2Fn Parser
3654
const values = getDynamicStringSegments(dsl).map((segment) => {
37-
if (!isDynamicSegment(segment)) {
38-
return segment;
39-
}
40-
const segmentRaw = segment.slice(2, -2);
41-
return JSON.stringify(contextMap[segmentRaw]);
55+
return evalDynamicSegment(segment, contextMap);
4256
});
4357

44-
return values.join("");
58+
return values.length === 1 ? values[0] : values.join("");
4559
}
4660

4761
export async function evalToValue<T extends Config>(
@@ -91,12 +105,8 @@ export async function evalToValue<T extends Config>(
91105
return toBoolean(evalCodeToValue(dsl, context));
92106
}
93107

94-
if (config.type === "jsonInput") {
95-
return toJsonValue(evalCodeToValue(dsl, context));
96-
}
97-
98-
if (config.type === "file") {
99-
return toStringOrJson(evalCodeToValue(dsl, context));
108+
if (config.type === "jsonInput" || config.type === "file") {
109+
return evalCodeToValue(dsl, context, true);
100110
}
101111

102112
throw new ServiceError(`invalid plugin definition, unknown config type: ${(config as any).type}`);
@@ -126,7 +136,9 @@ export function getPluginContext(req: Request): PluginContext {
126136

127137
async function getQueryConfig(plugin: DataSourcePlugin, dataSourceConfig: any = {}) {
128138
if (typeof plugin.queryConfig === "function") {
129-
return plugin.queryConfig(dataSourceConfig);
139+
const ret = await plugin.queryConfig(dataSourceConfig);
140+
console.info(ret);
141+
return ret;
130142
}
131143
return plugin.queryConfig;
132144
}

server/node-service/yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8373,16 +8373,16 @@ __metadata:
83738373
languageName: node
83748374
linkType: hard
83758375

8376-
"openblocks-core@npm:^0.0.5":
8377-
version: 0.0.5
8378-
resolution: "openblocks-core@npm:0.0.5::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Fopenblocks-core%2F-%2Fopenblocks-core-0.0.5.tgz"
8376+
"openblocks-core@npm:^0.0.7":
8377+
version: 0.0.7
8378+
resolution: "openblocks-core@npm:0.0.7"
83798379
dependencies:
83808380
"@rollup/plugin-commonjs": ^23.0.0
83818381
"@rollup/plugin-node-resolve": ^15.0.0
83828382
intl-messageformat: ^10.2.1
83838383
lodash: ^4.17.21
83848384
lru-cache: ^7.14.1
8385-
checksum: 1ef27a4e46f582b4941cf1f9c1e3eaff3d2505e3bf3c7d202119c6fd6f24e75584dede92378d1c89fa41bf22d73c21c8819971521e1a8f00498b038aecb88a19
8385+
checksum: 9469e04204faadb805a23666a4b5faa36c9d8a8a13cb19c4b7b69abd0d1a1d746f69a99ec1f53affd9c0532c2b65f4c9b8e91c8baaeb4195f99ff1b6fcbc60e7
83868386
languageName: node
83878387
linkType: hard
83888388

@@ -9294,7 +9294,7 @@ __metadata:
92949294
nock: ^13.3.0
92959295
nodemon: ^2.0.20
92969296
openapi-types: ^12.1.0
9297-
openblocks-core: ^0.0.5
9297+
openblocks-core: ^0.0.7
92989298
openblocks-sdk: ^0.0.35
92999299
stylis: ^4.1.3
93009300
swagger-client: ^3.18.5

0 commit comments

Comments
 (0)