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

Try work with named unions #1765

Merged
merged 6 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 26 additions & 26 deletions common/config/rush/pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions packages/cadl-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
"ts-node": "^10.7.0",
"typescript": "~4.8.0",
"prettier": "~2.7.1",
"@azure-tools/cadl-ranch-specs": "~0.7.0",
"@azure-tools/cadl-ranch-specs": "~0.8.0",
"@cadl-lang/versioning": "~0.40.0",
"@azure-tools/cadl-ranch-expect": "^0.1.16",
"@azure-tools/cadl-ranch": "~0.3.0",
"@azure-tools/cadl-ranch": "~0.3.1",
"chalk": "^4.0.0",
"@azure-rest/core-client": "^1.1.0",
"@azure/core-auth": "^1.3.2",
Expand Down
50 changes: 39 additions & 11 deletions packages/cadl-typescript/src/modelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
Type,
Union,
isNullType,
Scalar
Scalar,
UnionVariant
} from "@cadl-lang/compiler";
import { reportDiagnostic } from "./lib.js";
import {
Expand Down Expand Up @@ -85,6 +86,8 @@ export function getSchemaForType(
return schema;
} else if (type.kind === "Union") {
return getSchemaForUnion(program, type, usage);
} else if (type.kind === "UnionVariant") {
return getSchemaForUnionVariant(program, type, usage);
} else if (type.kind === "Enum") {
return getSchemaForEnum(program, type);
} else if (type.kind === "Scalar") {
Expand Down Expand Up @@ -146,26 +149,39 @@ function getSchemaForUnion(
union: Union,
usage?: SchemaContext[]
) {
const nonNullOptions = union.options.filter((t) => !isNullType(t));
const nullable = union.options.length != nonNullOptions.length;
const variants = Array.from(union.variants.values());
const nonNullOptions = variants.filter((t) => !isNullType(t.type));
const nullable = variants.length !== nonNullOptions.length;
if (nonNullOptions.length === 0 || nonNullOptions[0] === undefined) {
reportDiagnostic(program, { code: "union-null", target: union });
return {};
}

const values = [];

for (const option of nonNullOptions) {
for (const variant of nonNullOptions) {
// We already know it's not a model type
values.push(getSchemaForType(program, option, usage));
values.push(getSchemaForType(program, variant.type, usage));
}

const schema: any = {};
if (values.length > 0) {
schema.enum = values;
schema.type = values
.map((item) => `${getTypeName(item) ?? item}`)
const unionAlias = values
.map((item) => `${getTypeName(item, [SchemaContext.Input]) ?? item}`)
.join(" | ");
const outputUnionAlias = values
.map((item) => `${getTypeName(item, [SchemaContext.Output]) ?? item}`)
.join(" | ");
schema.type = !union.expression && union.name ? union.name : unionAlias;
if (!union.expression) {
schema.name = union.name;
schema.type = "object";
schema.typeName = union.name;
schema.outputTypeName = union.name + "Output";
schema.alias = unionAlias;
schema.outputAlias = outputUnionAlias;
}
}
if (nullable) {
schema["required"] = false;
Expand All @@ -174,6 +190,14 @@ function getSchemaForUnion(
return schema;
}

function getSchemaForUnionVariant(
program: Program,
variant: UnionVariant,
usage?: SchemaContext[]
): Schema {
return getSchemaForType(program, variant, usage);
}

// An openapi "string" can be defined in several different ways in Cadl
function isOasString(type: Type): boolean {
if (type.kind === "String") {
Expand Down Expand Up @@ -835,9 +859,9 @@ function getSchemaForStdScalar(program: Program, cadlType: Scalar) {
}
}

export function getTypeName(schema: Schema): string {
export function getTypeName(schema: Schema, usage?: SchemaContext[]): string {
// TODO: Handle more cases
return getPriorityName(schema) ?? schema.type ?? "any";
return getPriorityName(schema, usage) ?? schema.type ?? "any";
}

export function getImportedModelName(schema: Schema): string[] | undefined {
Expand All @@ -857,8 +881,12 @@ export function getImportedModelName(schema: Schema): string[] | undefined {
}
}

function getPriorityName(schema: Schema): string {
return schema.outputTypeName ?? schema.typeName ?? schema.name;
function getPriorityName(schema: Schema, usage?: SchemaContext[]): string {
return usage &&
usage.includes(SchemaContext.Input) &&
!usage.includes(SchemaContext.Output)
? schema.typeName ?? schema.name
: schema.outputTypeName ?? schema.typeName ?? schema.name;
}
function getDictionaryValueName(schema: DictionarySchema): string | undefined {
return schema.outputValueTypeName ?? schema.valueTypeName ?? undefined;
Expand Down
24 changes: 24 additions & 0 deletions packages/cadl-typescript/src/transform/transformSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ export function transformSchemas(
}
getGeneratedModels(prop[1].type, context);
}
if (
prop[1].type.kind === "Union" &&
(!program.stateMap(modelKey).get(prop[1].type) ||
!program.stateMap(modelKey).get(prop[1].type)?.includes(context))
) {
const variants = Array.from(prop[1].type.variants.values());
let hasModels = false;
for (const variant of variants) {
if (
variant.type.kind === "Model" &&
(!program.stateMap(modelKey).get(variant.type) ||
!program
.stateMap(modelKey)
.get(variant.type)
?.includes(context))
) {
hasModels = true;
getGeneratedModels(variant.type, context);
}
}
if (hasModels) {
setModelMap(prop[1].type, context);
}
}
}
const baseModel = model.baseModel;
if (
Expand Down
4 changes: 4 additions & 0 deletions packages/cadl-typescript/test/commands/cadl-ranch-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,9 @@ export const cadls: CadlRanchConfig[] = [
{
outputPath: "specialWords",
inputPath: "special-words"
},
{
outputPath: "unions",
inputPath: "unions"
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
emitters:
"../../../../../../../../../../packages/cadl-typescript/dist/src/index.js":
generateMetadata: false
generateTest: false
addCredentials: false
azureSdkForJs: false
isCadlTest: true
title: UnionsClient
packageDetails:
name: "@msinternal/unions"
description: "Unions Test Service"
Loading