Use elm-format to format Elm API client (#959)

* 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
This commit is contained in:
William Cheng 2018-09-05 08:04:27 +08:00 committed by GitHub
parent 5d52bd51bf
commit 26591f5d7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 105 additions and 91 deletions

View File

@ -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) | | Dart | @ircecho (2017/07) @swipesight (2018/09) |
| Eiffel | @jvelilla (2017/09) | | Eiffel | @jvelilla (2017/09) |
| Elixir | | | Elixir | |
| Elm | | | Elm | @trenneman (2018/09) |
| Erlang | @tsloughter (2017/11) | | Erlang | @tsloughter (2017/11) |
| Go | @antihax (2017/11) @bvwells (2017/12) @grokify (2018/07) | | Go | @antihax (2017/11) @bvwells (2017/12) @grokify (2018/07) |
| Groovy | | | Groovy | |

5
bin/elm-petstore-all.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
./bin/elm-0.18-petstore.sh
./bin/elm-petstore.sh

View File

@ -34,6 +34,12 @@ import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.SupportingFile; import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.utils.ModelUtils; 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.io.File;
import java.text.Collator; import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
@ -48,21 +54,20 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig { 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 ELM_VERSION = "elmVersion";
private static final String ENCODER = "elmEncoder"; private static final String ENCODER = "elmEncoder";
private static final String DECODER = "elmDecoder"; private static final String DECODER = "elmDecoder";
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value"; private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
private static final String UNION_TYPE = "elmUnionType"; private static final String UNION_TYPE = "elmUnionType";
private Set<String> customPrimitives = new HashSet<String>();
protected String packageName = "openapi"; protected String packageName = "openapi";
protected String packageVersion = "1.0.0"; protected String packageVersion = "1.0.0";
private ElmVersion elmVersion = ElmVersion.ELM_019;
public CodegenType getTag() { public CodegenType getTag() {
return CodegenType.CLIENT; return CodegenType.CLIENT;
} }
@ -160,6 +165,10 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
public void processOpts() { public void processOpts() {
super.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)) { if (additionalProperties.containsKey(ELM_VERSION)) {
final String version = (String) additionalProperties.get(ELM_VERSION); final String version = (String) additionalProperties.get(ELM_VERSION);
if ("0.18".equals(version)) { if ("0.18".equals(version)) {
@ -624,4 +633,36 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
ELM_018, ELM_018,
ELM_019 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);
}
}
} }

View File

@ -1 +1 @@
3.2.3-SNAPSHOT 3.3.0-SNAPSHOT

View File

@ -35,7 +35,6 @@ apiResponseDecoder =
|> optional "message" (Decode.nullable Decode.string) Nothing |> optional "message" (Decode.nullable Decode.string) Nothing
apiResponseEncoder : ApiResponse -> Encode.Value apiResponseEncoder : ApiResponse -> Encode.Value
apiResponseEncoder model = apiResponseEncoder model =
Encode.object Encode.object
@ -43,4 +42,3 @@ apiResponseEncoder model =
, ( "type", withDefault Encode.null (map Encode.string model.type_) ) , ( "type", withDefault Encode.null (map Encode.string model.type_) )
, ( "message", withDefault Encode.null (map Encode.string model.message) ) , ( "message", withDefault Encode.null (map Encode.string model.message) )
] ]

View File

@ -33,11 +33,9 @@ categoryDecoder =
|> optional "name" (Decode.nullable Decode.string) Nothing |> optional "name" (Decode.nullable Decode.string) Nothing
categoryEncoder : Category -> Encode.Value categoryEncoder : Category -> Encode.Value
categoryEncoder model = categoryEncoder model =
Encode.object Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) ) [ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) ) , ( "name", withDefault Encode.null (map Encode.string model.name) )
] ]

View File

@ -37,7 +37,6 @@ type Status
| Delivered | Delivered
orderDecoder : Decoder Order_ orderDecoder : Decoder Order_
orderDecoder = orderDecoder =
decode Order_ decode Order_
@ -49,7 +48,6 @@ orderDecoder =
|> optional "complete" (Decode.nullable Decode.bool) (Just False) |> optional "complete" (Decode.nullable Decode.bool) (Just False)
orderEncoder : Order_ -> Encode.Value orderEncoder : Order_ -> Encode.Value
orderEncoder model = orderEncoder model =
Encode.object Encode.object
@ -65,20 +63,21 @@ orderEncoder model =
statusDecoder : Decoder Status statusDecoder : Decoder Status
statusDecoder = statusDecoder =
Decode.string Decode.string
|> Decode.andThen (\str -> |> Decode.andThen
case str of (\str ->
"placed" -> case str of
Decode.succeed Placed "placed" ->
Decode.succeed Placed
"approved" -> "approved" ->
Decode.succeed Approved Decode.succeed Approved
"delivered" -> "delivered" ->
Decode.succeed Delivered Decode.succeed Delivered
other -> other ->
Decode.fail <| "Unknown type: " ++ other Decode.fail <| "Unknown type: " ++ other
) )
statusEncoder : Status -> Encode.Value statusEncoder : Status -> Encode.Value
@ -92,6 +91,3 @@ statusEncoder model =
Delivered -> Delivered ->
Encode.string "delivered" Encode.string "delivered"

