Skip to content

Commit

Permalink
Add name and tspNamespace to SdkArrayType (#1000)
Browse files Browse the repository at this point in the history
Fixes #999

This PR adds name and namespace to `SdkArrayType` so that we could know
if this array is an ordinary array, or an array created somewhere which
needs special handling on the SDK generator side.

---------

Co-authored-by: Haoling Dong <87355844+haolingdong-msft@users.noreply.github.com>
  • Loading branch information
ArcturusZhang and haolingdong-msft authored Jun 18, 2024
1 parent 45a9585 commit 0a349d2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Add `name` and `tspNamespace` to `SdkArrayType`
2 changes: 2 additions & 0 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export interface SdkDurationType extends SdkTypeBase {

export interface SdkArrayType extends SdkTypeBase {
kind: "array";
name: string;
tspNamespace?: string;
valueType: SdkType;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,15 @@ export function getSdkArrayOrDictWithDiagnostics(
keyType: diagnostics.pipe(
getClientTypeWithDiagnostics(context, type.indexer.key, operation)
),
valueType,
valueType: valueType,
});
} else if (name === "integer") {
// only array's index key name is integer
return diagnostics.wrap({
...getSdkTypeBaseHelper(context, type, "array"),
valueType,
name: getLibraryName(context, type),
tspNamespace: getNamespaceHelper(type.namespace),
valueType: valueType,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing";
import { ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { SdkTestRunner, createSdkTestRunner } from "../test-host.js";
Expand All @@ -11,16 +12,16 @@ describe("typespec-client-generator-core: array types", () => {

it("use model is to represent array", async () => {
await runner.compile(`
@service({})
namespace TestClient {
model TestModel {
prop: string;
}
model TestArray is TestModel[];
op get(): TestArray;
@service({})
namespace TestClient {
model TestModel {
prop: string;
}
`);
model TestArray is TestModel[];
op get(): TestArray;
}
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
const model = models[0];
Expand All @@ -32,7 +33,65 @@ describe("typespec-client-generator-core: array types", () => {
ok(method);
strictEqual(method.response.kind, "method");
strictEqual(method.response.type?.kind, "array");
strictEqual(method.response.type?.name, "TestArray");
strictEqual(method.response.type?.tspNamespace, "TestClient");
strictEqual(method.response.type?.valueType.kind, "model");
strictEqual(method.response.type?.valueType.name, "TestModel");
});

it("EmbeddingVector from azure-core", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core"],
"filter-out-core-models": false,
emitterName: "@azure-tools/typespec-java",
});
await runnerWithCore.compileWithBuiltInAzureCoreService(`
@service({})
namespace TestClient {
model ModelWithEmbeddingVector {
prop: EmbeddingVector<int32>;
}
op get(): ModelWithEmbeddingVector;
}
`);
const models = runnerWithCore.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
const model = models[0];
const property = model.properties[0];
strictEqual(property.type.kind, "array");
strictEqual(property.type.name, "EmbeddingVector");
strictEqual(property.type.tspNamespace, "Azure.Core");
strictEqual(property.type.valueType.kind, "int32");
});

it("alias of EmbeddingVector", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core"],
"filter-out-core-models": false,
emitterName: "@azure-tools/typespec-java",
});
await runnerWithCore.compileWithBuiltInAzureCoreService(`
@service({})
namespace TestClient {
alias MyEmbeddingVector = EmbeddingVector<int32>;
model ModelWithEmbeddingVector {
prop: MyEmbeddingVector;
}
op get(): ModelWithEmbeddingVector;
}
`);
const models = runnerWithCore.context.experimental_sdkPackage.models;
strictEqual(models.length, 1);
const model = models[0];
const property = model.properties[0];
strictEqual(property.type.kind, "array");
strictEqual(property.type.name, "EmbeddingVector");
strictEqual(property.type.tspNamespace, "Azure.Core");
strictEqual(property.type.valueType.kind, "int32");
});
});

0 comments on commit 0a349d2

Please sign in to comment.