mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-21 03:17:06 +00:00
Add support for enums in Elm operations (#2982)
This commit is contained in:
committed by
William Cheng
parent
d748312818
commit
38b1fe2d36
@@ -567,44 +567,56 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
return "(Just " + value + ")";
|
||||
}
|
||||
|
||||
private Optional<String> paramToStringMapper(final String paramName, final CodegenProperty property) {
|
||||
if (property.isEnum) {
|
||||
return Optional.of(toVarName(paramName) + "ToString");
|
||||
} else if (property.isString || property.isBinary || property.isByteArray) {
|
||||
return Optional.empty();
|
||||
} else if (property.isBoolean) {
|
||||
return Optional.of("(\\val -> if val then \"true\" else \"false\")");
|
||||
} else if (property.isDateTime) {
|
||||
return Optional.of("DateTime.toString");
|
||||
} else if (property.isDate) {
|
||||
return Optional.of("DateOnly.toString");
|
||||
} else if (property.isUuid) {
|
||||
return Optional.of("Uuid.toString");
|
||||
} else if (ElmVersion.ELM_018.equals(elmVersion)) {
|
||||
return Optional.of("toString");
|
||||
} else if (property.isInteger || property.isLong) {
|
||||
return Optional.of("String.fromInt");
|
||||
} else if (property.isFloat || property.isDouble) {
|
||||
return Optional.of("String.fromFloat");
|
||||
}
|
||||
throw new RuntimeException("Parameter '" + paramName + "' cannot be converted to a string. Please report the issue.");
|
||||
}
|
||||
|
||||
private CodegenProperty paramToProperty(final CodegenParameter parameter) {
|
||||
final CodegenProperty property = new CodegenProperty();
|
||||
property.isEnum = parameter.isEnum;
|
||||
property.isString = parameter.isString;
|
||||
property.isBinary = parameter.isBinary;
|
||||
property.isByteArray = parameter.isByteArray;
|
||||
property.isBoolean = parameter.isBoolean;
|
||||
property.isDateTime = parameter.isDateTime;
|
||||
property.isDate = parameter.isDate;
|
||||
property.isUuid = parameter.isUuid;
|
||||
property.isInteger = parameter.isInteger;
|
||||
property.isLong = parameter.isLong;
|
||||
property.isFloat = parameter.isFloat;
|
||||
property.isDouble = parameter.isDouble;
|
||||
return property;
|
||||
}
|
||||
|
||||
private String paramToString(final String prefix, final CodegenParameter param, final boolean useMaybe, final String maybeMapResult) {
|
||||
final String paramName = (ElmVersion.ELM_018.equals(elmVersion) ? "" : prefix + ".") + param.paramName;
|
||||
if (!useMaybe) {
|
||||
param.required = true;
|
||||
}
|
||||
|
||||
String mapFn = null;
|
||||
if (param.isString || param.isBinary || param.isByteArray) {
|
||||
mapFn = "";
|
||||
} else if (param.isBoolean) {
|
||||
mapFn = "(\\val -> if val then \"true\" else \"false\")";
|
||||
} else if (param.isDateTime) {
|
||||
mapFn = "DateTime.toString";
|
||||
} else if (param.isDate) {
|
||||
mapFn = "DateOnly.toString";
|
||||
} else if (param.isUuid) {
|
||||
mapFn = "Uuid.toString";
|
||||
} else if (ElmVersion.ELM_018.equals(elmVersion)) {
|
||||
mapFn = "toString";
|
||||
} else if (param.isInteger || param.isLong) {
|
||||
mapFn = "String.fromInt";
|
||||
} else if (param.isFloat || param.isDouble) {
|
||||
mapFn = "String.fromFloat";
|
||||
} else if (param.isListContainer) {
|
||||
// TODO duplicate ALL types from parameter to property...
|
||||
if (param.items.isString || param.items.isUuid || param.items.isBinary || param.items.isByteArray) {
|
||||
mapFn = "String.join \",\"";
|
||||
}
|
||||
}
|
||||
if (mapFn == null) {
|
||||
throw new RuntimeException("Parameter '" + param.paramName + "' cannot be converted to a string. Please report the issue.");
|
||||
}
|
||||
final String mapFn = param.isListContainer
|
||||
? "(String.join \",\"" + paramToStringMapper(param.paramName, param.items).map(mapper -> " << List.map " + mapper).orElse("") + ")"
|
||||
: paramToStringMapper(param.paramName, paramToProperty(param)).orElse("");
|
||||
|
||||
if (param.isListContainer) {
|
||||
if (!param.required) {
|
||||
mapFn = "(" + mapFn + ")";
|
||||
}
|
||||
}
|
||||
String mapResult = "";
|
||||
if (maybeMapResult != null) {
|
||||
if ("".equals(mapFn)) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{{>licenseInfo}}
|
||||
|
||||
module Request.{{classname}} exposing ({{#operations}}{{#operation}}{{^-first}}, {{/-first}}{{operationId}}{{/operation}}{{/operations}})
|
||||
module Request.{{classname}} exposing ({{#operations}}{{#operation}}{{^-first}}, {{/-first}}{{operationId}}{{#allParams}}{{#isEnum}}, {{enumName}}(..){{/isEnum}}{{/allParams}}{{/operation}}{{/operations}})
|
||||
|
||||
{{>imports}}import Dict
|
||||
import Http
|
||||
@@ -8,6 +8,29 @@ import Json.Decode as Decode
|
||||
import Url.Builder as Url
|
||||
|
||||
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
{{#allParams}}
|
||||
{{#isEnum}}
|
||||
type {{enumName}}
|
||||
{{#allowableValues.enumVars}} {{#-first}}= {{/-first}}{{^-first}}| {{/-first}}{{name}}
|
||||
{{/allowableValues.enumVars}}
|
||||
|
||||
{{paramName}}ToString : {{enumName}} -> String
|
||||
{{paramName}}ToString value =
|
||||
case value of
|
||||
{{#allowableValues.enumVars}} {{name}} ->
|
||||
{{{value}}}
|
||||
|
||||
{{/allowableValues.enumVars}}
|
||||
|
||||
|
||||
{{/isEnum}}
|
||||
{{/allParams}}
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
|
||||
|
||||
{{^enableCustomBasePaths}}basePath : String
|
||||
basePath =
|
||||
"{{basePath}}"
|
||||
@@ -29,8 +52,8 @@ basePath =
|
||||
{{#enableCustomBasePaths}} , basePath : String{{/enableCustomBasePaths}}
|
||||
{{#enableHttpRequestTrackers}} , tracker : Maybe String{{/enableHttpRequestTrackers}}
|
||||
{{#bodyParam}} , body : {{^required}}Maybe {{/required}}{{dataType}}{{/bodyParam}}
|
||||
{{#pathParams}} , {{paramName}} : {{#isListContainer}}List {{/isListContainer}}{{dataType}}{{/pathParams}}
|
||||
{{#queryParams}} , {{paramName}} : {{^required}}Maybe ({{/required}}{{#isListContainer}}List {{/isListContainer}}{{dataType}}{{^required}}){{/required}}{{/queryParams}}
|
||||
{{#pathParams}} , {{paramName}} : {{#isListContainer}}List {{/isListContainer}}{{#datatypeWithEnum}}{{datatypeWithEnum}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{dataType}}{{/datatypeWithEnum}}{{/pathParams}}
|
||||
{{#queryParams}} , {{paramName}} : {{^required}}Maybe ({{/required}}{{#isListContainer}}List {{/isListContainer}}{{#datatypeWithEnum}}{{datatypeWithEnum}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{dataType}}{{/datatypeWithEnum}}{{^required}}){{/required}}{{/queryParams}}
|
||||
}
|
||||
-> Cmd msg
|
||||
{{operationId}} {{#headerParams.0}}headers {{/headerParams.0}}params =
|
||||
|
||||
Reference in New Issue
Block a user