Skip to content

Commit

Permalink
Use elm-format to format Elm API client (OpenAPITools#959)
Browse files Browse the repository at this point in the history
* add elm-format support

* update elm petstore samples

* add trenneman to elm tech comm

* replace dart class with elm class

* revise elm format arguments

* update petstore samples
  • Loading branch information
wing328 authored Sep 5, 2018
1 parent 5d52bd5 commit 26591f5
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 91 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ If you want to join the committee, please kindly apply by sending an email to te
| Dart | @ircecho (2017/07) @swipesight (2018/09) |
| Eiffel | @jvelilla (2017/09) |
| Elixir | |
| Elm | |
| Elm | @trenneman (2018/09) |
| Erlang | @tsloughter (2017/11) |
| Go | @antihax (2017/11) @bvwells (2017/12) @grokify (2018/07) |
| Groovy | |
Expand Down
5 changes: 5 additions & 0 deletions bin/elm-petstore-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

./bin/elm-0.18-petstore.sh
./bin/elm-petstore.sh

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.utils.ModelUtils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.io.FilenameUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.text.Collator;
import java.util.ArrayList;
Expand All @@ -48,21 +54,20 @@
import java.util.Set;
import java.util.TreeSet;


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 static final String ELM_VERSION = "elmVersion";
private static final String ENCODER = "elmEncoder";
private static final String DECODER = "elmDecoder";
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
private static final String UNION_TYPE = "elmUnionType";

private Set<String> customPrimitives = new HashSet<String>();

protected String packageName = "openapi";
protected String packageVersion = "1.0.0";

private ElmVersion elmVersion = ElmVersion.ELM_019;

public CodegenType getTag() {
return CodegenType.CLIENT;
}
Expand Down Expand Up @@ -160,6 +165,10 @@ public ElmClientCodegen() {
public void processOpts() {
super.processOpts();

if (StringUtils.isEmpty(System.getenv("ELM_FORMAT_PATH"))) {
LOGGER.info("Environment variable ELM_FORMAT_PATH not defined so the Elm code may not be properly formatted. To define it, try 'export ELM_FORMAT_PATH=/usr/local/bin/elm-format' (Linux/Mac)");
}

if (additionalProperties.containsKey(ELM_VERSION)) {
final String version = (String) additionalProperties.get(ELM_VERSION);
if ("0.18".equals(version)) {
Expand Down Expand Up @@ -624,4 +633,36 @@ private enum ElmVersion {
ELM_018,
ELM_019
}

@Override
public void postProcessFile(File file, String fileType) {
if (file == null) {
return;
}

String elmFmtPath = System.getenv("ELM_FORMAT_PATH");
if (StringUtils.isEmpty(elmFmtPath)) {
return; // skip if ELM_FORMAT_PATH env variable is not defined
}

// only process files with elm extension
if ("elm".equals(FilenameUtils.getExtension(file.toString()))) {
// currently only support "elm-format -w yourcode.elm"
String command = elmFmtPath + " --yes " + file.toString();
if (ElmVersion.ELM_018.equals(elmVersion)) {
command += " --elm-version=0.18";
}

try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
if (p.exitValue() != 0) {
LOGGER.error("Error running the command ({}): {}", command, p.exitValue());
}
} catch (Exception e) {
LOGGER.error("Error running the command ({}): {}", command, e.getMessage());
}
LOGGER.info("Successfully executed: " + command);
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.3-SNAPSHOT
3.3.0-SNAPSHOT
2 changes: 0 additions & 2 deletions samples/client/petstore/elm-0.18/src/Data/ApiResponse.elm
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ apiResponseDecoder =
|> optional "message" (Decode.nullable Decode.string) Nothing



apiResponseEncoder : ApiResponse -> Encode.Value
apiResponseEncoder model =
Encode.object
[ ( "code", withDefault Encode.null (map Encode.int model.code) )
, ( "type", withDefault Encode.null (map Encode.string model.type_) )
, ( "message", withDefault Encode.null (map Encode.string model.message) )
]

2 changes: 0 additions & 2 deletions samples/client/petstore/elm-0.18/src/Data/Category.elm
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ categoryDecoder =
|> optional "name" (Decode.nullable Decode.string) Nothing



categoryEncoder : Category -> Encode.Value
categoryEncoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) )
]

28 changes: 12 additions & 16 deletions samples/client/petstore/elm-0.18/src/Data/Order_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type Status
| Delivered



orderDecoder : Decoder Order_
orderDecoder =
decode Order_
Expand All @@ -49,7 +48,6 @@ orderDecoder =
|> optional "complete" (Decode.nullable Decode.bool) (Just False)



orderEncoder : Order_ -> Encode.Value
orderEncoder model =
Encode.object
Expand All @@ -65,20 +63,21 @@ orderEncoder model =
statusDecoder : Decoder Status
statusDecoder =
Decode.string
|> Decode.andThen (\str ->
case str of
"placed" ->
Decode.succeed Placed
|> Decode.andThen
(\str ->
case str of
"placed" ->
Decode.succeed Placed

"approved" ->
Decode.succeed Approved
"approved" ->
Decode.succeed Approved

"delivered" ->
Decode.succeed Delivered
"delivered" ->
Decode.succeed Delivered

other ->
Decode.fail <| "Unknown type: " ++ other
)
other ->
Decode.fail <| "Unknown type: " ++ other
)


statusEncoder : Status -> Encode.Value
Expand All @@ -92,6 +91,3 @@ statusEncoder model =

Delivered ->
Encode.string "delivered"



28 changes: 12 additions & 16 deletions samples/client/petstore/elm-0.18/src/Data/Pet.elm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type Status
| Sold



petDecoder : Decoder Pet
petDecoder =
decode Pet
Expand All @@ -50,7 +49,6 @@ petDecoder =
|> optional "status" (Decode.nullable statusDecoder) Nothing



petEncoder : Pet -> Encode.Value
petEncoder model =
Encode.object
Expand All @@ -66,20 +64,21 @@ petEncoder model =
statusDecoder : Decoder Status
statusDecoder =
Decode.string
|> Decode.andThen (\str ->
case str of
"available" ->
Decode.succeed Available
|> Decode.andThen
(\str ->
case str of
"available" ->
Decode.succeed Available

"pending" ->
Decode.succeed Pending
"pending" ->
Decode.succeed Pending

"sold" ->
Decode.succeed Sold
"sold" ->
Decode.succeed Sold

other ->
Decode.fail <| "Unknown type: " ++ other
)
other ->
Decode.fail <| "Unknown type: " ++ other
)


statusEncoder : Status -> Encode.Value
Expand All @@ -93,6 +92,3 @@ statusEncoder model =

Sold ->
Encode.string "sold"



2 changes: 0 additions & 2 deletions samples/client/petstore/elm-0.18/src/Data/Tag.elm
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ tagDecoder =
|> optional "name" (Decode.nullable Decode.string) Nothing



tagEncoder : Tag -> Encode.Value
tagEncoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) )
]