View File

@ -38,7 +38,6 @@ type Status
| Sold | Sold
petDecoder : Decoder Pet petDecoder : Decoder Pet
petDecoder = petDecoder =
decode Pet decode Pet
@ -50,7 +49,6 @@ petDecoder =
|> optional "status" (Decode.nullable statusDecoder) Nothing |> optional "status" (Decode.nullable statusDecoder) Nothing
petEncoder : Pet -> Encode.Value petEncoder : Pet -> Encode.Value
petEncoder model = petEncoder model =
Encode.object Encode.object
@ -66,20 +64,21 @@ petEncoder model =
statusDecoder : Decoder Status statusDecoder : Decoder Status
statusDecoder = statusDecoder =
Decode.string Decode.string
|> Decode.andThen (\str -> |> Decode.andThen
case str of (\str ->
"available" -> case str of
Decode.succeed Available "available" ->
Decode.succeed Available
"pending" -> "pending" ->
Decode.succeed Pending Decode.succeed Pending
"sold" -> "sold" ->
Decode.succeed Sold Decode.succeed Sold
other -> other ->
Decode.fail <| "Unknown type: " ++ other Decode.fail <| "Unknown type: " ++ other
) )
statusEncoder : Status -> Encode.Value statusEncoder : Status -> Encode.Value
@ -93,6 +92,3 @@ statusEncoder model =
Sold -> Sold ->
Encode.string "sold" Encode.string "sold"

View File

@ -33,11 +33,9 @@ tagDecoder =
|> optional "name" (Decode.nullable Decode.string) Nothing |> optional "name" (Decode.nullable Decode.string) Nothing
tagEncoder : Tag -> Encode.Value tagEncoder : Tag -> Encode.Value
tagEncoder model = tagEncoder model =
Encode.object Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) ) [ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) ) , ( "name", withDefault Encode.null (map Encode.string model.name) )
] ]

View File

@ -45,7 +45,6 @@ userDecoder =
|> optional "userStatus" (Decode.nullable Decode.int) Nothing |> optional "userStatus" (Decode.nullable Decode.int) Nothing
userEncoder : User -> Encode.Value userEncoder : User -> Encode.Value
userEncoder model = userEncoder model =
Encode.object Encode.object
@ -58,4 +57,3 @@ userEncoder model =
, ( "phone", withDefault Encode.null (map Encode.string model.phone) ) , ( "phone", withDefault Encode.null (map Encode.string model.phone) )
, ( "userStatus", withDefault Encode.null (map Encode.int model.userStatus) ) , ( "userStatus", withDefault Encode.null (map Encode.int model.userStatus) )
] ]

View File

@ -12,8 +12,8 @@
module Request.Pet exposing (addPet, deletePet, findPetsByStatus, findPetsByTags, getPetById, updatePet, updatePetWithForm, uploadFile) 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.ApiResponse exposing (ApiResponse, apiResponseDecoder)
import Data.Pet exposing (Pet, petDecoder, petEncoder)
import Dict import Dict
import Http import Http
import Json.Decode as Decode import Json.Decode as Decode

View File

@ -1 +1 @@
3.2.3-SNAPSHOT 3.3.0-SNAPSHOT

View File

@ -35,7 +35,6 @@ apiResponseDecoder =
|> optional "message" (Decode.nullable Decode.string) Nothing |> optional "message" (Decode.nullable Decode.string) Nothing
apiResponseEncoder : ApiResponse -> Encode.Value apiResponseEncoder : ApiResponse -> Encode.Value
apiResponseEncoder model = apiResponseEncoder model =
Encode.object Encode.object
@ -43,4 +42,3 @@ apiResponseEncoder model =
, ( "type", withDefault Encode.null (map Encode.string model.type_) ) , ( "type", withDefault Encode.null (map Encode.string model.type_) )
, ( "message", withDefault Encode.null (map Encode.string model.message) ) , ( "message", withDefault Encode.null (map Encode.string model.message) )
] ]

View File

@ -33,11 +33,9 @@ categoryDecoder =
|> optional "name" (Decode.nullable Decode.string) Nothing |> optional "name" (Decode.nullable Decode.string) Nothing
categoryEncoder : Category -> Encode.Value categoryEncoder : Category -> Encode.Value
categoryEncoder model = categoryEncoder model =
Encode.object Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) ) [ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) ) , ( "name", withDefault Encode.null (map Encode.string model.name) )
] ]

View File

