Skip to content

Commit

Permalink
Change enum definition to latest grammer
Browse files Browse the repository at this point in the history
  • Loading branch information
pshao25 committed Apr 10, 2024
1 parent 977049c commit 260504e
Show file tree
Hide file tree
Showing 33 changed files with 3,372 additions and 2,809 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ 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 ? `"${m.name}": ${m.value}` : 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",

/**
* 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",

/**
* For building an extraction model to identify your domain categories using your
* own data.
*/
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",
"InvalidArgument",
"Unauthorized",
"Forbidden",
"NotFound",
"ProjectNotFound",
"OperationNotFound",
"AzureCognitiveSearchNotFound",
"AzureCognitiveSearchIndexNotFound",
"TooManyRequests",
"AzureCognitiveSearchThrottling",
"AzureCognitiveSearchIndexLimitReached",
"InternalServerError",
"ServiceUnavailable",
"Timeout",
"QuotaExceeded",
"Conflict",
"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",
"InvalidParameterValue",
"KnowledgeBaseNotFound",
"AzureCognitiveSearchNotFound",
"AzureCognitiveSearchThrottling",
"ExtractionFailure",
"InvalidRequestBodyFormat",
"EmptyRequest",
"MissingInputDocuments",
"InvalidDocument",
"ModelVersionIncorrect",
"InvalidDocumentBatch",
"UnsupportedLanguageCode",
"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",
}

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",
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,47 @@ enum ApiVersion {
* Define the impute method, can be one of auto, previous, linear, fixed, zero,
* notFill.
*/
enum ImputeMode {
auto,
previous,
linear,
fixed,
zero,
notFill,
union ImputeMode {
string,
"auto",
"previous",
"linear",
"fixed",
"zero",
"notFill",
}

/**
* The error code.
*/
enum AnomalyDetectorErrorCodes {
InvalidCustomInterval,
BadArgument,
InvalidGranularity,
InvalidPeriod,
InvalidModelArgument,
InvalidSeries,
InvalidJsonFormat,
RequiredGranularity,
RequiredSeries,
InvalidImputeMode,
InvalidImputeFixedValue,
union AnomalyDetectorErrorCodes {
string,
"InvalidCustomInterval",
"BadArgument",
"InvalidGranularity",
"InvalidPeriod",
"InvalidModelArgument",
"InvalidSeries",
"InvalidJsonFormat",
"RequiredGranularity",
"RequiredSeries",
"InvalidImputeMode",
"InvalidImputeFixedValue",
}

/**
* An optional field, indicating how missing values will be filled. One of
* Previous, Subsequent, Linear, Zero, Fixed, and NotFill. Cannot be set to
* NotFill, when the alignMode is Outer.
*/
enum FillNAMethod {
Previous,
Subsequent,
Linear,
Zero,
Fixed,
NotFill,
union FillNAMethod {
string,
"Previous",
"Subsequent",
"Linear",
"Zero",
"Fixed",
"NotFill",
}

/**
Expand All @@ -65,7 +68,6 @@ enum FillNAMethod {
* be none by default. If granularity is none, the timestamp property in time
* series point can be absent.
*/
@fixed
enum TimeGranularity {
yearly,
monthly,
Expand All @@ -81,15 +83,13 @@ enum TimeGranularity {
/**
* Status of detection results. One of CREATED, RUNNING, READY, and FAILED.
*/
@fixed
enum DetectionStatus {
CREATED,
RUNNING,
READY,
FAILED,
}

@fixed
enum DataSchema {
OneTable,
MultiTable,
Expand All @@ -99,7 +99,6 @@ enum DataSchema {
* An optional field, indicating how we align different variables to the same
* time-range. Either Inner or Outer.
*/
@fixed
enum AlignMode {
Inner,
Outer,
Expand All @@ -108,7 +107,6 @@ enum AlignMode {
/**
* Model status. One of CREATED, RUNNING, READY, and FAILED.
*/
@fixed
enum ModelStatus {
CREATED,
RUNNING,
Expand Down
Loading

0 comments on commit 260504e

Please sign in to comment.