Skip to content

Commit 67af8a7

Browse files
authored
encoding-default (#470)
1 parent 08263ea commit 67af8a7

File tree

3 files changed

+81
-22
lines changed

3 files changed

+81
-22
lines changed

src/typespec-aaz/src/convertor.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
isVoidType,
5656
resolveEncodedName,
5757
IntrinsicType,
58+
serializeValueAsJson,
5859
} from "@typespec/compiler";
5960
import { TwoLevelMap } from "@typespec/compiler/utils";
6061
import {
@@ -699,7 +700,7 @@ function convert2CMDSchema(
699700

700701
if (param.defaultValue) {
701702
schema.default = {
702-
value: getDefaultValue(context, param.defaultValue),
703+
value: getDefaultValue(context, param.defaultValue, param),
703704
};
704705
}
705706
}
@@ -708,6 +709,10 @@ function convert2CMDSchema(
708709
...schema,
709710
...applySchemaFormat(context, param, schema as CMDSchemaBase),
710711
};
712+
schema = {
713+
...schema,
714+
...applyEncoding(context, param, schema),
715+
};
711716
schema = {
712717
...schema,
713718
...applyExtensionsDecorators(context, param, schema),
@@ -759,6 +764,7 @@ function convert2CMDSchemaBase(context: AAZSchemaEmitterContext, type: Type): CM
759764
}
760765
if (schema) {
761766
schema = applySchemaFormat(context, type, schema);
767+
schema = applyEncoding(context, type, schema);
762768
schema = applyExtensionsDecorators(context, type, schema);
763769
}
764770

@@ -1898,6 +1904,20 @@ function emitArrayFormat(
18981904

18991905
// TODO: add emitResourceIdFormat
19001906

1907+
function applyEncoding(context: AAZSchemaEmitterContext, type: Type, schema: CMDSchemaBase): CMDSchemaBase {
1908+
if (type.kind !== "Scalar" && type.kind !== "ModelProperty") {
1909+
return schema;
1910+
}
1911+
const encodeData = getEncode(context.program, type);
1912+
if (encodeData !== undefined) {
1913+
schema = {
1914+
...schema,
1915+
...convertScalar2CMDSchemaBase(context, encodeData.type),
1916+
};
1917+
}
1918+
return schema;
1919+
}
1920+
19011921
// apply extension decorators
19021922
function applyExtensionsDecorators(context: AAZSchemaEmitterContext, type: Type, schema: CMDSchemaBase): CMDSchemaBase {
19031923
const extensions = getExtensions(context.program, type);
@@ -2124,25 +2144,6 @@ function getClsDefinitionModel(schema: CMDClsSchemaBase): CMDObjectSchemaBase |
21242144
return schema.type.pendingSchema.schema!;
21252145
}
21262146

2127-
function getDefaultValue(content: AAZSchemaEmitterContext, defaultType: Value): unknown {
2128-
switch (defaultType.valueKind) {
2129-
case "StringValue":
2130-
return defaultType.value;
2131-
case "NumericValue":
2132-
return defaultType.value.asNumber() ?? undefined;
2133-
case "BooleanValue":
2134-
return defaultType.value;
2135-
case "ArrayValue":
2136-
return defaultType.values.map((x) => getDefaultValue(content, x));
2137-
case "NullValue":
2138-
return null;
2139-
case "EnumValue":
2140-
return defaultType.value.value ?? defaultType.value.name;
2141-
default:
2142-
reportDiagnostic(content.program, {
2143-
code: "invalid-default",
2144-
format: { type: defaultType.valueKind },
2145-
target: defaultType,
2146-
});
2147-
}
2147+
function getDefaultValue(context: AAZSchemaEmitterContext, defaultType: Value, modelProperty: ModelProperty): any {
2148+
return serializeValueAsJson(context.program, defaultType, modelProperty);
21482149
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { TestHost, BasicTestRunner } from "@typespec/compiler/testing";
2+
import { describe, expect, it, beforeEach } from "vitest";
3+
import { createTypespecAazTestHost, createTypespecAazTestRunner, compileTypespecAAZOperations } from "./test-aaz.js";
4+
import { ModelVar, generateCompileArmResourceTemplate, findObjectsWithKey } from "./util.js";
5+
6+
describe("encoding parsing", () => {
7+
let host: TestHost;
8+
let runner: BasicTestRunner;
9+
10+
beforeEach(async () => {
11+
host = await createTypespecAazTestHost();
12+
runner = await createTypespecAazTestRunner(host);
13+
});
14+
15+
it("validate encoding duration with default", async () => {
16+
const modelTmp: ModelVar = {
17+
modelKey: "targetValue",
18+
modelContent: "TargetEncodingModel",
19+
isComposite: true,
20+
appendModel: `
21+
model TargetEncodingModel {
22+
@encode(DurationKnownEncoding.seconds, int32)
23+
errorRateTimeWindowInSeconds?: duration = duration.fromISO("PT60S");
24+
}
25+
`,
26+
};
27+
28+
const code: string = generateCompileArmResourceTemplate(modelTmp);
29+
const result = await compileTypespecAAZOperations(
30+
code,
31+
{
32+
"operation": "get-resources-operations",
33+
"api-version": "A",
34+
"resources": ["/subscriptions/{}/resourcegroups/{}/providers/microsoft.mock/mockresources/{}"],
35+
},
36+
runner,
37+
);
38+
const resultObj = JSON.parse(result!);
39+
expect(Array.isArray(resultObj)).toBe(true);
40+
expect(resultObj.length).toBe(1);
41+
const targetObj = findObjectsWithKey(resultObj[0].pathItem.get.read.http.responses, "targetValue");
42+
await expect(JSON.stringify(targetObj, null, 2)).toMatchFileSnapshot("./snapshots/encoding-duration-prop.json");
43+
});
44+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"type": "object",
3+
"props": [
4+
{
5+
"type": "integer32",
6+
"name": "errorRateTimeWindowInSeconds",
7+
"default": {
8+
"value": 60
9+
}
10+
}
11+
],
12+
"name": "targetValue",
13+
"required": true
14+
}

0 commit comments

Comments
 (0)