Skip to content

Commit

Permalink
Support scalar doc (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh authored Apr 2, 2024
1 parent 70186b9 commit 560f2e4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/scalar_doc-2024-3-2-17-15-13.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

support sclar doc
8 changes: 2 additions & 6 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ interface SdkTypeBase {
*/
nullable: boolean;
deprecation?: string;
description?: string;
details?: string;
}

export type SdkType =
Expand Down Expand Up @@ -241,8 +243,6 @@ export interface SdkEnumType extends SdkTypeBase {
valueType: SdkBuiltInType;
values: SdkEnumValueType[];
isFixed: boolean;
description?: string;
details?: string;
isFlags: boolean;
usage: UsageFlags;
access?: AccessFlags;
Expand All @@ -257,8 +257,6 @@ export interface SdkEnumValueType extends SdkTypeBase {
value: string | number;
enumType: SdkEnumType;
valueType: SdkBuiltInType;
description?: string;
details?: string;
}
export interface SdkConstantType extends SdkTypeBase {
kind: "constant";
Expand Down Expand Up @@ -288,8 +286,6 @@ export interface SdkModelType extends SdkTypeBase {
*/
isError: boolean;
isGeneratedName: boolean;
description?: string;
details?: string;
access?: AccessFlags;
usage: UsageFlags;
additionalProperties?: SdkType;
Expand Down
6 changes: 6 additions & 0 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,12 @@ function getSdkBuiltInTypeWithDiagnostics(
kind = getScalarKind(type);
}
}
const docWrapper = getDocHelper(context, type);
return diagnostics.wrap({
...getSdkTypeBaseHelper(context, type, kind),
encode: getEncodeHelper(context, type, kind),
description: docWrapper.description,
details: docWrapper.details,
});
} else if (type.kind === "String" || type.kind === "Boolean" || type.kind === "Number") {
let kind: SdkBuiltInKinds;
Expand Down Expand Up @@ -779,6 +782,9 @@ export function getClientTypeWithDiagnostics(
retval = getKnownValuesEnum(context, type, operation) ?? baseType;
const namespace = type.namespace ? getNamespaceFullName(type.namespace) : "";
retval.kind = context.knownScalars[`${namespace}.${type.name}`] ?? retval.kind;
const docWrapper = getDocHelper(context, type);
retval.description = docWrapper.description;
retval.details = docWrapper.details;
break;
}
if (type.name === "utcDateTime" || type.name === "offsetDateTime") {
Expand Down
29 changes: 28 additions & 1 deletion packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,25 @@ describe("typespec-client-generator-core: types", () => {
}
}
});
it("with doc", async () => {
await runner.compileWithBuiltInService(
`
@doc("doc")
@summary("title")
scalar TestScalar extends string;
@usage(Usage.input | Usage.output)
@access(Access.public)
model Test {
prop: TestScalar;
}
`
);
const models = getAllModels(runner.context);
strictEqual(models[0].kind, "model");
strictEqual(models[0].properties[0].type.description, "title");
strictEqual(models[0].properties[0].type.details, "doc");
});
});
describe("SdkDurationType", () => {
it("default", async function () {
Expand Down Expand Up @@ -333,9 +352,11 @@ describe("typespec-client-generator-core: types", () => {
it("float seconds decorated scalar", async function () {
await runner.compileWithBuiltInService(
`
@doc("doc")
@summary("title")
@encode(DurationKnownEncoding.seconds, float32)
scalar Float32Duration extends duration;
@usage(Usage.input | Usage.output)
@access(Access.public)
model Test {
Expand All @@ -348,6 +369,8 @@ describe("typespec-client-generator-core: types", () => {
strictEqual(sdkType.valueType.kind, "duration");
strictEqual(sdkType.valueType.wireType.kind, "float32");
strictEqual(sdkType.valueType.encode, "seconds");
strictEqual(sdkType.valueType.description, "title");
strictEqual(sdkType.valueType.details, "doc");
});
});

Expand Down Expand Up @@ -420,6 +443,8 @@ describe("typespec-client-generator-core: types", () => {
it("unixTimestamp array", async function () {
await runner.compileWithBuiltInService(
`
@doc("doc")
@summary("title")
@encode(DateTimeKnownEncoding.unixTimestamp, int64)
scalar unixTimestampDatetime extends utcDateTime;
Expand All @@ -435,6 +460,8 @@ describe("typespec-client-generator-core: types", () => {
strictEqual(sdkType.valueType.kind, "utcDateTime");
strictEqual(sdkType.valueType.wireType.kind, "int64");
strictEqual(sdkType.valueType.encode, "unixTimestamp");
strictEqual(sdkType.valueType.description, "title");
strictEqual(sdkType.valueType.details, "doc");
});
});
describe("SdkUnionType", () => {
Expand Down

0 comments on commit 560f2e4

Please sign in to comment.