Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public ElmClientCodegen() {

instantiationTypes.clear();
instantiationTypes.put("array", "List");
instantiationTypes.put("map", "Dict");

typeMapping.clear();
typeMapping.put("integer", "Int");
Expand All @@ -143,11 +144,12 @@ public ElmClientCodegen() {
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();
Expand Down Expand Up @@ -402,13 +404,17 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
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(" \\+\\+ \"\"", "");

Expand All @@ -433,7 +439,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}
}

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();
Expand All @@ -444,6 +450,22 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
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;
Expand Down Expand Up @@ -490,19 +512,24 @@ private String toOptionalValue(String value) {

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -32,3 +30,8 @@ decodeIsoString str =

Result.Err _ ->
Decode.fail <| "Invalid date: " ++ str


toString : DateOnly -> String
toString =
String.left 10 << Iso8601.fromTime
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -30,3 +30,8 @@ decodeIsoString str =

Result.Err msg ->
Decode.fail msg


toString : DateOnly -> String
toString =
toFormattedString "yyyy-MM-dd"
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -18,8 +18,8 @@ decoder =


encoder : DateTime -> Encode.Value
encoder model =
Encode.string <| Iso8601.fromTime model
encoder =
Encode.string << toString


decodeIsoString : String -> Decoder DateTime
Expand All @@ -30,3 +30,8 @@ decodeIsoString str =

Result.Err _ ->
Decode.fail <| "Invalid date: " ++ str


toString : DateTime -> String
toString =
Iso8601.fromTime
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -18,8 +18,8 @@ decoder =


encoder : DateTime -> Encode.Value
encoder model =
Encode.string <| toIsoString model
encoder =
Encode.string << toString


decodeIsoString : String -> Decoder DateTime
Expand All @@ -30,3 +30,8 @@ decodeIsoString str =

Result.Err msg ->
Decode.fail msg


toString : DateTime -> String
toString =
toIsoString
11 changes: 8 additions & 3 deletions samples/client/petstore/elm-0.18/src/DateOnly.elm
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -30,3 +30,8 @@ decodeIsoString str =

Result.Err msg ->
Decode.fail msg


toString : DateOnly -> String
toString =
toFormattedString "yyyy-MM-dd"
11 changes: 8 additions & 3 deletions samples/client/petstore/elm-0.18/src/DateTime.elm
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -18,8 +18,8 @@ decoder =


encoder : DateTime -> Encode.Value
encoder model =
Encode.string <| toIsoString model
encoder =
Encode.string << toString


decodeIsoString : String -> Decoder DateTime
Expand All @@ -30,3 +30,8 @@ decodeIsoString str =

Result.Err msg ->
Decode.fail msg


toString : DateTime -> String
toString =
toIsoString
13 changes: 8 additions & 5 deletions samples/client/petstore/elm/src/DateOnly.elm
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -32,3 +30,8 @@ decodeIsoString str =

Result.Err _ ->
Decode.fail <| "Invalid date: " ++ str


toString : DateOnly -> String
toString =
String.left 10 << Iso8601.fromTime
11 changes: 8 additions & 3 deletions samples/client/petstore/elm/src/DateTime.elm
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -18,8 +18,8 @@ decoder =


encoder : DateTime -> Encode.Value
encoder model =
Encode.string <| Iso8601.fromTime model
encoder =
Encode.string << toString


decodeIsoString : String -> Decoder DateTime
Expand All @@ -30,3 +30,8 @@ decodeIsoString str =

Result.Err _ ->
Decode.fail <| "Invalid date: " ++ str


toString : DateTime -> String
toString =
Iso8601.fromTime