Skip to content

Commit 8896af3

Browse files
committed
Implement writer for JSON-formatted extensions
1 parent 6bd4ff7 commit 8896af3

2 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎extensions/ql-vscode/src/model-editor/extension-serialization.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ export function modelExtensionFileToString(
400400
case ExtensionFormat.Yaml:
401401
return modelExtensionFileToYaml(data, headerComment);
402402
case ExtensionFormat.Json:
403-
// TODO: implement JSON serialization
404-
return "<JSON placeholder>";
403+
return modelExtensionFileToJson(data, headerComment);
405404
default:
406405
assertNever(format);
407406
}
@@ -432,6 +431,36 @@ function modelExtensionFileToYaml(
432431
${extensions.join("\n")}`;
433432
}
434433

434+
function modelExtensionFileToJson(
435+
data: ModelExtensionFile,
436+
headerComment?: string,
437+
): string {
438+
const extensions = data.extensions
439+
.map((extension) => {
440+
const data =
441+
extension.data.length === 0
442+
? "[]"
443+
: `[\n${extension.data
444+
.map((row) => ` ${JSON.stringify(row)}`)
445+
.join(",\n")}\n ]`;
446+
447+
return `{
448+
"addsTo": {
449+
"pack": "${extension.addsTo.pack}",
450+
"extensible": "${extension.addsTo.extensible}"
451+
},
452+
"data": ${data}
453+
}`;
454+
})
455+
.filter((extensions) => extensions !== "");
456+
457+
return `${headerComment ? `// ${headerComment}\n\n` : ""}{
458+
"extensions": [
459+
${extensions.join(",\n ")}
460+
]
461+
}`;
462+
}
463+
435464
function deserializeToObject(contents: string, filename: string) {
436465
const format = getFormatFromFileName(filename);
437466
switch (format) {

‎extensions/ql-vscode/test/unit-tests/model-editor/extension-serialization.test.ts‎

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,40 @@ describe("createDataExtensionYamlsForApplicationMode", () => {
935935
data:
936936
- ["org.sql2o","Query","executeScalar","(Class)","summary","manual"]
937937
`,
938-
"models/spring-boot.model.json": "<JSON placeholder>",
938+
"models/spring-boot.model.json": `{
939+
"extensions": [
940+
{
941+
"addsTo": {
942+
"pack": "codeql/java-all",
943+
"extensible": "sourceModel"
944+
},
945+
"data": []
946+
},
947+
{
948+
"addsTo": {
949+
"pack": "codeql/java-all",
950+
"extensible": "sinkModel"
951+
},
952+
"data": []
953+
},
954+
{
955+
"addsTo": {
956+
"pack": "codeql/java-all",
957+
"extensible": "summaryModel"
958+
},
959+
"data": []
960+
},
961+
{
962+
"addsTo": {
963+
"pack": "codeql/java-all",
964+
"extensible": "neutralModel"
965+
},
966+
"data": [
967+
["org.springframework.boot","SpringApplication","run","(Class,String[])","summary","manual"]
968+
]
969+
}
970+
]
971+
}`,
939972
});
940973
});
941974
});
@@ -1722,9 +1755,21 @@ extensions:
17221755
`);
17231756
});
17241757
it("should return JSON", () => {
1725-
expect(
1726-
modelExtensionFileToString(modelExtensionFile, ExtensionFormat.Json),
1727-
).toBe("<JSON placeholder>");
1758+
expect(modelExtensionFileToString(modelExtensionFile, ExtensionFormat.Json))
1759+
.toBe(`{
1760+
"extensions": [
1761+
{
1762+
"addsTo": {
1763+
"pack": "acme/super-pack",
1764+
"extensible": "fooModel"
1765+
},
1766+
"data": [
1767+
["abc",123,true],
1768+
["def",124,false]
1769+
]
1770+
}
1771+
]
1772+
}`);
17281773
});
17291774
it("should return JSON with a header comment", () => {
17301775
expect(
@@ -1733,6 +1778,21 @@ extensions:
17331778
ExtensionFormat.Json,
17341779
"Test header comment",
17351780
),
1736-
).toBe("<JSON placeholder>");
1781+
).toBe(`// Test header comment
1782+
1783+
{
1784+
"extensions": [
1785+
{
1786+
"addsTo": {
1787+
"pack": "acme/super-pack",
1788+
"extensible": "fooModel"
1789+
},
1790+
"data": [
1791+
["abc",123,true],
1792+
["def",124,false]
1793+
]
1794+
}
1795+
]
1796+
}`);
17371797
});
17381798
});

0 commit comments

Comments
 (0)