From b6ca37b3c4f35c8959f049a9d945744560a1ca6c Mon Sep 17 00:00:00 2001 From: Xiaogang Ding Date: Sun, 8 Dec 2024 14:05:21 +0800 Subject: [PATCH] Tsp - Add support for constant value --- .../src/utils/modelUtils.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/typespec-powershell/src/utils/modelUtils.ts b/packages/typespec-powershell/src/utils/modelUtils.ts index 030a2ff937..da28c3cf27 100644 --- a/packages/typespec-powershell/src/utils/modelUtils.ts +++ b/packages/typespec-powershell/src/utils/modelUtils.ts @@ -49,7 +49,7 @@ import { import { SdkContext, isReadOnly } from "@azure-tools/typespec-client-generator-core"; import { reportDiagnostic } from "../lib.js"; -import { AnySchema, SealedChoiceSchema, ChoiceSchema, ChoiceValue, SchemaType, ArraySchema, Schema, DictionarySchema, ObjectSchema, Discriminator as M4Discriminator, Property, StringSchema, NumberSchema, ConstantSchema, ConstantValue } from "@autorest/codemodel"; +import { AnySchema, SealedChoiceSchema, ChoiceSchema, ChoiceValue, SchemaType, ArraySchema, Schema, DictionarySchema, ObjectSchema, Discriminator as M4Discriminator, Property, StringSchema, NumberSchema, ConstantSchema, ConstantValue, BooleanSchema } from "@autorest/codemodel"; import { getHeaderFieldName, getPathParamName, @@ -967,15 +967,22 @@ function getSchemaForModel( // OA schema is just a regular object schema. function getSchemaForLiteral(type: Type): any { // ToDo: by xiaogang, need to implement other kinds as String - switch (type.kind) { - case "Number": - return { type: `${type.value}`, isConstant: true }; - case "String": { - const schema = new StringSchema(type.value, ""); - return schema; + if (type.kind) { + const schema = new ConstantSchema("", ""); + switch (type.kind) { + case "Number": + schema.valueType = new NumberSchema("Constant", "Constant number", SchemaType.Number, 64); + schema.value = new ConstantValue(type.value); + return schema; + case "String": + schema.valueType = new StringSchema("Constant", "Constant string"); + schema.value = new ConstantValue(type.value); + return schema; + case "Boolean": + schema.valueType = new BooleanSchema("Constant", "Constant boolean"); + schema.value = new ConstantValue(type.value); + return schema; } - case "Boolean": - return { type: `${type.value}`, isConstant: true }; } if (type.kind === undefined) { if (typeof type === "string") {