Skip to content

Update Elm for elm/http 2.0.0 #1510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2018
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 @@ -88,7 +88,6 @@ public ElmClientCodegen() {
super();
outputFolder = "generated-code/elm";
modelTemplateFiles.put("model.mustache", ".elm");
apiTemplateFiles.put("api.mustache", ".elm");
templateDir = "elm";

supportsInheritance = true;
Expand Down Expand Up @@ -198,6 +197,7 @@ public void processOpts() {
case ELM_018:
LOGGER.info("Elm version: 0.18");
additionalProperties.put("isElm018", true);
apiTemplateFiles.put("api018.mustache", ".elm");
supportingFiles.add(new SupportingFile("DateOnly018.mustache", "src", "DateOnly.elm"));
supportingFiles.add(new SupportingFile("DateTime018.mustache", "src", "DateTime.elm"));
supportingFiles.add(new SupportingFile("elm-package018.mustache", "", "elm-package.json"));
Expand All @@ -206,6 +206,7 @@ public void processOpts() {
case ELM_019:
LOGGER.info("Elm version: 0.19");
additionalProperties.put("isElm019", true);
apiTemplateFiles.put("api.mustache", ".elm");
supportingFiles.add(new SupportingFile("DateOnly.mustache", "src", "DateOnly.elm"));
supportingFiles.add(new SupportingFile("DateTime.mustache", "src", "DateTime.elm"));
supportingFiles.add(new SupportingFile("elm.mustache", "", "elm.json"));
Expand Down Expand Up @@ -423,14 +424,33 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
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;
if (ElmVersion.ELM_018.equals(elmVersion)) {
String path = op.path;
for (CodegenParameter param : op.pathParams) {
final String var = paramToString(param, false, null);
path = path.replace("{" + param.paramName + "}", "\" ++ " + var + " ++ \"");
hasDateTime = hasDateTime || param.isDateTime;
hasDate = hasDate || param.isDate;
}
op.path = ("\"" + path + "\"").replaceAll(" \\+\\+ \"\"", "");
} else {
final List<String> paths = Arrays.asList(op.path.substring(1).split("/"));
String path = paths.stream().map(str -> str.charAt(0) == '{' ? str : "\"" + str + "\"").collect(Collectors.joining(", "));
for (CodegenParameter param : op.pathParams) {
String str = paramToString(param, false, null);
path = path.replace("{" + param.paramName + "}", str);
hasDateTime = hasDateTime || param.isDateTime;
hasDate = hasDate || param.isDate;
}
op.path = path;

final String query = op.queryParams.stream()
.map(param -> paramToString(param, true, "Url.string \"" + param.paramName + "\""))
.collect(Collectors.joining(", "));
op.vendorExtensions.put("query", query);
// TODO headers
// TODO forms
}
op.path = ("\"" + path + "\"").replaceAll(" \\+\\+ \"\"", "");

if (op.bodyParam != null && !op.bodyParam.isPrimitiveType && !op.bodyParam.isMapContainer) {
final String encoder = (String) op.bodyParam.vendorExtensions.get(ENCODER);
Expand Down Expand Up @@ -523,26 +543,49 @@ private String toOptionalValue(String value) {
return "(Just " + value + ")";
}

private String paramToString(final CodegenParameter param) {
final String paramName = param.paramName;
private String paramToString(final CodegenParameter param, final boolean useMaybe, final String maybeMapResult) {
final String paramName = (ElmVersion.ELM_018.equals(elmVersion) ? "" : "params.") + param.paramName;
if (!useMaybe) {
param.required = true;
}

String mapFn = null;
if (param.isString || param.isUuid || param.isBinary || param.isByteArray) {
return paramName;
mapFn = "";
} else if (param.isBoolean) {
return "if " + paramName + " then \"true\" else \"false\"";
mapFn = "(\\val -> if val then \"true\" else \"false\")";
} else if (param.isDateTime) {
return "DateTime.toString " + paramName;
mapFn = "DateTime.toString";
} else if (param.isDate) {
return "DateOnly.toString " + paramName;
mapFn = "DateOnly.toString";
} else if (ElmVersion.ELM_018.equals(elmVersion)) {
return "toString " + paramName;
mapFn = "toString";
} else if (param.isInteger || param.isLong) {
return "String.fromInt " + paramName;
mapFn = "String.fromInt";
} else if (param.isFloat || param.isDouble) {
return "String.fromFloat " + paramName;
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 \",\"";
}
}

throw new RuntimeException("Parameter '" + paramName + "' cannot be converted to a string. Please report the issue.");
if (mapFn == null) {
throw new RuntimeException("Parameter '" + param.paramName + "' cannot be converted to a string. Please report the issue.");
}

if (param.isListContainer) {
if (!param.required) {
mapFn = "(" + mapFn + ")";
}
}
String mapResult = "";
if (maybeMapResult != null) {
mapResult = maybeMapResult + (param.required ? " <|" : " <<");
}
final String just = useMaybe ? "Just (" : "";
final String justEnd = useMaybe ? ")" : "";
return (param.required ? just : "Maybe.map") + mapResult + " " + mapFn + " " + paramName + (param.required ? justEnd : "");
}

@Override
Expand Down
29 changes: 19 additions & 10 deletions modules/openapi-generator/src/main/resources/elm/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Request.{{classname}} exposing ({{#operations}}{{#operation}}{{^-first}},
{{>imports}}import Dict
import Http
import Json.Decode as Decode
import Url.Builder as Url


basePath : String
Expand All @@ -18,16 +19,24 @@ basePath =
{-| {{{notes}}}
-}
{{/notes}}
{{operationId}} : {{#pathParams}}{{dataType}} -> {{/pathParams}}{{#bodyParam}}{{dataType}} -> {{/bodyParam}}Http.Request {{^responses}}(){{/responses}}{{#responses}}{{#-first}}{{^dataType}}(){{/dataType}}{{#isMapContainer}}(Dict.Dict String {{/isMapContainer}}{{#isListContainer}}(List {{/isListContainer}}{{dataType}}{{#isListContainer}}){{/isListContainer}}{{#isMapContainer}}){{/isMapContainer}}{{/-first}}{{/responses}}
{{operationId}} {{#pathParams}}{{paramName}} {{/pathParams}}{{#bodyParam}}model {{/bodyParam}}=
{ method = "{{httpMethod}}"
, url = basePath ++ {{{path}}}
, headers = []
, body = {{#bodyParam}}Http.jsonBody <| {{vendorExtensions.elmEncoder}} model{{/bodyParam}}{{^bodyParam}}Http.emptyBody{{/bodyParam}}
, expect = {{^responses}}Http.expectStringResponse (\_ -> Ok ()){{/responses}}{{#responses}}{{#-first}}{{^dataType}}Http.expectStringResponse (\_ -> Ok ()){{/dataType}}{{#dataType}}Http.expectJson {{#isMapContainer}}(Decode.dict {{/isMapContainer}}{{#isListContainer}}(Decode.list {{/isListContainer}}{{#vendorExtensions}}{{elmDecoder}}{{/vendorExtensions}}{{#isListContainer}}){{/isListContainer}}{{#isMapContainer}}){{/isMapContainer}}{{/dataType}}{{/-first}}{{/responses}}
, timeout = Just 30000
, withCredentials = False
{{operationId}} :
{ onSend : Result Http.Error {{^responses}}(){{/responses}}{{#responses}}{{#-first}}{{^dataType}}(){{/dataType}}{{#isMapContainer}}(Dict.Dict String {{/isMapContainer}}{{#isListContainer}}(List {{/isListContainer}}{{dataType}}{{#isListContainer}}){{/isListContainer}}{{#isMapContainer}}){{/isMapContainer}}{{/-first}}{{/responses}} -> msg
{{#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}}
}
|> Http.request
-> Cmd msg
{{operationId}} params =
Http.request
{ method = "{{httpMethod}}"
, headers = []
, url = Url.crossOrigin basePath
[{{{path}}}]
(List.filterMap identity [{{{vendorExtensions.query}}}])
, body = {{#bodyParam}}{{^required}}Maybe.withDefault Http.emptyBody <| Maybe.map ({{/required}}Http.jsonBody {{#required}}<|{{/required}}{{^required}}<<{{/required}} {{vendorExtensions.elmEncoder}}{{^required}}){{/required}} params.body{{/bodyParam}}{{^bodyParam}}Http.emptyBody{{/bodyParam}}
, expect = {{^responses}}Http.expectWhatever params.onSend{{/responses}}{{#responses}}{{#-first}}{{^dataType}}Http.expectWhatever params.onSend{{/dataType}}{{#dataType}}Http.expectJson params.onSend {{#isMapContainer}}(Decode.dict {{/isMapContainer}}{{#isListContainer}}(Decode.list {{/isListContainer}}{{#vendorExtensions}}{{elmDecoder}}{{/vendorExtensions}}{{#isListContainer}}){{/isListContainer}}{{#isMapContainer}}){{/isMapContainer}}{{/dataType}}{{/-first}}{{/responses}}
, timeout = Just 30000
, tracker = Nothing
}
{{/operation}}
{{/operations}}
33 changes: 33 additions & 0 deletions modules/openapi-generator/src/main/resources/elm/api018.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{>licenseInfo}}

module Request.{{classname}} exposing ({{#operations}}{{#operation}}{{^-first}}, {{/-first}}{{operationId}}{{/operation}}{{/operations}})

{{>imports}}import Dict
import Http
import Json.Decode as Decode


basePath : String
basePath =
"{{basePath}}"
{{#operations}}
{{#operation}}


{{#notes}}
{-| {{{notes}}}
-}
{{/notes}}
{{operationId}} : {{#pathParams}}{{dataType}} -> {{/pathParams}}{{#bodyParam}}{{dataType}} -> {{/bodyParam}}Http.Request {{^responses}}(){{/responses}}{{#responses}}{{#-first}}{{^dataType}}(){{/dataType}}{{#isMapContainer}}(Dict.Dict String {{/isMapContainer}}{{#isListContainer}}(List {{/isListContainer}}{{dataType}}{{#isListContainer}}){{/isListContainer}}{{#isMapContainer}}){{/isMapContainer}}{{/-first}}{{/responses}}
{{operationId}} {{#pathParams}}{{paramName}} {{/pathParams}}{{#bodyParam}}model {{/bodyParam}}=
{ method = "{{httpMethod}}"
, url = basePath ++ {{{path}}}
, headers = []
, body = {{#bodyParam}}Http.jsonBody <| {{vendorExtensions.elmEncoder}} model{{/bodyParam}}{{^bodyParam}}Http.emptyBody{{/bodyParam}}
, expect = {{^responses}}Http.expectStringResponse (\_ -> Ok ()){{/responses}}{{#responses}}{{#-first}}{{^dataType}}Http.expectStringResponse (\_ -> Ok ()){{/dataType}}{{#dataType}}Http.expectJson {{#isMapContainer}}(Decode.dict {{/isMapContainer}}{{#isListContainer}}(Decode.list {{/isListContainer}}{{#vendorExtensions}}{{elmDecoder}}{{/vendorExtensions}}{{#isListContainer}}){{/isListContainer}}{{#isMapContainer}}){{/isMapContainer}}{{/dataType}}{{/-first}}{{/responses}}
, timeout = Just 30000
, withCredentials = False
}
|> Http.request
{{/operation}}
{{/operations}}
18 changes: 10 additions & 8 deletions modules/openapi-generator/src/main/resources/elm/elm.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
"dependencies": {
"direct": {
"NoRedInk/elm-json-decode-pipeline": "1.0.0",
"elm/browser": "1.0.0",
"elm/core": "1.0.0",
"elm/browser": "1.0.1",
"elm/core": "1.0.2",
"elm/html": "1.0.0",
"elm/http": "1.0.0",
"elm/json": "1.0.0",
"elm/http": "2.0.0",
"elm/json": "1.1.2",
"elm/time": "1.0.0",
"rtfeldman/elm-iso8601-date-strings": "1.0.0"
"elm/url": "1.0.0",
"rtfeldman/elm-iso8601-date-strings": "1.1.2"
},
"indirect": {
"elm/parser": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.0"
"elm/bytes": "1.0.5",
"elm/file": "1.0.1",
"elm/parser": "1.1.0",
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.2-SNAPSHOT
4.0.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore/elm/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.2-SNAPSHOT
4.0.0-SNAPSHOT
18 changes: 10 additions & 8 deletions samples/client/petstore/elm/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
"dependencies": {
"direct": {
"NoRedInk/elm-json-decode-pipeline": "1.0.0",
"elm/browser": "1.0.0",
"elm/core": "1.0.0",
"elm/browser": "1.0.1",
"elm/core": "1.0.2",
"elm/html": "1.0.0",
"elm/http": "1.0.0",
"elm/json": "1.0.0",
"elm/http": "2.0.0",
"elm/json": "1.1.2",
"elm/time": "1.0.0",
"rtfeldman/elm-iso8601-date-strings": "1.0.0"
"elm/url": "1.0.0",
"rtfeldman/elm-iso8601-date-strings": "1.1.2"
},
"indirect": {
"elm/parser": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.0"
"elm/bytes": "1.0.5",
"elm/file": "1.0.1",
"elm/parser": "1.1.0",
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
Expand Down
Loading