Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle map in workflows #1438

Merged
merged 4 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/cli/src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
defaultWorkflowManifest,
parseManifestFileOption,
parseLogFileOption,
typesHandler,
} from "../lib";
import { createLogger } from "./utils/createLogger";

Expand Down Expand Up @@ -143,7 +144,10 @@ const _run = async (options: WorkflowCommandOptions) => {
fs.writeFileSync(outputFile, yaml.stringify(printableOutput, null, 2));
break;
case "json":
fs.writeFileSync(outputFile, JSON.stringify(printableOutput, null, 2));
fs.writeFileSync(
outputFile,
JSON.stringify(printableOutput, typesHandler, 2)
);
break;
default:
throw new Error(
Expand Down
30 changes: 23 additions & 7 deletions packages/cli/src/lib/helpers/workflow-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ export function cueExists(logger: Logger): boolean {
return stdout ? stdout.startsWith("cue version ") : false;
}

export const typesHandler = (_: unknown, value: unknown): unknown => {
if (value instanceof Map) {
return Array.from(value).reduce(
(obj: Record<string, unknown>, [key, value]) => {
obj[key] = value;
return obj;
},
{}
);
}

return value;
};

export function validateOutput(
output: WorkflowOutput,
validateScriptPath: string,
Expand All @@ -30,20 +44,22 @@ export function validateOutput(
const stepId = id.substring(index + 1);

const selector = `${jobId}.\\$${stepId}`;
const jsonOutput = `${TMPDIR}/${id}.json`;
const tempOutputPath = `${TMPDIR}/${id}.json`;

fs.writeFileSync(
jsonOutput,
JSON.stringify({ data, error: error?.message }, null, 2)
const outputData = JSON.stringify(
{ data, error: error?.message },
typesHandler,
2
);
fs.writeFileSync(tempOutputPath, outputData);

const { stderr } = runCommandSync(
`cue vet -d ${selector} ${validateScriptPath} ${jsonOutput}`,
`cue vet -d ${selector} ${validateScriptPath} ${tempOutputPath}`,
Fixed Show fixed Hide fixed
logger
);

if (fs.existsSync(jsonOutput)) {
fs.unlinkSync(jsonOutput);
if (fs.existsSync(tempOutputPath)) {
fs.unlinkSync(tempOutputPath);
}

if (!stderr) {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/lib/workflow/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { WorkflowOutput } from "./types";
import path from "path";
import fs from "fs";
import { WorkflowJobs } from "@polywrap/polywrap-manifest-types-js";
import { typesHandler } from "../helpers";

export const validateJobNames = (
jobs: WorkflowJobs | undefined,
Expand Down Expand Up @@ -44,7 +45,7 @@ export function printJobOutput(output: WorkflowOutput): void {
console.log(`Job status: ${output.status}`);

if (output.data !== undefined) {
console.log(`Data: ${JSON.stringify(output.data, null, 2)}`);
console.log(`Data: ${JSON.stringify(output.data, typesHandler, 2)}`);
}

if (output.error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ jobs:
args:
x: "$cases.0.data"
y: "$cases.0.data"
case3:
steps:
- uri: fs/../run-test-wrapper/build
method: returnMap
args:
map:
nested_map:
Hello: 1
Heyo: 50
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Args_add,
Args_addInBox,
Args_addFromEnv,
Args_returnMap,
Num,
Env
} from "./wrap";
Expand All @@ -16,4 +17,8 @@ export function addInBox(args: Args_addInBox): Num {

export function addFromEnv(args: Args_addFromEnv, env: Env): i32 {
return args.x + env.value;
}
}

export function returnMap(args: Args_returnMap): Map<string, Map<string, i32>> {
return args.map;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type Module {
addFromEnv(
x: Int32!
): Int32! @env(required: true)

returnMap(
map: Map! @annotate(type: "Map<String!, Map<String!, Int!>!>!")
): Map! @annotate(type: "Map<String!, Map<String!, Int!>!>!")
}

type Num {
Expand Down