Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java codegen, use TCGC enum name for MPG #4315

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ChoiceMapper implements IMapper<ChoiceSchema, IType> {
private static final ChoiceMapper INSTANCE = new ChoiceMapper();
Map<ChoiceSchema, IType> parsed = new ConcurrentHashMap<>();

private ChoiceMapper() {
protected ChoiceMapper() {
}

/**
Expand Down Expand Up @@ -47,6 +47,6 @@ public IType map(ChoiceSchema enumType) {
}

private IType createChoiceType(ChoiceSchema enumType) {
return MapperUtils.createEnumType(enumType, true);
return MapperUtils.createEnumType(enumType, true, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@
/**
* Contains utility methods to help map from modelerfour to Java Autorest.
*/
final class MapperUtils {
static IType createEnumType(ChoiceSchema enumType, boolean expandable) {
public final class MapperUtils {
/**
* Create enum client type from code model.
* @param enumType code model schema for enum
* @param expandable whether it's expandable enum
* @param useCodeModelNameForEnumMember whether to use code model enum member name for client enum member name
* @return enum client type
*/
public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember) {
JavaSettings settings = JavaSettings.getInstance();
String enumTypeName = enumType.getLanguage().getJava().getName();

Expand All @@ -54,7 +61,7 @@ static IType createEnumType(ChoiceSchema enumType, boolean expandable) {
for (ChoiceValue enumValue : enumType.getChoices()) {
String enumName = enumValue.getValue();
String enumDescription = null;
if (!settings.isFluent()) {
if (useCodeModelNameForEnumMember) {
if (enumValue.getLanguage() != null && enumValue.getLanguage().getJava() != null
&& enumValue.getLanguage().getJava().getName() != null) {
enumName = enumValue.getLanguage().getJava().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SealedChoiceMapper implements IMapper<SealedChoiceSchema, IType> {
private static final SealedChoiceMapper INSTANCE = new SealedChoiceMapper();
Map<SealedChoiceSchema, IType> parsed = new ConcurrentHashMap<>();

private SealedChoiceMapper() {
protected SealedChoiceMapper() {
}

public static SealedChoiceMapper getInstance() {
Expand All @@ -38,6 +38,6 @@ public IType map(SealedChoiceSchema enumType) {
}

private IType createSealedChoiceType(SealedChoiceSchema enumType) {
return MapperUtils.createEnumType(enumType, false);
return MapperUtils.createEnumType(enumType, false, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.typespec.http.client.generator.mgmt.mapper;

import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema;
import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.MapperUtils;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType;

public class FluentChoiceMapper extends ChoiceMapper {
private static final FluentChoiceMapper INSTANCE = new FluentChoiceMapper();
private FluentChoiceMapper() {}

public static FluentChoiceMapper getInstance() {
return INSTANCE;
}

@Override
public IType map(ChoiceSchema enumType) {
return MapperUtils.createEnumType(enumType, true, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.microsoft.typespec.http.client.generator.mgmt.mapper;

import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.ClientMethodMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.DefaultMapperFactory;
import com.microsoft.typespec.http.client.generator.core.mapper.ExceptionMapper;
Expand All @@ -11,6 +12,7 @@
import com.microsoft.typespec.http.client.generator.core.mapper.ObjectMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.PrimitiveMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.ProxyMethodMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.SealedChoiceMapper;

public class FluentMapperFactory extends DefaultMapperFactory {

Expand Down Expand Up @@ -48,4 +50,14 @@ public PrimitiveMapper getPrimitiveMapper() {
public ModelMapper getModelMapper() {
return FluentModelMapper.getInstance();
}

@Override
public ChoiceMapper getChoiceMapper() {
return FluentChoiceMapper.getInstance();
}

@Override
public SealedChoiceMapper getSealedChoiceMapper() {
return FluentSealedChoiceMapper.getInstance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.typespec.http.client.generator.mgmt.mapper;

import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.SealedChoiceSchema;
import com.microsoft.typespec.http.client.generator.core.mapper.MapperUtils;
import com.microsoft.typespec.http.client.generator.core.mapper.SealedChoiceMapper;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType;

public class FluentSealedChoiceMapper extends SealedChoiceMapper {
private static final FluentSealedChoiceMapper INSTANCE = new FluentSealedChoiceMapper();
private FluentSealedChoiceMapper() {}

public static FluentSealedChoiceMapper getInstance() {
return INSTANCE;
}

@Override
public IType map(SealedChoiceSchema enumType) {
return MapperUtils.createEnumType(enumType, false, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
*/
public final class ManagedServiceIdentityType extends ExpandableStringEnum<ManagedServiceIdentityType> {
/**
* Static value None for ManagedServiceIdentityType.
* No managed identity.
XiaofeiCao marked this conversation as resolved.
Show resolved Hide resolved
*/
public static final ManagedServiceIdentityType NONE = fromString("None");

/**
* Static value SystemAssigned for ManagedServiceIdentityType.
* System assigned managed identity.
*/
public static final ManagedServiceIdentityType SYSTEM_ASSIGNED = fromString("SystemAssigned");

/**
* Static value UserAssigned for ManagedServiceIdentityType.
* User assigned managed identity.
*/
public static final ManagedServiceIdentityType USER_ASSIGNED = fromString("UserAssigned");

/**
* Static value SystemAssigned,UserAssigned for ManagedServiceIdentityType.
* System and user assigned managed identity.
*/
public static final ManagedServiceIdentityType SYSTEM_ASSIGNED_USER_ASSIGNED
public static final ManagedServiceIdentityType SYSTEM_AND_USER_ASSIGNED_V3
XiaofeiCao marked this conversation as resolved.
Show resolved Hide resolved
= fromString("SystemAssigned,UserAssigned");

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
*/
public final class ProvisioningState extends ExpandableStringEnum<ProvisioningState> {
/**
* Static value Succeeded for ProvisioningState.
* Resource has been created.
*/
public static final ProvisioningState SUCCEEDED = fromString("Succeeded");

/**
* Static value Failed for ProvisioningState.
* Resource creation failed.
*/
public static final ProvisioningState FAILED = fromString("Failed");

/**
* Static value Canceled for ProvisioningState.
* Resource creation was canceled.
*/
public static final ProvisioningState CANCELED = fromString("Canceled");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public final class ActionType extends ExpandableStringEnum<ActionType> {
/**
* Static value Internal for ActionType.
* Actions are for internal-only APIs.
*/
public static final ActionType INTERNAL = fromString("Internal");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind", defaultImpl = Dog.class, visible = true)
@JsonTypeName("Dog")
@JsonSubTypes({ @JsonSubTypes.Type(name = "golden", value = Golden.class) })
@JsonSubTypes({ @JsonSubTypes.Type(name = "golden_dog", value = Golden.class) })
@Fluent
public class Dog {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
public final class DogKind extends ExpandableStringEnum<DogKind> {
/**
* Static value golden for DogKind.
* Species golden.
*/
public static final DogKind GOLDEN = fromString("golden");
public static final DogKind GOLDEN = fromString("golden_dog");

/**
* Creates a new instance of DogKind value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Golden dog model.
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind", defaultImpl = Golden.class, visible = true)
@JsonTypeName("golden")
@JsonTypeName("golden_dog")
@Fluent
public final class Golden extends Dog {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
*/
public final class ManagedServiceIdentityType extends ExpandableStringEnum<ManagedServiceIdentityType> {
/**
* Static value None for ManagedServiceIdentityType.
* No managed identity.
*/
public static final ManagedServiceIdentityType NONE = fromString("None");

/**
* Static value SystemAssigned for ManagedServiceIdentityType.
* System assigned managed identity.
*/
public static final ManagedServiceIdentityType SYSTEM_ASSIGNED = fromString("SystemAssigned");

/**
* Static value UserAssigned for ManagedServiceIdentityType.
* User assigned managed identity.
*/
public static final ManagedServiceIdentityType USER_ASSIGNED = fromString("UserAssigned");

/**
* Static value SystemAssigned,UserAssigned for ManagedServiceIdentityType.
* System and user assigned managed identity.
*/
public static final ManagedServiceIdentityType SYSTEM_ASSIGNED_USER_ASSIGNED
public static final ManagedServiceIdentityType SYSTEM_AND_USER_ASSIGNED_V3
= fromString("SystemAssigned,UserAssigned");

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
*/
public final class Origin extends ExpandableStringEnum<Origin> {
/**
* Static value user for Origin.
* Indicates the operation is initiated by a user.
*/
public static final Origin USER = fromString("user");

/**
* Static value system for Origin.
* Indicates the operation is initiated by a system.
*/
public static final Origin SYSTEM = fromString("system");

/**
* Static value user,system for Origin.
* Indicates the operation is initiated by a user or system.
*/
public static final Origin USER_SYSTEM = fromString("user,system");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
*/
public final class ProvisioningState extends ExpandableStringEnum<ProvisioningState> {
/**
* Static value Succeeded for ProvisioningState.
* Resource has been created.
*/
public static final ProvisioningState SUCCEEDED = fromString("Succeeded");

/**
* Static value Failed for ProvisioningState.
* Resource creation failed.
*/
public static final ProvisioningState FAILED = fromString("Failed");

/**
* Static value Canceled for ProvisioningState.
* Resource creation was canceled.
*/
public static final ProvisioningState CANCELED = fromString("Canceled");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ union DogKind {
string,

@doc("Species golden")
Golden: "golden",
Golden: "golden_dog",
}

@doc("Test extensible enum type for discriminator")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@

public class Main {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private static final String DEFAULT_OUTPUT_DIR = "http-client-generator-test/tsp-output/";


private static Yaml yaml = null;

// java -jar target/azure-typespec-extension-jar-with-dependencies.jar
public static void main(String[] args) throws IOException {
// parameters
String inputYamlFileName = "typespec-tests/tsp-output/code-model.yaml";
String inputYamlFileName = DEFAULT_OUTPUT_DIR + "code-model.yaml";
if (args.length >= 1) {
inputYamlFileName = args[0];
}
Expand Down Expand Up @@ -200,7 +201,7 @@ private static EmitterOptions loadEmitterOptions(CodeModel codeModel) {

// output path
if (CoreUtils.isNullOrEmpty(options.getOutputDir())) {
options.setOutputDir("typespec-tests/tsp-output/");
options.setOutputDir(DEFAULT_OUTPUT_DIR);
} else if (!options.getOutputDir().endsWith("/")) {
options.setOutputDir(options.getOutputDir() + "/");
}
Expand All @@ -212,7 +213,7 @@ private static EmitterOptions loadEmitterOptions(CodeModel codeModel) {
if (options == null) {
// default if emitterOptions fails
options = new EmitterOptions();
options.setOutputDir("typespec-tests/tsp-output/");
options.setOutputDir(DEFAULT_OUTPUT_DIR);
if (codeModel.getLanguage().getJava() != null && !CoreUtils.isNullOrEmpty(
codeModel.getLanguage().getJava().getNamespace())) {
options.setNamespace(codeModel.getLanguage().getJava().getNamespace());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package com.microsoft.typespec.http.client.generator.fluent;

import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.SealedChoiceMapper;
import com.microsoft.typespec.http.client.generator.mgmt.mapper.FluentMapperFactory;
import com.microsoft.typespec.http.client.generator.core.mapper.ClientMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.ModelMapper;
Expand Down Expand Up @@ -37,4 +39,14 @@ public ModelMapper getModelMapper() {
public ModelPropertyMapper getModelPropertyMapper() {
return TypeSpecFluentModelPropertyMapper.getInstance();
}

@Override
public ChoiceMapper getChoiceMapper() {
return ChoiceMapper.getInstance();
}

@Override
public SealedChoiceMapper getSealedChoiceMapper() {
return SealedChoiceMapper.getInstance();
}
}
Loading