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

[tcgc] support value type for client default value #1631

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

support value type for client default value
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export interface SdkModelPropertyTypeBase extends DecoratedType {
summary?: string;
apiVersions: string[];
onClient: boolean;
clientDefaultValue?: any;
clientDefaultValue?: unknown;
isApiVersionParam: boolean;
optional: boolean;
crossLanguageDefinitionId: string;
Expand Down
29 changes: 28 additions & 1 deletion packages/typespec-client-generator-core/src/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
namespace?: Namespace | Interface,
): {
isApiVersionParam: boolean;
clientDefaultValue?: unknown;
clientDefaultValue?: string;
} {
const isApiVersionParam = isApiVersion(context, type);
return {
Expand Down Expand Up @@ -569,3 +569,30 @@
)
);
}

export function getValueTypeValue(
value: Value,
): string | boolean | null | number | Array<unknown> | Object | undefined {

Check failure on line 575 in packages/typespec-client-generator-core/src/internal-utils.ts

View workflow job for this annotation

GitHub Actions / Lint

Prefer using the primitive `object` as a type name, rather than the upper-cased `Object`
switch (value.valueKind) {
case "ArrayValue":
return value.values.map((x) => getValueTypeValue(x));
case "BooleanValue":
case "StringValue":
case "NullValue":
return value.value;
case "NumericValue":
return value.value.asNumber();
case "EnumValue":
return value.value.value ?? value.value.name;
case "ObjectValue":
return Object.fromEntries(
[...value.properties.keys()].map((x) => [
x,
getValueTypeValue(value.properties.get(x)!.value),
]),
);
case "ScalarValue":
// TODO: handle scalar value
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
return undefined;
}
}
5 changes: 3 additions & 2 deletions packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
getHashForType,
getLocationOfOperation,
getTypeDecorators,
getValueTypeValue,
isNeverOrVoidType,
isSubscriptionId,
updateWithApiVersionInformation,
Expand Down Expand Up @@ -531,8 +532,8 @@ function getEndpointTypeFromSingleServer<
if (sdkParam.kind === "path") {
templateArguments.push(sdkParam);
sdkParam.onClient = true;
if (param.defaultValue && "value" in param.defaultValue) {
sdkParam.clientDefaultValue = param.defaultValue.value;
if (param.defaultValue) {
sdkParam.clientDefaultValue = getValueTypeValue(param.defaultValue);
}
const apiVersionInfo = updateWithApiVersionInformation(context, param, client.__raw.type);
sdkParam.isApiVersionParam = apiVersionInfo.isApiVersionParam;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ok, strictEqual } from "assert";
import { Model } from "@typespec/compiler";
import { deepStrictEqual, ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { getValueTypeValue } from "../src/internal-utils.js";
import { listSubClients } from "../src/public-utils.js";
import { SdkTestRunner, createSdkTestRunner } from "./test-host.js";

Expand Down Expand Up @@ -156,4 +158,150 @@ describe("typespec-client-generator-core: internal-utils", () => {
strictEqual(subClients[6].name, "AABGroup2");
});
});

describe("getValueTypeValue", () => {
it("string default value", async () => {
const { Test } = (await runner.compile(`
@service({})
namespace My.Service;

@test
model Test {
prop: string = "default";
}
`)) as { Test: Model };

strictEqual(getValueTypeValue(Test.properties.get("prop")?.defaultValue!), "default");
});

it("boolean default value", async () => {
const { Test } = (await runner.compile(`
@service({})
namespace My.Service;

@test
model Test {
prop: boolean = false;
}
`)) as { Test: Model };

strictEqual(getValueTypeValue(Test.properties.get("prop")?.defaultValue!), false);
});

it("null default value", async () => {
const { Test } = (await runner.compile(`
@service({})
namespace My.Service;

@test
model Test {
prop: boolean | null = null;
}
`)) as { Test: Model };

strictEqual(getValueTypeValue(Test.properties.get("prop")?.defaultValue!), null);
});

it("numeric int default value", async () => {
const { Test } = (await runner.compile(`
@service({})
namespace My.Service;

@test
model Test {
prop: int32 = 1;
}
`)) as { Test: Model };

strictEqual(getValueTypeValue(Test.properties.get("prop")?.defaultValue!), 1);
});

it("numeric float default value", async () => {
const { Test } = (await runner.compile(`
@service({})
namespace My.Service;

@test
model Test {
prop: float32 = 1.234;
}
`)) as { Test: Model };

strictEqual(getValueTypeValue(Test.properties.get("prop")?.defaultValue!), 1.234);
});

it("enum member default value", async () => {
const { Test } = (await runner.compile(`
@service({})
namespace My.Service;

@test
model Test {
prop: MyEnum = MyEnum.A;
}

enum MyEnum {
A: "A",
B: "B",
}
`)) as { Test: Model };

strictEqual(getValueTypeValue(Test.properties.get("prop")?.defaultValue!), "A");
});

it("enum member without value default value", async () => {
const { Test } = (await runner.compile(`
@service({})
namespace My.Service;

@test
model Test {
prop: MyEnum = MyEnum.A;
}

enum MyEnum {
A,
B,
}
`)) as { Test: Model };

strictEqual(getValueTypeValue(Test.properties.get("prop")?.defaultValue!), "A");
});

it("array default value", async () => {
const { Test } = (await runner.compile(`
@service({})
namespace My.Service;

@test
model Test {
prop: string[] = #["a", "b"];
}
`)) as { Test: Model };

deepStrictEqual(getValueTypeValue(Test.properties.get("prop")?.defaultValue!), ["a", "b"]);
});

it("object default value", async () => {
const { Test } = (await runner.compile(`
@service({})
namespace My.Service;

@test
model Test {
prop: Point = #{ x: 0, y: 0 };
}

model Point {
x: int32;
y: int32;
}
`)) as { Test: Model };

deepStrictEqual(getValueTypeValue(Test.properties.get("prop")?.defaultValue!), {
x: 0,
y: 0,
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -942,4 +942,48 @@ describe("typespec-client-generator-core: package", () => {
client.initialization.properties.find((x) => x.isApiVersionParam),
);
});

it("endpoint template argument with default value of enum member", async () => {
await runner.compile(`
@server(
"{endpoint}/client/structure/{client}",
"",
{
@doc("Need to be set as 'http://localhost:3000' in client.")
endpoint: url,

@doc("Need to be set as 'default', 'multi-client', 'renamed-operation', 'two-operation-group' in client.")
client: ClientType = ClientType.Default,
}
)
@service({})
namespace My.Service;

enum ClientType {
Default: "default",
MultiClient: "multi-client",
RenamedOperation: "renamed-operation",
TwoOperationGroup: "two-operation-group",
ClientOperationGroup: "client-operation-group",
}
`);
const sdkPackage = runner.context.sdkPackage;
strictEqual(sdkPackage.clients.length, 1);
const client = sdkPackage.clients[0];

strictEqual(client.initialization.properties.length, 1);
const parameter = client.initialization.properties[0];
strictEqual(parameter.name, "endpoint");
strictEqual(parameter.type.kind, "union");

const templateArg = parameter.type.variantTypes[0];
strictEqual(templateArg.kind, "endpoint");
strictEqual(templateArg.templateArguments.length, 2);
const clientTemplateArg = templateArg.templateArguments[1];
strictEqual(clientTemplateArg.kind, "path");
strictEqual(clientTemplateArg.name, "client");
strictEqual(clientTemplateArg.optional, false);
strictEqual(clientTemplateArg.onClient, true);
strictEqual(clientTemplateArg.clientDefaultValue, "default");
});
});
Loading