forked from loafle/openapi-generator-original
Better handling of different types in Elm generator (#1100)
* better handling of different types in elm generator * [elm] Fix toString for special cases
This commit is contained in:
parent
2040050e55
commit
755dfe53de
@ -133,6 +133,7 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
instantiationTypes.clear();
|
||||
instantiationTypes.put("array", "List");
|
||||
instantiationTypes.put("map", "Dict");
|
||||
|
||||
typeMapping.clear();
|
||||
typeMapping.put("integer", "Int");
|
||||
@ -143,11 +144,12 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("boolean", "Bool");
|
||||
typeMapping.put("string", "String");
|
||||
typeMapping.put("array", "List");
|
||||
typeMapping.put("map", "Dict");
|
||||
typeMapping.put("date", "DateOnly");
|
||||
typeMapping.put("DateTime", "DateTime");
|
||||
typeMapping.put("password", "String");
|
||||
typeMapping.put("file", "String");
|
||||
typeMapping.put("ByteArray", "Byte");
|
||||
typeMapping.put("file", "String");
|
||||
typeMapping.put("binary", "String");
|
||||
|
||||
importMapping.clear();
|
||||
@ -402,13 +404,17 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
Map<String, Object> objs = (Map<String, Object>) operations.get("operations");
|
||||
List<CodegenOperation> ops = (List<CodegenOperation>) objs.get("operation");
|
||||
|
||||
Map<String, Set<String>> dependencies = new HashMap<>();
|
||||
boolean hasDateTime = false;
|
||||
boolean hasDate = false;
|
||||
final Map<String, Set<String>> dependencies = new HashMap<>();
|
||||
|
||||
for (CodegenOperation op : ops) {
|
||||
String path = op.path;
|
||||
for (CodegenParameter param : op.pathParams) {
|
||||
final String var = paramToString(param);
|
||||
path = path.replace("{" + param.paramName + "}", "\" ++ " + var + " ++ \"");
|
||||
hasDateTime = hasDateTime || param.isDateTime;
|
||||
hasDate = hasDate || param.isDate;
|
||||
}
|
||||
op.path = ("\"" + path + "\"").replaceAll(" \\+\\+ \"\"", "");
|
||||
|
||||
@ -433,7 +439,7 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
}
|
||||
|
||||
List<ElmImport> elmImports = new ArrayList<>();
|
||||
final List<ElmImport> elmImports = new ArrayList<>();
|
||||
for (Map.Entry<String, Set<String>> entry : dependencies.entrySet()) {
|
||||
final ElmImport elmImport = new ElmImport();
|
||||
final String key = entry.getKey();
|
||||
@ -444,6 +450,22 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
elmImport.hasExposures = true;
|
||||
elmImports.add(elmImport);
|
||||
}
|
||||
if (hasDate) {
|
||||
final ElmImport elmImport = new ElmImport();
|
||||
elmImport.moduleName = "DateOnly";
|
||||
elmImport.exposures = new TreeSet<>();
|
||||
elmImport.exposures.add("DateOnly");
|
||||
elmImport.hasExposures = true;
|
||||
elmImports.add(elmImport);
|
||||
}
|
||||
if (hasDateTime) {
|
||||
final ElmImport elmImport = new ElmImport();
|
||||
elmImport.moduleName = "DateTime";
|
||||
elmImport.exposures = new TreeSet<>();
|
||||
elmImport.exposures.add("DateTime");
|
||||
elmImport.hasExposures = true;
|
||||
elmImports.add(elmImport);
|
||||
}
|
||||
operations.put("elmImports", elmImports);
|
||||
|
||||
return operations;
|
||||
@ -490,19 +512,24 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
private String paramToString(final CodegenParameter param) {
|
||||
final String paramName = param.paramName;
|
||||
if (param.isString || param.isUuid) {
|
||||
|
||||
if (param.isString || param.isUuid || param.isBinary || param.isByteArray) {
|
||||
return paramName;
|
||||
}
|
||||
if (ElmVersion.ELM_018.equals(elmVersion)) {
|
||||
} else if (param.isBoolean) {
|
||||
return "if " + paramName + " then \"true\" else \"false\"";
|
||||
} else if (param.isDateTime) {
|
||||
return "DateTime.toString " + paramName;
|
||||
} else if (param.isDate) {
|
||||
return "DateOnly.toString " + paramName;
|
||||
} else if (ElmVersion.ELM_018.equals(elmVersion)) {
|
||||
return "toString " + paramName;
|
||||
}
|
||||
if (param.isInteger || param.isLong) {
|
||||
} else if (param.isInteger || param.isLong) {
|
||||
return "String.fromInt " + paramName;
|
||||
}
|
||||
if (param.isFloat || param.isDouble) {
|
||||
} else if (param.isFloat || param.isDouble) {
|
||||
return "String.fromFloat " + paramName;
|
||||
}
|
||||
throw new RuntimeException("Parameter '" + paramName + "' cannot be converted to a string");
|
||||
|
||||
throw new RuntimeException("Parameter '" + paramName + "' cannot be converted to a string. Please report the issue.");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,4 +1,4 @@
|
||||
module DateOnly exposing (DateOnly, decoder, encoder)
|
||||
module DateOnly exposing (DateOnly, decoder, encoder, toString)
|
||||
|
||||
import Iso8601
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
@ -18,10 +18,8 @@ decoder =
|
||||
|
||||
|
||||
encoder : DateOnly -> Encode.Value
|
||||
encoder model =
|
||||
Iso8601.fromTime model
|
||||
|> String.left 10
|
||||
|> Encode.string
|
||||
encoder =
|
||||
Encode.string << toString
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateOnly
|
||||
@ -32,3 +30,8 @@ decodeIsoString str =
|
||||
|
||||
Result.Err _ ->
|
||||
Decode.fail <| "Invalid date: " ++ str
|
||||
|
||||
|
||||
toString : DateOnly -> String
|
||||
toString =
|
||||
String.left 10 << Iso8601.fromTime
|
||||
|
@ -1,4 +1,4 @@
|
||||
module DateOnly exposing (DateOnly, decoder, encoder)
|
||||
module DateOnly exposing (DateOnly, decoder, encoder, toString)
|
||||
|
||||
import Date
|
||||
import Date.Extra exposing (fromIsoString, toFormattedString)
|
||||
@ -18,8 +18,8 @@ decoder =
|
||||
|
||||
|
||||
encoder : DateOnly -> Encode.Value
|
||||
encoder model =
|
||||
Encode.string <| toFormattedString "yyyy-MM-dd" model
|
||||
encoder =
|
||||
Encode.string << toString
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateOnly
|
||||
@ -30,3 +30,8 @@ decodeIsoString str =
|
||||
|
||||
Result.Err msg ->
|
||||
Decode.fail msg
|
||||
|
||||
|
||||
toString : DateOnly -> String
|
||||
toString =
|
||||
toFormattedString "yyyy-MM-dd"
|
||||
|
@ -1,4 +1,4 @@
|
||||
module DateTime exposing (DateTime, decoder, encoder)
|
||||
module DateTime exposing (DateTime, decoder, encoder, toString)
|
||||
|
||||
import Iso8601
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
@ -18,8 +18,8 @@ decoder =
|
||||
|
||||
|
||||
encoder : DateTime -> Encode.Value
|
||||
encoder model =
|
||||
Encode.string <| Iso8601.fromTime model
|
||||
encoder =
|
||||
Encode.string << toString
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateTime
|
||||
@ -30,3 +30,8 @@ decodeIsoString str =
|
||||
|
||||
Result.Err _ ->
|
||||
Decode.fail <| "Invalid date: " ++ str
|
||||
|
||||
|
||||
toString : DateTime -> String
|
||||
toString =
|
||||
Iso8601.fromTime
|
@ -1,4 +1,4 @@
|
||||
module DateTime exposing (DateTime, decoder, encoder)
|
||||
module DateTime exposing (DateTime, decoder, encoder, toString)
|
||||
|
||||
import Date
|
||||
import Date.Extra exposing (fromIsoString, toIsoString)
|
||||
@ -18,8 +18,8 @@ decoder =
|
||||
|
||||
|
||||
encoder : DateTime -> Encode.Value
|
||||
encoder model =
|
||||
Encode.string <| toIsoString model
|
||||
encoder =
|
||||
Encode.string << toString
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateTime
|
||||
@ -30,3 +30,8 @@ decodeIsoString str =
|
||||
|
||||
Result.Err msg ->
|
||||
Decode.fail msg
|
||||
|
||||
|
||||
toString : DateTime -> String
|
||||
toString =
|
||||
toIsoString
|
@ -1,4 +1,4 @@
|
||||
module DateOnly exposing (DateOnly, decoder, encoder)
|
||||
module DateOnly exposing (DateOnly, decoder, encoder, toString)
|
||||
|
||||
import Date
|
||||
import Date.Extra exposing (fromIsoString, toFormattedString)
|
||||
@ -18,8 +18,8 @@ decoder =
|
||||
|
||||
|
||||
encoder : DateOnly -> Encode.Value
|
||||
encoder model =
|
||||
Encode.string <| toFormattedString "yyyy-MM-dd" model
|
||||
encoder =
|
||||
Encode.string << toString
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateOnly
|
||||
@ -30,3 +30,8 @@ decodeIsoString str =
|
||||
|
||||
Result.Err msg ->
|
||||
Decode.fail msg
|
||||
|
||||
|
||||
toString : DateOnly -> String
|
||||
toString =
|
||||
toFormattedString "yyyy-MM-dd"
|
||||
|
@ -1,4 +1,4 @@
|
||||
module DateTime exposing (DateTime, decoder, encoder)
|
||||
module DateTime exposing (DateTime, decoder, encoder, toString)
|
||||
|
||||
import Date
|
||||
import Date.Extra exposing (fromIsoString, toIsoString)
|
||||
@ -18,8 +18,8 @@ decoder =
|
||||
|
||||
|
||||
encoder : DateTime -> Encode.Value
|
||||
encoder model =
|
||||
Encode.string <| toIsoString model
|
||||
encoder =
|
||||
Encode.string << toString
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateTime
|
||||
@ -30,3 +30,8 @@ decodeIsoString str =
|
||||
|
||||
Result.Err msg ->
|
||||
Decode.fail msg
|
||||
|
||||
|
||||
toString : DateTime -> String
|
||||
toString =
|
||||
toIsoString
|
||||
|
@ -1,4 +1,4 @@
|
||||
module DateOnly exposing (DateOnly, decoder, encoder)
|
||||
module DateOnly exposing (DateOnly, decoder, encoder, toString)
|
||||
|
||||
import Iso8601
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
@ -18,10 +18,8 @@ decoder =
|
||||
|
||||
|
||||
encoder : DateOnly -> Encode.Value
|
||||
encoder model =
|
||||
Iso8601.fromTime model
|
||||
|> String.left 10
|
||||
|> Encode.string
|
||||
encoder =
|
||||
Encode.string << toString
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateOnly
|
||||
@ -32,3 +30,8 @@ decodeIsoString str =
|
||||
|
||||
Result.Err _ ->
|
||||
Decode.fail <| "Invalid date: " ++ str
|
||||
|
||||
|
||||
toString : DateOnly -> String
|
||||
toString =
|
||||
String.left 10 << Iso8601.fromTime
|
||||
|
@ -1,4 +1,4 @@
|
||||
module DateTime exposing (DateTime, decoder, encoder)
|
||||
module DateTime exposing (DateTime, decoder, encoder, toString)
|
||||
|
||||
import Iso8601
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
@ -18,8 +18,8 @@ decoder =
|
||||
|
||||
|
||||
encoder : DateTime -> Encode.Value
|
||||
encoder model =
|
||||
Encode.string <| Iso8601.fromTime model
|
||||
encoder =
|
||||
Encode.string << toString
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateTime
|
||||
@ -30,3 +30,8 @@ decodeIsoString str =
|
||||
|
||||
Result.Err _ ->
|
||||
Decode.fail <| "Invalid date: " ++ str
|
||||
|
||||
|
||||
toString : DateTime -> String
|
||||
toString =
|
||||
Iso8601.fromTime
|
||||
|
Loading…
x
Reference in New Issue
Block a user