-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83cf845
commit 5d5f320
Showing
5 changed files
with
124 additions
and
141 deletions.
There are no files selected for viewing
57 changes: 0 additions & 57 deletions
57
designer/client/src/components/graph/node-modal/aggregate/aggMapLikeParser.tsx
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
designer/client/src/components/graph/node-modal/aggregate/pareserHelpers.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { parse } from "ts-spel"; | ||
import { Ast } from "ts-spel/lib/lib/Ast"; | ||
|
||
function getAst(input: string) { | ||
try { | ||
return parse(input, true); | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
function printFragment(text: string, el: { start: number; end: number }): string { | ||
return text.slice(el.start, el.end).trim(); | ||
} | ||
|
||
function mapToList(input: string, ast?: Ast): string[] | null { | ||
if (ast?.type !== "CompoundExpression") return null; | ||
if (ast.expressionComponents[0].type !== "InlineList") return null; | ||
if (ast.expressionComponents[1].type !== "PropertyReference") return null; | ||
if (ast.expressionComponents[1].propertyName !== "toString") return null; | ||
return ast.expressionComponents[0].elements.map((el) => printFragment(input, el)); | ||
} | ||
|
||
function mapToObject(input: string, ast?: Ast): Record<string, string> | null { | ||
switch (ast?.type) { | ||
case "InlineList": | ||
return ast.elements.length < 1 ? {} : null; | ||
case "InlineMap": | ||
return Object.fromEntries(Object.entries(ast.elements).map(([key, el]) => [key, printFragment(input, el)])); | ||
case "CompoundExpression": | ||
if (ast.expressionComponents[0].type !== "VariableReference") return null; | ||
if (ast.expressionComponents[0].variableName !== "AGG") return null; | ||
if (ast.expressionComponents[1].type !== "MethodReference") return null; | ||
if (ast.expressionComponents[1].methodName !== "map") return null; | ||
return mapToObject(input, ast.expressionComponents[1].args[0]); | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
export function parseToList(input: string): string[] { | ||
return mapToList(input, getAst(input)); | ||
} | ||
|
||
export function parseToObject(input: string): Record<string, string> { | ||
return mapToObject(input, getAst(input)); | ||
} |
72 changes: 0 additions & 72 deletions
72
designer/client/src/components/graph/node-modal/aggregate/parser.test.ts
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
designer/client/src/components/graph/node-modal/aggregate/parserHelpers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { parseToList, parseToObject } from "./pareserHelpers"; | ||
|
||
describe("parseHelpers", () => { | ||
describe("parseToList", () => { | ||
it.each([ | ||
[`aaa`, null], | ||
[`"aaa"`, null], | ||
[`{`, null], | ||
[`{}`, null], | ||
[`{123}`, null], | ||
[`{}.xxxx`, null], | ||
[`{}.toString`, []], | ||
[`#COLLECTION.join({}, "|")`, null], | ||
[`#xxxx.yyyy({}, "|")`, null], | ||
[`{123}.toString`, ["123"]], | ||
[`{ 123 }.toString`, ["123"]], | ||
[`{123,456}.toString`, ["123", "456"]], | ||
[`{ 123, 456 }.toString`, ["123", "456"]], | ||
[`{ 123, aaa, 456 }.toString`, ["123", "aaa", "456"]], | ||
[`{ 123, aaa.bbb }.toString`, ["123", "aaa.bbb"]], | ||
[`{ 123, aaa_bbb }.toString`, ["123", "aaa_bbb"]], | ||
[`{ 123, aaa bbb, ccc }.toString`, ["123", "aaa", "bbb", "ccc"]], | ||
[`{ 123, "aaa bbb" }.toString`, ["123", `"aaa bbb"`]], | ||
[`{ 123, 'aaa bbb' }.toString`, ["123", `'aaa bbb'`]], | ||
[`{ 123, #aaa.bbb }.toString`, ["123", `#aaa.bbb`]], | ||
[`{ 123, 123.bbb }.toString`, ["123", `123.bbb`]], | ||
[`{ 123, 123.456.bbb }.toString`, ["123", `123.456.bbb`]], | ||
[`{ 123, "{ 123, 456 }" }.toString`, ["123", `"{ 123, 456 }"`]], | ||
[`{ 123, { 123, "456" } }.toString`, ["123", `{ 123, "456" }`]], | ||
[`{ 123, { aaa: 123, bbb: "456" } }.toString`, ["123", `{ aaa: 123, bbb: "456" }`]], | ||
[`{ 123, {123,456}.^[2 + 5].test(123) }.toString`, ["123", `{123,456}.^[2 + 5].test(123)`]], | ||
[`#COLLECTION.join({ 123, 456 }, "|")`, null], | ||
])("should parse: %s to: %s", (input, output) => { | ||
expect(parseToList(input)).toEqual(output); | ||
}); | ||
}); | ||
|
||
describe("parseToObject", () => { | ||
it.each([ | ||
[`aaa`, null], | ||
[`"aaa"`, null], | ||
[`{}`, {}], | ||
[`#AGG.map({})`, {}], | ||
[`#AGG.map({:})`, {}], | ||
[`#AGG2.map({})`, null], | ||
[`{aaa:123}`, { aaa: "123" }], | ||
[`#AGG.map({aaa:123})`, { aaa: "123" }], | ||
[`#AGG.map({ aaa:123, bbb:456 })`, { aaa: "123", bbb: "456" }], | ||
[`{ aaa: 123, bbb: "456" }`, { aaa: "123", bbb: `"456"` }], | ||
[`{ a: 123, b c: 123 }`, { a: "123" }], | ||
[`{ aaa: 1 2 3 }`, { aaa: "1" }], | ||
[`{ "a a a": 123 }`, { "a a a": "123" }], | ||
[`{ aaa: #aaa.bbb }`, { aaa: "#aaa.bbb" }], | ||
[`{ aaa: aaa.bbb }`, { aaa: "aaa.bbb" }], | ||
[`{ aaa: "aaa bbb" }`, { aaa: `"aaa bbb"` }], | ||
[`{ aaa: {} }`, { aaa: `{}` }], | ||
[`{ aaa: { bbb: 123, ccc: "456" } }`, { aaa: `{ bbb: 123, ccc: "456" }` }], | ||
[`{ aaa: { 123, "456" } }`, { aaa: `{ 123, "456" }` }], | ||
[`{ aaa: "{ 123, '456' }" }`, { aaa: `"{ 123, '456' }"` }], | ||
[`{ aaa: '{ 123, "456" }' }`, { aaa: `'{ 123, "456" }'` }], | ||
[`{ aaa: '{ 123, "456" }' }`, { aaa: `'{ 123, "456" }'` }], | ||
[`{ aaa:\n { 123,\n "456" } }`, { aaa: `{ 123,\n "456" }` }], | ||
[`{ aaa: {123,456}.![2 + 5] }`, { aaa: "{123,456}.![2 + 5]" }], | ||
[`{ aaa: {123,456}.![2 + 5], bbb: 123 }`, { aaa: `{123,456}.![2 + 5]`, bbb: `123` }], | ||
[`{ aaa: {123,456}.?[2 + 5] }`, { aaa: `{123,456}.?[2 + 5]` }], | ||
[`{ aaa: {123,456}.^[2 + 5] }`, { aaa: `{123,456}.^[2 + 5]` }], | ||
[`{ aaa: {123,456}.^[2 + 5].test(123) }`, { aaa: `{123,456}.^[2 + 5].test(123)` }], | ||
[`{ aaa: 123 + 456 }`, { aaa: `123 + 456` }], | ||
])("should parse: %s to: %s", (input, output) => { | ||
expect(parseToObject(input)).toEqual(output); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters