Skip to content

Commit

Permalink
Merge pull request #5299 from systeminit/victor/generate-asset-funcs
Browse files Browse the repository at this point in the history
Generate subprops on asset func
  • Loading branch information
sprutton1 authored Jan 22, 2025
2 parents fb95d12 + 2a271a4 commit 51ed55f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 34 deletions.
99 changes: 66 additions & 33 deletions bin/si-gen-cloud-control/src/pipeline-steps/generateAssetFuncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
import { FuncSpec } from "../../../../lib/si-pkg/bindings/FuncSpec.ts";
import { SchemaVariantSpec } from "../bindings/SchemaVariantSpec.ts";
import _ from "lodash";
import { PropSpec } from "../bindings/PropSpec.ts";
import { strippedBase64 } from "../spec/siFuncs.ts";

export function generateAssetFuncs(specs: PkgSpec[]): PkgSpec[] {
const newSpecs = [] as PkgSpec[];
Expand All @@ -29,9 +31,7 @@ export function generateAssetFuncs(specs: PkgSpec[]): PkgSpec[] {
displayName: null,
description: null,
handler: "main",
codeBase64: btoa(
assetFuncCode,
).replace(/=/g, ""),
codeBase64: strippedBase64(assetFuncCode),
backendKind: "jsSchemaVariantDefinition",
responseType: "schemaVariantDefinition",
hidden: false,
Expand All @@ -57,40 +57,73 @@ export function generateAssetFuncs(specs: PkgSpec[]): PkgSpec[] {
function generateAssetCodeFromVariantSpec(variant: SchemaVariantSpec): string {
if (variant.domain.kind !== "object") throw "Domain prop is not object";

const queue = _.cloneDeep(variant.domain.entries);

let propDeclarations = "";
// Code for Props
let propDeclarations = `${indent(1)}// Props\n`;
let propAdds = "";

while (queue.length > 0) {
const prop = queue.pop();
for (const prop of variant.domain.entries) {
const varName = `${prop.name}Prop`;
switch (prop.kind) {
case "array":
break;
case "map":
break;
case "object":
break;
case "boolean":
case "json":
case "number":
case "string":
propDeclarations += `
const ${varName} = new PropBuilder()
.setKind("${prop.kind}")
.setName("${prop.name}")
.build();
`;

propAdds += `
.addProp(${varName})`;
break;
}
propDeclarations += `${indent(1)}const ${varName} = ${
generatePropBuilderString(prop, 2)
};\n`;
propAdds += `${indent(2)}.addProp(${varName})\n`;
}

return `function main() {${propDeclarations}
return new AssetBuilder()${propAdds}
.build();
// TODO add code for sockets

return `function main() {\n${propDeclarations}
return new AssetBuilder()\n${propAdds}${indent(2)}.build();
}`;
}

function generatePropBuilderString(
prop: PropSpec,
indent_level: number,
): string {
switch (prop.kind) {
case "array":
case "map": {
const entryBlock = `${indent(indent_level)}.setEntry(\n` +
`${indent(indent_level + 1)}${
generatePropBuilderString(prop.typeProp, indent_level + 1)
}\n` +
`${indent(indent_level)})\n`;

return `new PropBuilder()\n` +
`${indent(indent_level)}.setKind("${prop.kind}")\n` +
`${indent(indent_level)}.setName("${prop.name}")\n` +
`${entryBlock}` +
`${indent(indent_level)}.build()`;
}
case "object": {
const children = prop.entries.map((p) =>
generatePropBuilderString(p, indent_level + 1)
);

let addChildBlock = "";

for (const child of children) {
addChildBlock += `${indent(indent_level)}.addChild(\n` +
`${indent(indent_level + 1)}${child}\n` +
`${indent(indent_level)})\n`;
}

return `new PropBuilder()\n` +
`${indent(indent_level)}.setKind("object")\n` +
`${indent(indent_level)}.setName("${prop.name}")\n` +
`${addChildBlock}` +
`${indent(indent_level)}.build()`;
}
case "number":
return `new PropBuilder().setName("${prop.name}").setKind("integer").build()`;
case "boolean":
case "json":
case "string":
return `new PropBuilder().setName("${prop.name}").setKind("${prop.kind}").build()`;
}
}

function indent(count: number) {
const spaces = count * 4;
return " ".repeat(spaces);
}
7 changes: 7 additions & 0 deletions bin/si-gen-cloud-control/src/spec/siFuncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,10 @@ export function createSiFunc(name: string): FuncSpec {
export function getSiFuncId(kind: string): string {
return funcSpecs[kind].id;
}

// Si uses a version of base64 that removes the padding at the end for some reason
export function strippedBase64(code: string) {
return btoa(
code,
).replace(/=/g, "");
}
2 changes: 1 addition & 1 deletion bin/si-gen-cloud-control/src/specPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function pkgSpecFromCf(src: CfSchema): PkgSpec {
version,
description: src.description,
createdAt: new Date().toISOString(),
createdBy: "Cagador", // TODO Figure out a better name
createdBy: "Clover", // TODO this is still subject to change
defaultChangeSet: null,
workspacePk: null,
workspaceName: null,
Expand Down

0 comments on commit 51ed55f

Please sign in to comment.