Skip to content

Commit

Permalink
(fix, cli): literal examples are generated correctly in the IR (#3907)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Jun 22, 2024
1 parent 38c9cee commit 76c94b9
Show file tree
Hide file tree
Showing 122 changed files with 1,795 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,18 @@ export class GeneratedTypeReferenceExampleImpl implements GeneratedTypeReference
throw new Error("Unknown primitive example: " + primitiveExample.type);
}
}),
container: () => {
container: (containerExample) => {
switch (containerExample.type) {
case "literal":
return this.getExampleAsPropertyName({
example: {
shape: ExampleTypeReferenceShape.primitive(containerExample.literal),
jsonExample: example.jsonExample
},
context,
opts
});
}
throw new Error("Cannot convert container to property name");
},
named: ({ shape: example, typeName }) => {
Expand Down
5 changes: 5 additions & 0 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.25.2] - 2024-06-20

- Fix: The generator now removes `fs`, `path`, and `os` depdencencies from the browser
runtime.

## [0.25.1] - 2024-06-20

- Fix: The generator now removes `fs`, `path`, and `os` depdencencies from the browser
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.25.1
0.25.2
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ export class GeneratedRequestWrapperExampleImpl implements GeneratedRequestWrapp
}
}
const headerProperties = [...this.example.serviceHeaders, ...this.example.endpointHeaders]
.filter((header) => this.isLiteral(header.value.shape))
.filter((header) => this.isNotLiteral(header.value.shape))
.map((header) => {
return ts.factory.createPropertyAssignment(
asObjectProperty(generatedType.getPropertyNameOfNonLiteralHeaderFromName(header.name).propertyName),
context.type.getGeneratedExample(header.value).build(context, opts)
);
});
const queryParamProperties = [...this.example.queryParameters]
.filter((queryParam) => this.isLiteral(queryParam.value.shape))
.filter((queryParam) => this.isNotLiteral(queryParam.value.shape))
.map((queryParam) => {
return ts.factory.createPropertyAssignment(
asObjectProperty(
Expand All @@ -110,7 +110,7 @@ export class GeneratedRequestWrapperExampleImpl implements GeneratedRequestWrapp
this.example.request?._visit<ts.PropertyAssignment[]>({
inlinedRequestBody: (body) => {
return body.properties
.filter((property) => this.isLiteral(property.value.shape))
.filter((property) => this.isNotLiteral(property.value.shape))
.map((property) => {
if (property.originalTypeDeclaration != null) {
const originalTypeForProperty = context.type.getGeneratedType(
Expand Down Expand Up @@ -167,7 +167,7 @@ export class GeneratedRequestWrapperExampleImpl implements GeneratedRequestWrapp
);
}

private isLiteral(shape: ExampleTypeReferenceShape): boolean {
private isNotLiteral(shape: ExampleTypeReferenceShape): boolean {
return !(shape.type === "container" && shape.container.type === "literal");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -842,4 +842,4 @@ exports[`dependencies correctly incorporates dependencies 1`] = `
}"
`;

exports[`dependencies file dependencies 1`] = `2802229`;
exports[`dependencies file dependencies 1`] = `2804645`;
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,16 @@ export function convertTypeReferenceExample({
literal: (literal) => {
switch (literal.type) {
case "boolean":
return ExampleTypeReferenceShape.primitive(ExamplePrimitive.boolean(literal.boolean));
return ExampleTypeReferenceShape.container(
ExampleContainer.literal(ExamplePrimitive.boolean(literal.boolean))
);
case "string":
return ExampleTypeReferenceShape.primitive(
ExamplePrimitive.string({
original: literal.string
})
return ExampleTypeReferenceShape.container(
ExampleContainer.literal(
ExamplePrimitive.string({
original: literal.string
})
)
);
default:
assertNever(literal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,16 @@ exports[`migrateFromV47ToV46 snapshot 1`] = `
"value": {
"jsonExample": "goose",
"shape": {
"primitive": {
"string": {
"original": "goose",
"container": {
"literal": {
"string": {
"original": "goose",
},
"type": "string",
},
"type": "string",
"type": "literal",
},
"type": "primitive",
"type": "container",
},
},
},
Expand Down Expand Up @@ -756,13 +759,16 @@ exports[`migrateFromV47ToV46 snapshot 1`] = `
"value": {
"jsonExample": "goose",
"shape": {
"primitive": {
"string": {
"original": "goose",
"container": {
"literal": {
"string": {
"original": "goose",
},
"type": "string",
},
"type": "string",
"type": "literal",
},
"type": "primitive",
"type": "container",
},
},
},
Expand Down Expand Up @@ -928,7 +934,7 @@ exports[`migrateFromV47ToV46 snapshot 1`] = `
"endpointHeaders": [],
"endpointPathParameters": [],
"exampleType": "generated",
"id": "57602a4bf907e3ac838da409789827c823858b7c",
"id": "1507bb3bff076877f695465398d74db8e464f689",
"name": null,
"queryParameters": [
{
Expand Down Expand Up @@ -1289,13 +1295,16 @@ exports[`migrateFromV47ToV46 snapshot 1`] = `
"value": {
"jsonExample": "goose",
"shape": {
"primitive": {
"string": {
"original": "goose",
"container": {
"literal": {
"string": {
"original": "goose",
},
"type": "string",
},
"type": "string",
"type": "literal",
},
"type": "primitive",
"type": "container",
},
},
},
Expand Down Expand Up @@ -1570,13 +1579,16 @@ exports[`migrateFromV47ToV46 snapshot 1`] = `
"value": {
"jsonExample": "goose",
"shape": {
"primitive": {
"string": {
"original": "goose",
"container": {
"literal": {
"string": {
"original": "goose",
},
"type": "string",
},
"type": "string",
"type": "literal",
},
"type": "primitive",
"type": "container",
},
},
},
Expand Down
51 changes: 46 additions & 5 deletions packages/seed/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ import { ScriptRunner } from "./commands/test/ScriptRunner";
import { TaskContextFactory } from "./commands/test/TaskContextFactory";
import { DockerTestRunner, LocalTestRunner } from "./commands/test/test-runner";
import { FIXTURES, testGenerator } from "./commands/test/testWorkspaceFixtures";
import { loadGeneratorWorkspaces } from "./loadGeneratorWorkspaces";
import { GeneratorWorkspace, loadGeneratorWorkspaces } from "./loadGeneratorWorkspaces";
import { Semaphore } from "./Semaphore";

void tryRunCli();

export async function tryRunCli(): Promise<void> {
const cli: Argv = yargs(hideBin(process.argv));
const cli: Argv = yargs(hideBin(process.argv))
.strict()
.fail((message, error: unknown, argv) => {
// if error is null, it's a yargs validation error
if (error == null) {
argv.showHelp();
// eslint-disable-next-line
console.error(message);
}
});

addTestCommand(cli);
addRunCommand(cli);
Expand Down Expand Up @@ -76,6 +85,9 @@ function addTestCommand(cli: Argv) {
}),
async (argv) => {
const generators = await loadGeneratorWorkspaces();
if (argv.generator != null) {
throwIfGeneratorDoesNotExist({ seedWorkspaces: generators, generators: argv.generator });
}

const taskContextFactory = new TaskContextFactory(argv["log-level"]);
const lock = new Semaphore(argv.parallel);
Expand All @@ -87,14 +99,14 @@ function addTestCommand(cli: Argv) {
continue;
}
let testRunner;
const scriptRunner = new ScriptRunner(generator);
const scriptRunner = new ScriptRunner(generator, argv.skipScripts);
if (argv.local && generator.workspaceConfig.local != null) {
testRunner = new LocalTestRunner({
generator,
lock,
taskContextFactory,
skipScripts: argv.skipScripts,
scriptRunner: new ScriptRunner(generator),
scriptRunner: scriptRunner,
keepDocker: false // dummy
});
} else {
Expand All @@ -104,7 +116,7 @@ function addTestCommand(cli: Argv) {
taskContextFactory,
skipScripts: argv.skipScripts,
keepDocker: argv.keepDocker,
scriptRunner: new ScriptRunner(generator)
scriptRunner: scriptRunner
});
scriptRunners.push(scriptRunner);
CONSOLE_LOGGER.info(`${generator.workspaceName} does not support local mode. Running in docker.`);
Expand Down Expand Up @@ -161,6 +173,8 @@ function addRunCommand(cli: Argv) {
}),
async (argv) => {
const generators = await loadGeneratorWorkspaces();
throwIfGeneratorDoesNotExist({ seedWorkspaces: generators, generators: [argv.generator] });

const generator = generators.find((g) => g.workspaceName === argv.generator);
if (generator == null) {
throw new Error(
Expand All @@ -179,3 +193,30 @@ function addRunCommand(cli: Argv) {
}
);
}

function throwIfGeneratorDoesNotExist({
seedWorkspaces,
generators
}: {
seedWorkspaces: GeneratorWorkspace[];
generators: string[];
}) {
const generatorNames = new Set(
seedWorkspaces.map((gen) => {
return gen.workspaceName;
})
);
const missingGenerators = [];
for (const generator of generators) {
if (!generatorNames.has(generator)) {
missingGenerators.push(generator);
}
}
if (missingGenerators.length > 0) {
throw new Error(
`Generators ${missingGenerators.join(
", "
)} not found. Please make sure that there is a folder with those names in the seed directory.`
);
}
}
2 changes: 1 addition & 1 deletion packages/seed/src/commands/run/runWithCustomFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function runWithCustomFixture({
taskContextFactory,
skipScripts: true,
keepDocker: true,
scriptRunner: new ScriptRunner(workspace)
scriptRunner: new ScriptRunner(workspace, false)
});

const fernWorkspace = await convertGeneratorWorkspaceToFernWorkspace({
Expand Down
8 changes: 5 additions & 3 deletions packages/seed/src/commands/test/ScriptRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ interface RunningScriptConfig extends ScriptConfig {
* Runs scripts on the generated code to verify the output.
*/
export class ScriptRunner {
private startContainersFn: Promise<void>;
private startContainersFn: Promise<void> | undefined;
private scripts: RunningScriptConfig[] = [];
private lock = new Semaphore(1);

constructor(private readonly workspace: GeneratorWorkspace) {
this.startContainersFn = this.startContainers();
constructor(private readonly workspace: GeneratorWorkspace, private readonly skipScripts: boolean) {
if (!skipScripts) {
this.startContainersFn = this.startContainers();
}
}

public async run({ taskContext, id, outputDir }: ScriptRunner.RunArgs): Promise<ScriptRunner.RunResponse> {
Expand Down
14 changes: 13 additions & 1 deletion seed/csharp-model/streaming/.mock/definition/dummy.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion seed/csharp-sdk/streaming/.mock/definition/dummy.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 76c94b9

Please sign in to comment.