From 5324fdb5abc9b21eb0a9ecc9f838b67e3d202804 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Sat, 11 May 2024 16:53:45 +0800 Subject: [PATCH] tsp, enum is closed enum (#2752) --- typespec-extension/changelog.md | 6 ++ typespec-extension/package-lock.json | 4 +- typespec-extension/package.json | 2 +- typespec-extension/src/code-model-builder.ts | 3 +- typespec-tests/package.json | 2 +- .../longrunning/models/OperationState.java | 62 +++++++++---------- .../cadl/response/models/OperationState.java | 62 +++++++++---------- .../com/versioning/added/models/EnumV1.java | 57 +++++++++-------- .../com/versioning/added/models/EnumV2.java | 52 ++++++++-------- .../com/versioning/removed/models/EnumV2.java | 52 ++++++++-------- .../renamedfrom/models/NewEnum.java | 52 ++++++++-------- typespec-tests/tsp/enum.tsp | 4 -- typespec-tests/tsp/internal.tsp | 1 - typespec-tests/tsp/naming.tsp | 1 - 14 files changed, 175 insertions(+), 185 deletions(-) diff --git a/typespec-extension/changelog.md b/typespec-extension/changelog.md index 4ede67dca8..a7634ba380 100644 --- a/typespec-extension/changelog.md +++ b/typespec-extension/changelog.md @@ -1,5 +1,11 @@ # Release History +## 0.15.18 (2024-05-13) + +Compatible with compiler 0.56. + +- `enum` is closed enum. + ## 0.15.17 (2024-05-11) Compatible with compiler 0.56. diff --git a/typespec-extension/package-lock.json b/typespec-extension/package-lock.json index d3aa8cb160..00c9a0de0f 100644 --- a/typespec-extension/package-lock.json +++ b/typespec-extension/package-lock.json @@ -1,12 +1,12 @@ { "name": "@azure-tools/typespec-java", - "version": "0.15.17", + "version": "0.15.18", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@azure-tools/typespec-java", - "version": "0.15.17", + "version": "0.15.18", "license": "MIT", "dependencies": { "@autorest/codemodel": "~4.20.0", diff --git a/typespec-extension/package.json b/typespec-extension/package.json index e4125e5b91..baaef548d2 100644 --- a/typespec-extension/package.json +++ b/typespec-extension/package.json @@ -1,6 +1,6 @@ { "name": "@azure-tools/typespec-java", - "version": "0.15.17", + "version": "0.15.18", "description": "TypeSpec library for emitting Java client from the TypeSpec REST protocol binding", "keywords": [ "TypeSpec" diff --git a/typespec-extension/src/code-model-builder.ts b/typespec-extension/src/code-model-builder.ts index 6300a0f725..d35258b88e 100644 --- a/typespec-extension/src/code-model-builder.ts +++ b/typespec-extension/src/code-model-builder.ts @@ -61,7 +61,6 @@ import { Version, getAddedOnVersions, getVersion } from "@typespec/versioning"; import { isPollingLocation, getPagedResult, - isFixed, getLroMetadata, getUnionAsEnum, UnionEnum, @@ -1726,7 +1725,7 @@ export class CodeModelBuilder { return this.processConstantSchemaForLiteral(type, nameHint); case "Enum": - return this.processChoiceSchema(type, this.getName(type), isFixed(this.program, type)); + return this.processChoiceSchema(type, this.getName(type), true); case "Union": return this.processUnionSchema(type, this.getName(type, nameHint)); diff --git a/typespec-tests/package.json b/typespec-tests/package.json index d7f3b0c157..45e7d72583 100644 --- a/typespec-tests/package.json +++ b/typespec-tests/package.json @@ -10,7 +10,7 @@ }, "dependencies": { "@azure-tools/cadl-ranch-specs": "0.33.4", - "@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.15.17.tgz" + "@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.15.18.tgz" }, "devDependencies": { "@typespec/prettier-plugin-typespec": "~0.56.0", diff --git a/typespec-tests/src/main/java/com/cadl/longrunning/models/OperationState.java b/typespec-tests/src/main/java/com/cadl/longrunning/models/OperationState.java index 30784b9a26..9313f4f237 100644 --- a/typespec-tests/src/main/java/com/cadl/longrunning/models/OperationState.java +++ b/typespec-tests/src/main/java/com/cadl/longrunning/models/OperationState.java @@ -4,72 +4,68 @@ package com.cadl.longrunning.models; -import com.azure.core.annotation.Generated; -import com.azure.core.util.ExpandableStringEnum; -import java.util.Collection; - /** * Enum describing allowed operation states. */ -public final class OperationState extends ExpandableStringEnum { +public enum OperationState { /** * The operation has not started. */ - @Generated - public static final OperationState NOT_STARTED = fromString("NotStarted"); + NOT_STARTED("NotStarted"), /** * The operation is in progress. */ - @Generated - public static final OperationState RUNNING = fromString("Running"); + RUNNING("Running"), /** * The operation has completed successfully. */ - @Generated - public static final OperationState SUCCEEDED = fromString("Succeeded"); + SUCCEEDED("Succeeded"), /** * The operation has failed. */ - @Generated - public static final OperationState FAILED = fromString("Failed"); + FAILED("Failed"), /** * The operation has been canceled by the user. */ - @Generated - public static final OperationState CANCELED = fromString("Canceled"); + CANCELED("Canceled"); /** - * Creates a new instance of OperationState value. - * - * @deprecated Use the {@link #fromString(String)} factory method. + * The actual serialized value for a OperationState instance. */ - @Generated - @Deprecated - public OperationState() { + private final String value; + + OperationState(String value) { + this.value = value; } /** - * Creates or finds a OperationState from its string representation. + * Parses a serialized value to a OperationState instance. * - * @param name a name to look for. - * @return the corresponding OperationState. + * @param value the serialized value to parse. + * @return the parsed OperationState object, or null if unable to parse. */ - @Generated - public static OperationState fromString(String name) { - return fromString(name, OperationState.class); + public static OperationState fromString(String value) { + if (value == null) { + return null; + } + OperationState[] items = OperationState.values(); + for (OperationState item : items) { + if (item.toString().equalsIgnoreCase(value)) { + return item; + } + } + return null; } /** - * Gets known OperationState values. - * - * @return known OperationState values. + * {@inheritDoc} */ - @Generated - public static Collection values() { - return values(OperationState.class); + @Override + public String toString() { + return this.value; } } diff --git a/typespec-tests/src/main/java/com/cadl/response/models/OperationState.java b/typespec-tests/src/main/java/com/cadl/response/models/OperationState.java index ce23be521f..f8874d5a62 100644 --- a/typespec-tests/src/main/java/com/cadl/response/models/OperationState.java +++ b/typespec-tests/src/main/java/com/cadl/response/models/OperationState.java @@ -4,72 +4,68 @@ package com.cadl.response.models; -import com.azure.core.annotation.Generated; -import com.azure.core.util.ExpandableStringEnum; -import java.util.Collection; - /** * Enum describing allowed operation states. */ -public final class OperationState extends ExpandableStringEnum { +public enum OperationState { /** * The operation has not started. */ - @Generated - public static final OperationState NOT_STARTED = fromString("NotStarted"); + NOT_STARTED("NotStarted"), /** * The operation is in progress. */ - @Generated - public static final OperationState RUNNING = fromString("Running"); + RUNNING("Running"), /** * The operation has completed successfully. */ - @Generated - public static final OperationState SUCCEEDED = fromString("Succeeded"); + SUCCEEDED("Succeeded"), /** * The operation has failed. */ - @Generated - public static final OperationState FAILED = fromString("Failed"); + FAILED("Failed"), /** * The operation has been canceled by the user. */ - @Generated - public static final OperationState CANCELED = fromString("Canceled"); + CANCELED("Canceled"); /** - * Creates a new instance of OperationState value. - * - * @deprecated Use the {@link #fromString(String)} factory method. + * The actual serialized value for a OperationState instance. */ - @Generated - @Deprecated - public OperationState() { + private final String value; + + OperationState(String value) { + this.value = value; } /** - * Creates or finds a OperationState from its string representation. + * Parses a serialized value to a OperationState instance. * - * @param name a name to look for. - * @return the corresponding OperationState. + * @param value the serialized value to parse. + * @return the parsed OperationState object, or null if unable to parse. */ - @Generated - public static OperationState fromString(String name) { - return fromString(name, OperationState.class); + public static OperationState fromString(String value) { + if (value == null) { + return null; + } + OperationState[] items = OperationState.values(); + for (OperationState item : items) { + if (item.toString().equalsIgnoreCase(value)) { + return item; + } + } + return null; } /** - * Gets known OperationState values. - * - * @return known OperationState values. + * {@inheritDoc} */ - @Generated - public static Collection values() { - return values(OperationState.class); + @Override + public String toString() { + return this.value; } } diff --git a/typespec-tests/src/main/java/com/versioning/added/models/EnumV1.java b/typespec-tests/src/main/java/com/versioning/added/models/EnumV1.java index 469a70ce4f..35a2adf324 100644 --- a/typespec-tests/src/main/java/com/versioning/added/models/EnumV1.java +++ b/typespec-tests/src/main/java/com/versioning/added/models/EnumV1.java @@ -4,54 +4,53 @@ package com.versioning.added.models; -import com.azure.core.annotation.Generated; -import com.azure.core.util.ExpandableStringEnum; -import java.util.Collection; - /** * Defines values for EnumV1. */ -public final class EnumV1 extends ExpandableStringEnum { +public enum EnumV1 { /** - * Static value enumMemberV1 for EnumV1. + * Enum value enumMemberV1. */ - @Generated - public static final EnumV1 ENUM_MEMBER_V1 = fromString("enumMemberV1"); + ENUM_MEMBER_V1("enumMemberV1"), /** - * Static value enumMemberV2 for EnumV1. + * Enum value enumMemberV2. */ - @Generated - public static final EnumV1 ENUM_MEMBER_V2 = fromString("enumMemberV2"); + ENUM_MEMBER_V2("enumMemberV2"); /** - * Creates a new instance of EnumV1 value. - * - * @deprecated Use the {@link #fromString(String)} factory method. + * The actual serialized value for a EnumV1 instance. */ - @Generated - @Deprecated - public EnumV1() { + private final String value; + + EnumV1(String value) { + this.value = value; } /** - * Creates or finds a EnumV1 from its string representation. + * Parses a serialized value to a EnumV1 instance. * - * @param name a name to look for. - * @return the corresponding EnumV1. + * @param value the serialized value to parse. + * @return the parsed EnumV1 object, or null if unable to parse. */ - @Generated - public static EnumV1 fromString(String name) { - return fromString(name, EnumV1.class); + public static EnumV1 fromString(String value) { + if (value == null) { + return null; + } + EnumV1[] items = EnumV1.values(); + for (EnumV1 item : items) { + if (item.toString().equalsIgnoreCase(value)) { + return item; + } + } + return null; } /** - * Gets known EnumV1 values. - * - * @return known EnumV1 values. + * {@inheritDoc} */ - @Generated - public static Collection values() { - return values(EnumV1.class); + @Override + public String toString() { + return this.value; } } diff --git a/typespec-tests/src/main/java/com/versioning/added/models/EnumV2.java b/typespec-tests/src/main/java/com/versioning/added/models/EnumV2.java index 4cdef517c5..0b9382c290 100644 --- a/typespec-tests/src/main/java/com/versioning/added/models/EnumV2.java +++ b/typespec-tests/src/main/java/com/versioning/added/models/EnumV2.java @@ -4,48 +4,48 @@ package com.versioning.added.models; -import com.azure.core.annotation.Generated; -import com.azure.core.util.ExpandableStringEnum; -import java.util.Collection; - /** * Defines values for EnumV2. */ -public final class EnumV2 extends ExpandableStringEnum { +public enum EnumV2 { /** - * Static value enumMember for EnumV2. + * Enum value enumMember. */ - @Generated - public static final EnumV2 ENUM_MEMBER = fromString("enumMember"); + ENUM_MEMBER("enumMember"); /** - * Creates a new instance of EnumV2 value. - * - * @deprecated Use the {@link #fromString(String)} factory method. + * The actual serialized value for a EnumV2 instance. */ - @Generated - @Deprecated - public EnumV2() { + private final String value; + + EnumV2(String value) { + this.value = value; } /** - * Creates or finds a EnumV2 from its string representation. + * Parses a serialized value to a EnumV2 instance. * - * @param name a name to look for. - * @return the corresponding EnumV2. + * @param value the serialized value to parse. + * @return the parsed EnumV2 object, or null if unable to parse. */ - @Generated - public static EnumV2 fromString(String name) { - return fromString(name, EnumV2.class); + public static EnumV2 fromString(String value) { + if (value == null) { + return null; + } + EnumV2[] items = EnumV2.values(); + for (EnumV2 item : items) { + if (item.toString().equalsIgnoreCase(value)) { + return item; + } + } + return null; } /** - * Gets known EnumV2 values. - * - * @return known EnumV2 values. + * {@inheritDoc} */ - @Generated - public static Collection values() { - return values(EnumV2.class); + @Override + public String toString() { + return this.value; } } diff --git a/typespec-tests/src/main/java/com/versioning/removed/models/EnumV2.java b/typespec-tests/src/main/java/com/versioning/removed/models/EnumV2.java index 4ee52e10bd..9a64c307a4 100644 --- a/typespec-tests/src/main/java/com/versioning/removed/models/EnumV2.java +++ b/typespec-tests/src/main/java/com/versioning/removed/models/EnumV2.java @@ -4,48 +4,48 @@ package com.versioning.removed.models; -import com.azure.core.annotation.Generated; -import com.azure.core.util.ExpandableStringEnum; -import java.util.Collection; - /** * Defines values for EnumV2. */ -public final class EnumV2 extends ExpandableStringEnum { +public enum EnumV2 { /** - * Static value enumMemberV2 for EnumV2. + * Enum value enumMemberV2. */ - @Generated - public static final EnumV2 ENUM_MEMBER_V2 = fromString("enumMemberV2"); + ENUM_MEMBER_V2("enumMemberV2"); /** - * Creates a new instance of EnumV2 value. - * - * @deprecated Use the {@link #fromString(String)} factory method. + * The actual serialized value for a EnumV2 instance. */ - @Generated - @Deprecated - public EnumV2() { + private final String value; + + EnumV2(String value) { + this.value = value; } /** - * Creates or finds a EnumV2 from its string representation. + * Parses a serialized value to a EnumV2 instance. * - * @param name a name to look for. - * @return the corresponding EnumV2. + * @param value the serialized value to parse. + * @return the parsed EnumV2 object, or null if unable to parse. */ - @Generated - public static EnumV2 fromString(String name) { - return fromString(name, EnumV2.class); + public static EnumV2 fromString(String value) { + if (value == null) { + return null; + } + EnumV2[] items = EnumV2.values(); + for (EnumV2 item : items) { + if (item.toString().equalsIgnoreCase(value)) { + return item; + } + } + return null; } /** - * Gets known EnumV2 values. - * - * @return known EnumV2 values. + * {@inheritDoc} */ - @Generated - public static Collection values() { - return values(EnumV2.class); + @Override + public String toString() { + return this.value; } } diff --git a/typespec-tests/src/main/java/com/versioning/renamedfrom/models/NewEnum.java b/typespec-tests/src/main/java/com/versioning/renamedfrom/models/NewEnum.java index d5967e7d01..7206ee2585 100644 --- a/typespec-tests/src/main/java/com/versioning/renamedfrom/models/NewEnum.java +++ b/typespec-tests/src/main/java/com/versioning/renamedfrom/models/NewEnum.java @@ -4,48 +4,48 @@ package com.versioning.renamedfrom.models; -import com.azure.core.annotation.Generated; -import com.azure.core.util.ExpandableStringEnum; -import java.util.Collection; - /** * Defines values for NewEnum. */ -public final class NewEnum extends ExpandableStringEnum { +public enum NewEnum { /** - * Static value newEnumMember for NewEnum. + * Enum value newEnumMember. */ - @Generated - public static final NewEnum NEW_ENUM_MEMBER = fromString("newEnumMember"); + NEW_ENUM_MEMBER("newEnumMember"); /** - * Creates a new instance of NewEnum value. - * - * @deprecated Use the {@link #fromString(String)} factory method. + * The actual serialized value for a NewEnum instance. */ - @Generated - @Deprecated - public NewEnum() { + private final String value; + + NewEnum(String value) { + this.value = value; } /** - * Creates or finds a NewEnum from its string representation. + * Parses a serialized value to a NewEnum instance. * - * @param name a name to look for. - * @return the corresponding NewEnum. + * @param value the serialized value to parse. + * @return the parsed NewEnum object, or null if unable to parse. */ - @Generated - public static NewEnum fromString(String name) { - return fromString(name, NewEnum.class); + public static NewEnum fromString(String value) { + if (value == null) { + return null; + } + NewEnum[] items = NewEnum.values(); + for (NewEnum item : items) { + if (item.toString().equalsIgnoreCase(value)) { + return item; + } + } + return null; } /** - * Gets known NewEnum values. - * - * @return known NewEnum values. + * {@inheritDoc} */ - @Generated - public static Collection values() { - return values(NewEnum.class); + @Override + public String toString() { + return this.value; } } diff --git a/typespec-tests/tsp/enum.tsp b/typespec-tests/tsp/enum.tsp index 1fff3f6e68..54d21fbec7 100644 --- a/typespec-tests/tsp/enum.tsp +++ b/typespec-tests/tsp/enum.tsp @@ -12,7 +12,6 @@ using Azure.ClientGenerator.Core; namespace Cadl.EnumService; #suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "For testing" -@fixed enum Color { Red, Blue, @@ -38,14 +37,12 @@ union PriorityModel { } #suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "For testing" -@fixed enum Priority { High: 100, Low: 0, } #suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "For testing" -@fixed enum Unit { Grams: 1, KiloGrams: 0.001, @@ -53,7 +50,6 @@ enum Unit { } #suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "For testing" -@fixed enum OperationStateValues { Running, Completed, diff --git a/typespec-tests/tsp/internal.tsp b/typespec-tests/tsp/internal.tsp index 8161bd910a..95e1981cc3 100644 --- a/typespec-tests/tsp/internal.tsp +++ b/typespec-tests/tsp/internal.tsp @@ -60,7 +60,6 @@ model StandAloneUnion { // Color and ColorModel will be generated in implementation.models package #suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "For testing" @access(Access.internal) -@fixed enum Color { Red, Blue, diff --git a/typespec-tests/tsp/naming.tsp b/typespec-tests/tsp/naming.tsp index ba4011ea31..3b5c86726e 100644 --- a/typespec-tests/tsp/naming.tsp +++ b/typespec-tests/tsp/naming.tsp @@ -77,7 +77,6 @@ model BytesData extends Data { #suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "For testing" @summary("summary of Types") @doc("description of Types") -@fixed enum TypesModel { Blob, File,