Skip to content

Commit

Permalink
template reference
Browse files Browse the repository at this point in the history
code

fix typespec

fix
  • Loading branch information
XiaofeiCao committed Sep 20, 2024
1 parent 45f46e1 commit 29841a7
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,27 @@
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 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) {
return createEnumType(enumType, expandable, useCodeModelNameForEnumMember, null, null);
}

/**
* 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
* @param serializationMethodName method name for serialization
* @param deserializationMethodName method name for deserialization
* @return enum client type
*/
public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember, String serializationMethodName, String deserializationMethodName) {
JavaSettings settings = JavaSettings.getInstance();
String enumTypeName = enumType.getLanguage().getJava().getName();

Expand Down Expand Up @@ -82,17 +97,19 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, bo
}

return new EnumType.Builder()
.packageName(enumPackage)
.name(enumTypeName)
.description(description)
.expandable(expandable)
.values(enumValues)
.elementType(Mappers.getSchemaMapper().map(enumType.getChoiceType()))
.implementationDetails(new ImplementationDetails.Builder()
.usages(SchemaUtil.mapSchemaContext(enumType.getUsage()))
.build())
.crossLanguageDefinitionId(enumType.getCrossLanguageDefinitionId())
.build();
.packageName(enumPackage)
.name(enumTypeName)
.description(description)
.expandable(expandable)
.values(enumValues)
.elementType(Mappers.getSchemaMapper().map(enumType.getChoiceType()))
.implementationDetails(new ImplementationDetails.Builder()
.usages(SchemaUtil.mapSchemaContext(enumType.getUsage()))
.build())
.crossLanguageDefinitionId(enumType.getCrossLanguageDefinitionId())
.fromMethodName(deserializationMethodName)
.toMethodName(serializationMethodName)
.build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.microsoft.typespec.http.client.generator.core.model.clientmodel;