@ -37,7 +37,6 @@ type Status
| Delivered | Delivered
orderDecoder : Decoder Order_ orderDecoder : Decoder Order_
orderDecoder = orderDecoder =
Decode.succeed Order_ Decode.succeed Order_
@ -49,7 +48,6 @@ orderDecoder =
|> optional "complete" (Decode.nullable Decode.bool) (Just False) |> optional "complete" (Decode.nullable Decode.bool) (Just False)
orderEncoder : Order_ -> Encode.Value orderEncoder : Order_ -> Encode.Value
orderEncoder model = orderEncoder model =
Encode.object Encode.object
@ -65,20 +63,21 @@ orderEncoder model =
statusDecoder : Decoder Status statusDecoder : Decoder Status
statusDecoder = statusDecoder =
Decode.string Decode.string
|> Decode.andThen (\str -> |> Decode.andThen
case str of (\str ->
"placed" -> case str of
Decode.succeed Placed "placed" ->
Decode.succeed Placed
"approved" -> "approved" ->
Decode.succeed Approved Decode.succeed Approved
"delivered" -> "delivered" ->
Decode.succeed Delivered Decode.succeed Delivered
other -> other ->
Decode.fail <| "Unknown type: " ++ other Decode.fail <| "Unknown type: " ++ other
) )
statusEncoder : Status -> Encode.Value statusEncoder : Status -> Encode.Value
@ -92,6 +91,3 @@ statusEncoder model =
Delivered -> Delivered ->
Encode.string "delivered" Encode.string "delivered"

View File

@ -38,7 +38,6 @@ type Status
| Sold | Sold
petDecoder : Decoder Pet petDecoder : Decoder Pet
petDecoder = petDecoder =
Decode.succeed Pet Decode.succeed Pet
@ -50,14 +49,13 @@ petDecoder =
|> optional "status" (Decode.nullable statusDecoder) Nothing |> optional "status" (Decode.nullable statusDecoder) Nothing
petEncoder : Pet -> Encode.Value petEncoder : Pet -> Encode.Value
petEncoder model = petEncoder model =
Encode.object Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) ) [ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "category", withDefault Encode.null (map categoryEncoder model.category) ) , ( "category", withDefault Encode.null (map categoryEncoder model.category) )
, ( "name", Encode.string model.name ) , ( "name", Encode.string model.name )
, ( "photoUrls", (Encode.list Encode.string) model.photoUrls ) , ( "photoUrls", Encode.list Encode.string model.photoUrls )
, ( "tags", withDefault Encode.null (map (Encode.list tagEncoder) model.tags) ) , ( "tags", withDefault Encode.null (map (Encode.list tagEncoder) model.tags) )
, ( "status", withDefault Encode.null (map statusEncoder model.status) ) , ( "status", withDefault Encode.null (map statusEncoder model.status) )
] ]
@ -66,20 +64,21 @@ petEncoder model =
statusDecoder : Decoder Status statusDecoder : Decoder Status
statusDecoder = statusDecoder =
Decode.string Decode.string
|> Decode.andThen (\str -> |> Decode.andThen
case str of (\str ->
"available" -> case str of
Decode.succeed Available "available" ->
Decode.succeed Available
"pending" -> "pending" ->
Decode.succeed Pending Decode.succeed Pending
"sold" -> "sold" ->
Decode.succeed Sold Decode.succeed Sold
other -> other ->
Decode.fail <| "Unknown type: " ++ other Decode.fail <| "Unknown type: " ++ other
) )
statusEncoder : Status -> Encode.Value statusEncoder : Status -> Encode.Value
@ -93,6 +92,3 @@ statusEncoder model =
Sold -> Sold ->
Encode.string "sold" Encode.string "sold"

View File

@ -33,11 +33,9 @@ tagDecoder =
|> optional "name" (Decode.nullable Decode.string) Nothing |> optional "name" (Decode.nullable Decode.string) Nothing
tagEncoder : Tag -> Encode.Value tagEncoder : Tag -> Encode.Value
tagEncoder model = tagEncoder model =
Encode.object Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) ) [ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "name", withDefault Encode.null (map Encode.string model.name) ) , ( "name", withDefault Encode.null (map Encode.string model.name) )
] ]

View File

@ -45,7 +45,6 @@ userDecoder =
|> optional "userStatus" (Decode.nullable Decode.int) Nothing |> optional "userStatus" (Decode.nullable Decode.int) Nothing
userEncoder : User -> Encode.Value userEncoder : User -> Encode.Value
userEncoder model = userEncoder model =
Encode.object Encode.object
@ -58,4 +57,3 @@ userEncoder model =
, ( "phone", withDefault Encode.null (map Encode.string model.phone) ) , ( "phone", withDefault Encode.null (map Encode.string model.phone) )
, ( "userStatus", withDefault Encode.null (map Encode.int model.userStatus) ) , ( "userStatus", withDefault Encode.null (map Encode.int model.userStatus) )
] ]

View File

@ -12,8 +12,8 @@
module Request.Pet exposing (addPet, deletePet, findPetsByStatus, findPetsByTags, getPetById, updatePet, updatePetWithForm, uploadFile) 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.ApiResponse exposing (ApiResponse, apiResponseDecoder)
import Data.Pet exposing (Pet, petDecoder, petEncoder)
import Dict import Dict
import Http import Http
import Json.Decode as Decode import Json.Decode as Decode