Skip to content

Commit

Permalink
Java codegen, use TCGC enum name for MPG (#4315)
Browse files Browse the repository at this point in the history
- fix Azure/autorest.java#2940
- test generated with no diff
  • Loading branch information
XiaofeiCao committed Sep 20, 2024
1 parent 594dc97 commit 91f0042
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 32 deletions.
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.
*/
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 @@ -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 @@ -14,7 +14,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 @@ -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();
}
}

0 comments on commit 91f0042

Please sign in to comment.