[elm] Add flag to prefix custom type variants (#1288)

This commit is contained in:
Erik Timmers 2018-10-26 12:20:36 +02:00 committed by William Cheng
parent ed8433a849
commit 1b115bb4f7
2 changed files with 33 additions and 4 deletions

View File

@ -27,6 +27,6 @@ fi
# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g elm -o samples/client/petstore/elm $@"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g elm -t modules/openapi-generator/src/main/resources/elm -o samples/openapi3/client/petstore/elm --additional-properties elmPrefixCustomTypeVariants=true $@"
java $JAVA_OPTS -jar $executable $ags

View File

@ -59,8 +59,10 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(ElmClientCodegen.class);
private Set<String> customPrimitives = new HashSet<String>();
private ElmVersion elmVersion = ElmVersion.ELM_019;
private Boolean elmPrefixCustomTypeVariants = false;
private static final String ELM_VERSION = "elmVersion";
private static final String ELM_PREFIX_CUSTOM_TYPE_VARIANTS = "elmPrefixCustomTypeVariants";
private static final String ENCODER = "elmEncoder";
private static final String DECODER = "elmDecoder";
private static final String DISCRIMINATOR_NAME = "discriminatorName";
@ -162,13 +164,14 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
supportedVersions.put("0.19", "Elm 0.19");
elmVersion.setEnum(supportedVersions);
cliOptions.add(elmVersion);
final CliOption elmPrefixCustomTypeVariants = CliOption.newBoolean(ELM_PREFIX_CUSTOM_TYPE_VARIANTS, "Prefix custom type variants");
cliOptions.add(elmPrefixCustomTypeVariants);
}
@Override
public void processOpts() {
super.processOpts();
if (additionalProperties.containsKey(ELM_VERSION)) {
final String version = (String) additionalProperties.get(ELM_VERSION);
if ("0.18".equals(version)) {
@ -178,6 +181,10 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
}
}
if (additionalProperties.containsKey(ELM_PREFIX_CUSTOM_TYPE_VARIANTS)) {
elmPrefixCustomTypeVariants = Boolean.TRUE.equals(Boolean.valueOf(additionalProperties.get(ELM_PREFIX_CUSTOM_TYPE_VARIANTS).toString()));
}
if (StringUtils.isEmpty(System.getenv("ELM_POST_PROCESS_FILE"))) {
if (elmVersion.equals(ElmVersion.ELM_018)) { // 0.18
LOGGER.info("Environment variable ELM_POST_PROCESS_FILE not defined so the Elm code may not be properly formatted. To define it, try `export ELM_POST_PROCESS_FILE=\"/usr/local/bin/elm-format --elm-version={} --yes\"` (Linux/Mac)", "0.18");
@ -188,7 +195,7 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
switch (elmVersion) {
case ELM_018:
LOGGER.info("Elm version = 0.18");
LOGGER.info("Elm version: 0.18");
additionalProperties.put("isElm018", true);
supportingFiles.add(new SupportingFile("DateOnly018.mustache", "src", "DateOnly.elm"));
supportingFiles.add(new SupportingFile("DateTime018.mustache", "src", "DateTime.elm"));
@ -196,7 +203,7 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
supportingFiles.add(new SupportingFile("Main018.mustache", "src", "Main.elm"));
break;
case ELM_019:
LOGGER.info("Elm version = 0.19");
LOGGER.info("Elm version: 0.19");
additionalProperties.put("isElm019", true);
supportingFiles.add(new SupportingFile("DateOnly.mustache", "src", "DateOnly.elm"));
supportingFiles.add(new SupportingFile("DateTime.mustache", "src", "DateTime.elm"));
@ -590,6 +597,28 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
addEncoderAndDecoder(parameter.vendorExtensions, parameter.dataType, isPrimitiveType ? DataTypeExposure.PRIMITIVE : DataTypeExposure.EXTERNAL);
}
@Override
public void updateCodegenPropertyEnum(final CodegenProperty property) {
super.updateCodegenPropertyEnum(property);
if (!elmPrefixCustomTypeVariants) {
return;
}
final Map<String, Object> allowableValues = property.allowableValues;
if (allowableValues == null) {
return;
}
final List<Map<String, Object>> enumVars = (ArrayList<Map<String, Object>>) allowableValues.get("enumVars");
if (enumVars == null) {
return;
}
final String prefix = toEnumName(property);
for (Map<String, Object> enumVar : enumVars) {
enumVar.put("name", prefix + enumVar.get("name"));
}
}
private boolean isPrimitiveDataType(String dataType) {
return languageSpecificPrimitives.contains(dataType);
}