import com.azure.core.util.ExpandableEnum;
import com.microsoft.typespec.http.client.generator.core.extension.model.extensionmodel.XmsExtensions;
import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings;
import com.microsoft.typespec.http.client.generator.core.util.TemplateUtil;
Expand Down Expand Up @@ -159,6 +160,7 @@ private static ClassType.Builder getClassTypeBuilder(Class<?> classKey) {
public static final ClassType RESPONSE = getClassTypeBuilder(Response.class).build();
public static final ClassType SIMPLE_RESPONSE = getClassTypeBuilder(SimpleResponse.class).build();
public static final ClassType EXPANDABLE_STRING_ENUM = getClassTypeBuilder(ExpandableStringEnum.class).build();
public static final ClassType EXPANDABLE_ENUM = getClassTypeBuilder(ExpandableEnum.class).build();
public static final ClassType HTTP_PIPELINE_BUILDER = getClassTypeBuilder(HttpPipelineBuilder.class).build();
public static final ClassType KEY_CREDENTIAL_POLICY = getClassTypeBuilder(KeyCredentialPolicy.class).build();
public static final ClassType KEY_CREDENTIAL_TRAIT = getClassTypeBuilder(KeyCredentialTrait.class).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.microsoft.typespec.http.client.generator.core.model.clientmodel;

import com.azure.core.util.CoreUtils;
import com.microsoft.typespec.http.client.generator.core.util.CodeNamer;

import java.util.List;
Expand Down Expand Up @@ -35,19 +36,24 @@ public class EnumType implements IType {
private final ImplementationDetails implementationDetails;

private String crossLanguageDefinitionId;
private final String fromMethodName;
private final String toMethodName;

/**
* Create a new Enum with the provided properties.
* @param name The name of the new Enum.
* @param description The description of the Enum.
* @param expandable Whether this will be an ExpandableStringEnum type.
* @param values The values of the Enum.
*
* @param name The name of the new Enum.
* @param description The description of the Enum.
* @param expandable Whether this will be an ExpandableStringEnum type.
* @param values The values of the Enum.
* @param fromMethodName The method name used to convert JSON to the enum type.
* @param toMethodName The method name used to convert the enum type to JSON.
*/
private EnumType(String packageKeyword, String name, String description,
boolean expandable, List<ClientEnumValue> values,
IType elementType,
ImplementationDetails implementationDetails,
String crossLanguageDefinitionId) {
String crossLanguageDefinitionId, String fromMethodName, String toMethodName) {
this.name = name;
this.packageName = packageKeyword;
this.description = description;
Expand All @@ -56,6 +62,8 @@ private EnumType(String packageKeyword, String name, String description,
this.elementType = elementType;
this.implementationDetails = implementationDetails;
this.crossLanguageDefinitionId = crossLanguageDefinitionId;
this.fromMethodName = fromMethodName;
this.toMethodName = toMethodName;
}

public String getCrossLanguageDefinitionId() {
Expand Down Expand Up @@ -134,7 +142,9 @@ public final String defaultValueExpression(String sourceExpression) {
* @return The method name used to convert JSON to the enum type.
*/
public final String getFromMethodName() {
return "from" + CodeNamer.toPascalCase(elementType.getClientType().toString());
return CoreUtils.isNullOrEmpty(fromMethodName)
? "from" + CodeNamer.toPascalCase(elementType.getClientType().toString())
: fromMethodName;
}

/**
Expand All @@ -143,7 +153,9 @@ public final String getFromMethodName() {
* @return The method name used to convert the enum type to JSON.
*/
public final String getToMethodName() {
return "to" + CodeNamer.toPascalCase(elementType.getClientType().toString());
return CoreUtils.isNullOrEmpty(toMethodName)
? "to" + CodeNamer.toPascalCase(elementType.getClientType().toString())
: toMethodName;
}

@Override
Expand Down Expand Up @@ -231,6 +243,8 @@ public static class Builder {
private ImplementationDetails implementationDetails;

private String crossLanguageDefinitionId;
private String fromMethodName;
private String toMethodName;

/**
* Sets the name of the Enum.
Expand Down Expand Up @@ -309,6 +323,16 @@ public Builder crossLanguageDefinitionId(String crossLanguageDefinitionId) {
return this;
}

public Builder fromMethodName(String fromMethodName) {
this.fromMethodName = fromMethodName;
return this;
}

public Builder toMethodName(String toMethodName) {
this.toMethodName = toMethodName;
return this;
}

/**
* @return an immutable EnumType instance with the configurations on this builder.
*/
Expand All @@ -321,8 +345,8 @@ public EnumType build() {
values,
elementType,
implementationDetails,
crossLanguageDefinitionId
);
crossLanguageDefinitionId,
fromMethodName, toMethodName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final void write(EnumType enumType, JavaFile javaFile) {

if (enumType.getExpandable()) {
if(settings.isBranded()) {
writeExpandableStringEnum(enumType, javaFile, settings);
writeBrandedExpandableEnum(enumType, javaFile, settings);
} else {
writeExpandableStringEnumInterface(enumType, javaFile, settings);
}
Expand All @@ -49,6 +49,17 @@ public final void write(EnumType enumType, JavaFile javaFile) {
}
}

/**
* Extension point for expandable enum implementation of branded flavor.
*
* @param enumType enumType to write implementation
* @param javaFile javaFile to write into
* @param settings {@link JavaSettings} instance
*/
protected void writeBrandedExpandableEnum(EnumType enumType, JavaFile javaFile, JavaSettings settings) {
writeExpandableStringEnum(enumType, javaFile, settings);
}

private void writeExpandableStringEnumInterface(EnumType enumType, JavaFile javaFile, JavaSettings settings) {
Set<String> imports = new HashSet<>();
imports.add("java.util.Collection");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaPackage;
import com.microsoft.typespec.http.client.generator.core.preprocessor.Preprocessor;
import com.microsoft.typespec.http.client.generator.core.preprocessor.tranformer.Transformer;
import com.microsoft.typespec.http.client.generator.core.template.Templates;
import com.microsoft.typespec.http.client.generator.core.util.ClientModelUtil;
import com.azure.core.util.CoreUtils;
import com.azure.json.JsonReader;
import com.azure.json.ReadValueCallback;
import com.microsoft.typespec.http.client.generator.mapper.TypeSpecMapperFactory;
import com.microsoft.typespec.http.client.generator.model.EmitterOptions;
import com.microsoft.typespec.http.client.generator.template.TypeSpecTemplateFactory;
import com.microsoft.typespec.http.client.generator.util.FileUtil;
import com.microsoft.typespec.http.client.generator.util.ModelUtil;
import org.slf4j.Logger;
Expand Down Expand Up @@ -287,6 +289,7 @@ public TypeSpecPlugin(EmitterOptions options, boolean sdkIntegration) {
LOGGER.info("Namespace: {}", JavaSettings.getInstance().getPackage());

Mappers.setFactory(new TypeSpecMapperFactory());
Templates.setFactory(new TypeSpecTemplateFactory());
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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.mapper.TypeSpecChoiceMapper;
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 @@ -42,7 +43,7 @@ public ModelPropertyMapper getModelPropertyMapper() {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.microsoft.typespec.http.client.generator.core.extension.model.Message;
import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.CodeModel;
import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings;
import com.microsoft.typespec.http.client.generator.core.template.Templates;
import com.microsoft.typespec.http.client.generator.mgmt.FluentGen;
import com.microsoft.typespec.http.client.generator.mgmt.mapper.FluentMapper;
import com.microsoft.typespec.http.client.generator.mgmt.model.javamodel.FluentJavaPackage;
Expand Down Expand Up @@ -89,6 +90,7 @@ public void writeFile(String fileName, String content, List<Object> sourceMap) {
protected FluentMapper getFluentMapper() {
FluentMapper fluentMapper = super.getFluentMapper();
Mappers.setFactory(new TypeSpecFluentMapperFactory());
Templates.setFactory(new TypeSpecFluentTemplateFactory());
return fluentMapper;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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

import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate;
import com.microsoft.typespec.http.client.generator.mgmt.template.FluentTemplateFactory;
import com.microsoft.typespec.http.client.generator.template.TypeSpecEnumTemplate;

/**
* TypeSpec mgmt template factory.
*/
public class TypeSpecFluentTemplateFactory extends FluentTemplateFactory {
@Override
public EnumTemplate getEnumTemplate() {
return TypeSpecEnumTemplate.getInstance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.microsoft.typespec.http.client.generator.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.mapper.Mappers;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType;

public class TypeSpecChoiceMapper extends ChoiceMapper {
private static TypeSpecChoiceMapper INSTANCE = new TypeSpecChoiceMapper();

public static TypeSpecChoiceMapper getInstance() {
return INSTANCE;
}
private TypeSpecChoiceMapper() {
}

@Override
public IType map(ChoiceSchema enumType) {
IType elementType = Mappers.getSchemaMapper().map(enumType.getChoiceType());
boolean isStringEnum = elementType == ClassType.STRING;
if (isStringEnum) {
return MapperUtils.createEnumType(enumType, true, true);
} else {
return MapperUtils.createEnumType(enumType, true, true, "getValue", "fromValue");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.ClientMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.DefaultMapperFactory;
import com.microsoft.typespec.http.client.generator.core.mapper.PrimitiveMapper;
Expand All @@ -18,4 +19,9 @@ public ClientMapper getClientMapper() {
public PrimitiveMapper getPrimitiveMapper() {
return TypeSpecPrimitiveMapper.getInstance();
}

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

0 comments on commit 29841a7

Please sign in to comment.