2 changes: 0 additions & 2 deletions samples/client/petstore/elm-0.18/src/Data/User.elm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ userDecoder =
|> optional "userStatus" (Decode.nullable Decode.int) Nothing



userEncoder : User -> Encode.Value
userEncoder model =
Encode.object
Expand All @@ -58,4 +57,3 @@ userEncoder model =
, ( "phone", withDefault Encode.null (map Encode.string model.phone) )
, ( "userStatus", withDefault Encode.null (map Encode.int model.userStatus) )
]

2 changes: 1 addition & 1 deletion samples/client/petstore/elm-0.18/src/Request/Pet.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

module Request.Pet exposing (addPet, deletePet, findPetsByStatus, findPetsByTags, getPetById, updatePet, updatePetWithForm, uploadFile)

import Data.Pet exposing (Pet, petDecoder, petEncoder)
import Data.ApiResponse exposing (ApiResponse, apiResponseDecoder)
import Data.Pet exposing (Pet, petDecoder, petEncoder)
import Dict
import Http
import Json.Decode as Decode
Expand Down
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.2.3-SNAPSHOT
3.3.0-SNAPSHOT
2 changes: 0 additions & 2 deletions samples/client/petstore/elm/src/Data/ApiResponse.elm
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ apiResponseDecoder =
|> optional "message" (Decode.nullable Decode.string) Nothing



apiResponseEncoder : ApiResponse -> Encode.Value
apiResponseEncoder model =
Encode.object
[ ( "code", withDefault Encode.null (map Encode.int model.code) )
, ( "type", withDefault Encode.null (map Encode.string model.type_) )
, ( "message", withDefault Encode.null (map Encode.string model.message) )
]

2 changes: 0 additions & 2 deletions samples/client/petstore/elm/src/Data/Category.elm
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ categoryDecoder =
|> optional "name" (Decode.nullable Decode.string) Nothing



categoryEncoder : Category -> Encode.Value
categoryEncoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) )
]

28 changes: 12 additions & 16 deletions samples/client/petstore/elm/src/Data/Order_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type Status
| Delivered



orderDecoder : Decoder Order_
orderDecoder =
Decode.succeed Order_
Expand All @@ -49,7 +48,6 @@ orderDecoder =
|> optional "complete" (Decode.nullable Decode.bool) (Just False)



orderEncoder : Order_ -> Encode.Value
orderEncoder model =
Encode.object
Expand All @@ -65,20 +63,21 @@ orderEncoder model =
statusDecoder : Decoder Status
statusDecoder =
Decode.string
|> Decode.andThen (\str ->
case str of
"placed" ->
Decode.succeed Placed
|> Decode.andThen
(\str ->
case str of
"placed" ->
Decode.succeed Placed

"approved" ->
Decode.succeed Approved
"approved" ->
Decode.succeed Approved

"delivered" ->
Decode.succeed Delivered
"delivered" ->
Decode.succeed Delivered

other ->
Decode.fail <| "Unknown type: " ++ other
)
other ->
Decode.fail <| "Unknown type: " ++ other
)


statusEncoder : Status -> Encode.Value
Expand All @@ -92,6 +91,3 @@ statusEncoder model =

Delivered ->
Encode.string "delivered"



Loading

0 comments on commit 26591f5

Please sign in to comment.