Skip to content

Commit

Permalink
Add azureLocation (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Mar 4, 2024
1 parent dc49c3e commit 7dd798b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 16 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/add_azure_location-2024-1-29-16-5-38.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-azure-core"
---

Add new `azureLocation` scalar
7 changes: 7 additions & 0 deletions .chronus/changes/add_azure_location-2024-2-4-12-13-28.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

add support for azureLocation scalar in azure core
14 changes: 14 additions & 0 deletions docs/libraries/azure-core/reference/data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,20 @@ Repeatability Result header options
union Azure.Core.RepeatabilityResult
```

### `azureLocation` {#Azure.Core.azureLocation}

Represents an Azure geography region where supported resource providers live.

```typespec
scalar Azure.Core.azureLocation
```

#### Examples

```
WestUS
```

### `eTag` {#Azure.Core.eTag}

The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource.
Expand Down
11 changes: 11 additions & 0 deletions packages/typespec-azure-core/lib/models.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,14 @@ scalar ipV6Address extends string;
#suppress "@azure-tools/typespec-autorest/invalid-format" "Foundation."
@format("eTag")
scalar eTag extends string;

/**
* Represents an Azure geography region where supported resource providers live.
*
* @example
*
* ```
* WestUS
* ```
*/
scalar azureLocation extends string;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Operation, Program, Type } from "@typespec/compiler";
import { SdkEnumType, SdkModelType } from "./interfaces.js";
import { SdkBuiltInKinds, SdkEnumType, SdkModelType } from "./interfaces.js";

export function parseEmitterName(emitterName?: string): string {
if (!emitterName) {
Expand All @@ -24,6 +24,7 @@ export interface TCGCContext {
modelsMap?: Map<Type, SdkModelType | SdkEnumType>;
operationModelsMap?: Map<Operation, Map<Type, SdkModelType | SdkEnumType>>;
generatedNames?: Set<string>;
knownScalars?: Record<string, SdkBuiltInKinds>;
}

export function createTCGCContext(program: Program): TCGCContext {
Expand Down
23 changes: 13 additions & 10 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
getEncode,
getFormat,
getKnownValues,
getNamespaceFullName,
getVisibility,
ignoreDiagnostics,
isNeverType,
Expand Down Expand Up @@ -114,28 +115,22 @@ function addFormatInfo(
type: ModelProperty | Scalar,
propertyType: SdkType
): void {
const format = getFormat(context.program, type)?.toLocaleLowerCase();
const format = getFormat(context.program, type);
if (format) {
switch (format) {
case "guid":
case "uuid":
case "password":
case "etag":
case "azureLocation":
case "armId":
case "ipAddress":
propertyType.kind = format;
break;
case "url":
case "uri":
propertyType.kind = "url";
break;
case "armid":
propertyType.kind = "armId";
break;
case "ipaddress":
propertyType.kind = "ipAddress";
break;
case "azurelocation":
propertyType.kind = "azureLocation";
break;
default:
break;
}
Expand Down Expand Up @@ -213,6 +208,7 @@ function getScalarKind(scalar: Scalar): SdkBuiltInKinds {
case "decimal":
case "plainDate":
case "plainTime":
case "azureLocation":
return scalar.name;
default:
throw Error(`Unknown scalar kind ${scalar.name}`);
Expand Down Expand Up @@ -713,6 +709,11 @@ export function getClientTypeWithDiagnostics(
type: Type,
operation?: Operation
): [SdkType, readonly Diagnostic[]] {
if (!context.knownScalars) {
context.knownScalars = {
"Azure.Core.azureLocation": "azureLocation",
};
}
const diagnostics = createDiagnosticCollector();
let retval: SdkType | undefined = undefined;
switch (type.kind) {
Expand Down Expand Up @@ -741,6 +742,8 @@ export function getClientTypeWithDiagnostics(
addEncodeInfo(context, type, baseType);
addFormatInfo(context, type, baseType);
retval = getKnownValuesEnum(context, type, operation) ?? baseType;
const namespace = type.namespace ? getNamespaceFullName(type.namespace) : "";
retval.kind = context.knownScalars[`${namespace}.${type.name}`] ?? retval.kind;
break;
}
if (type.name === "utcDateTime" || type.name === "offsetDateTime") {
Expand Down
16 changes: 11 additions & 5 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,17 @@ describe("typespec-client-generator-core: types", () => {
});

it("format", async function () {
await runner.compileWithBuiltInService(
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core"],
emitterName: "@azure-tools/typespec-java",
});
await runnerWithCore.compile(
`
@useDependency(Azure.Core.Versions.v1_0_Preview_2)
@service({})
namespace MyService;
@format("guid")
scalar guid extends string;
Expand All @@ -159,9 +168,6 @@ describe("typespec-client-generator-core: types", () => {
@format("ipAddress")
scalar ipAddress extends string;
@format("azureLocation")
scalar azureLocation extends string;
@format("etag")
scalar etag extends string;
Expand Down Expand Up @@ -196,7 +202,7 @@ describe("typespec-client-generator-core: types", () => {
}
`
);
const models = getAllModels(runner.context);
const models = getAllModels(runnerWithCore.context);
for (const property of (models[0] as SdkModelType).properties) {
strictEqual(
property.type.kind,
Expand Down

0 comments on commit 7dd798b

Please sign in to comment.