Skip to content

Commit

Permalink
Change enum definition to latest grammer (#4945)
Browse files Browse the repository at this point in the history
Fix #4943
  • Loading branch information
pshao25 committed May 29, 2024
1 parent 21d78e4 commit 72bbf9c
Show file tree
Hide file tree
Showing 38 changed files with 4,384 additions and 2,884 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@autorest/openapi-to-typespec",
"comment": "Change enum outout to the latest definition",
"type": "patch"
}
],
"packageName": "@autorest/openapi-to-typespec"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ export function generateEnums(typespecEnum: TypespecEnum) {
const decorators = generateDecorators(typespecEnum.decorators);
decorators && definitions.push(decorators);

const enumDefinition = `
const enumDefinition =
typespecEnum.isExtensible && !["ApiVersion"].includes(typespecEnum.name)
? `
union ${typespecEnum.name} {
${typespecEnum.choiceType},\n
${typespecEnum.members
.map((m) => {
const kv = `"${m.name}": ${m.value}`;
return `${generateDocs(m)}${kv}`;
})
.join(", ")}
}\n\n`
: `
enum ${typespecEnum.name} {
${typespecEnum.members
.map((m) => {
Expand Down
1 change: 1 addition & 0 deletions packages/extensions/openapi-to-typespec/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export interface TypespecEnum extends TypespecDataType {
isExtensible: boolean;
decorators?: TypespecDecorator[];
clientDecorators?: TypespecDecorator[];
choiceType: string;
}

export interface WithFixMe {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChoiceSchema, ChoiceValue, CodeModel, SchemaType, SealedChoiceSchema } from "@autorest/codemodel";
import { getDataTypes } from "../data-types";
import { TypespecChoiceValue, TypespecEnum } from "../interfaces";
import { getEnumChoiceClientDecorators, getEnumClientDecorators, getEnumDecorators } from "../utils/decorators";
import { getEnumChoiceClientDecorators, getEnumClientDecorators } from "../utils/decorators";
import { transformValue } from "../utils/values";

export function transformEnum(schema: SealedChoiceSchema | ChoiceSchema, codeModel: CodeModel): TypespecEnum {
Expand All @@ -11,7 +11,7 @@ export function transformEnum(schema: SealedChoiceSchema | ChoiceSchema, codeMod

if (!typespecEnum) {
typespecEnum = {
decorators: getEnumDecorators(schema),
decorators: [],
clientDecorators: getEnumClientDecorators(schema),
doc: schema.language.default.description,
kind: "enum",
Expand All @@ -23,6 +23,7 @@ export function transformEnum(schema: SealedChoiceSchema | ChoiceSchema, codeMod
"// FIXME: (synthetic-name) This enum has a generated name. Please rename it to something more appropriate.",
],
}),
choiceType: schema.choiceType.type,
};
}
return typespecEnum;
Expand Down
14 changes: 0 additions & 14 deletions packages/extensions/openapi-to-typespec/src/utils/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,6 @@ function escapeRegex(str: string) {
return str.replace(/\\/g, "\\\\");
}

export function getEnumDecorators(enumeration: SealedChoiceSchema | ChoiceSchema): TypespecDecorator[] {
const decorators: TypespecDecorator[] = [];

if (isSealedChoiceSchema(enumeration)) {
decorators.push({
name: "fixed",
module: "@azure-tools/typespec-azure-core",
namespace: "Azure.Core",
});
}

return decorators;
}

export function getEnumClientDecorators(enumeration: SealedChoiceSchema | ChoiceSchema): TypespecDecorator[] {
const decorators: TypespecDecorator[] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,91 +8,101 @@ using Azure.Core;

namespace Azure.Language.Authoring;

enum ProjectKind {
union ProjectKind {
string,

/**
* For building a classification model to classify text using your own data. Each
* file will have only one label. For example, file 1 is classified as A and file
* 2 is classified as B.
*/
CustomSingleLabelClassification,
CustomSingleLabelClassification: "CustomSingleLabelClassification",

/**
* For building a classification model to classify text using your own data. Each
* file can have one or many labels. For example, file 1 is classified as A, B,
* and C and file 2 is classified as B and C.
*/
CustomMultiLabelClassification,
CustomMultiLabelClassification: "CustomMultiLabelClassification",

/**
* For building an extraction model to identify your domain categories using your
* own data.
*/
CustomEntityRecognition,
CustomEntityRecognition: "CustomEntityRecognition",
}

/**
* Human-readable error code.
*/
enum ErrorCode {
InvalidRequest,
InvalidArgument,
Unauthorized,
Forbidden,
NotFound,
ProjectNotFound,
OperationNotFound,
AzureCognitiveSearchNotFound,
AzureCognitiveSearchIndexNotFound,
TooManyRequests,
AzureCognitiveSearchThrottling,
AzureCognitiveSearchIndexLimitReached,
InternalServerError,
ServiceUnavailable,
Timeout,
QuotaExceeded,
Conflict,
Warning,
union ErrorCode {
string,
InvalidRequest: "InvalidRequest",
InvalidArgument: "InvalidArgument",
Unauthorized: "Unauthorized",
Forbidden: "Forbidden",
NotFound: "NotFound",
ProjectNotFound: "ProjectNotFound",
OperationNotFound: "OperationNotFound",
AzureCognitiveSearchNotFound: "AzureCognitiveSearchNotFound",
AzureCognitiveSearchIndexNotFound: "AzureCognitiveSearchIndexNotFound",
TooManyRequests: "TooManyRequests",
AzureCognitiveSearchThrottling: "AzureCognitiveSearchThrottling",
AzureCognitiveSearchIndexLimitReached: "AzureCognitiveSearchIndexLimitReached",
InternalServerError: "InternalServerError",
ServiceUnavailable: "ServiceUnavailable",
Timeout: "Timeout",
QuotaExceeded: "QuotaExceeded",
Conflict: "Conflict",
Warning: "Warning",
}

/**
* Human-readable error code.
*/
enum InnerErrorCode {
InvalidRequest,
InvalidParameterValue,
KnowledgeBaseNotFound,
AzureCognitiveSearchNotFound,
AzureCognitiveSearchThrottling,
ExtractionFailure,
InvalidRequestBodyFormat,
EmptyRequest,
MissingInputDocuments,
InvalidDocument,
ModelVersionIncorrect,
InvalidDocumentBatch,
UnsupportedLanguageCode,
InvalidCountryHint,
union InnerErrorCode {
string,
InvalidRequest: "InvalidRequest",
InvalidParameterValue: "InvalidParameterValue",
KnowledgeBaseNotFound: "KnowledgeBaseNotFound",
AzureCognitiveSearchNotFound: "AzureCognitiveSearchNotFound",
AzureCognitiveSearchThrottling: "AzureCognitiveSearchThrottling",
ExtractionFailure: "ExtractionFailure",
InvalidRequestBodyFormat: "InvalidRequestBodyFormat",
EmptyRequest: "EmptyRequest",
MissingInputDocuments: "MissingInputDocuments",
InvalidDocument: "InvalidDocument",
ModelVersionIncorrect: "ModelVersionIncorrect",
InvalidDocumentBatch: "InvalidDocumentBatch",
UnsupportedLanguageCode: "UnsupportedLanguageCode",
InvalidCountryHint: "InvalidCountryHint",
}

enum StringIndexType {
union StringIndexType {
string,

/**
* The offset and length values will correspond to UTF-16 code units. Use this
* option if your application is written in a language that support Unicode, for
* example Java, JavaScript.
*/
Utf16CodeUnit,
Utf16CodeUnit: "Utf16CodeUnit",
}

enum StringIndexTypeAutoGenerated {
union StringIndexTypeAutoGenerated {
string,

/**
* The offset and length values will correspond to UTF-16 code units. Use this
* option if your application is written in a language that support Unicode, for
* example Java, JavaScript.
*/
Utf16CodeUnit,
Utf16CodeUnit: "Utf16CodeUnit",
}

enum EvaluationKind {
union EvaluationKind {
string,

/**
* Split the data into training and test sets according to user-defined
* percentages.
Expand All @@ -105,7 +115,8 @@ enum EvaluationKind {
Manual: "manual",
}

enum JobStatus {
union JobStatus {
string,
NotStarted: "notStarted",
Running: "running",
Succeeded: "succeeded",
Expand Down
Loading

0 comments on commit 72bbf9c

Please sign in to comment.