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) |
| 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 | |

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.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;
@ -48,21 +54,20 @@ import java.util.Map;
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;
}
@ -160,6 +165,10 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
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)) {
@ -624,4 +633,36 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
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);
}
}
}

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
apiResponseEncoder : ApiResponse -> Encode.Value
apiResponseEncoder model =
Encode.object
@ -43,4 +42,3 @@ apiResponseEncoder model =
, ( "type", withDefault Encode.null (map Encode.string model.type_) )
, ( "message", withDefault Encode.null (map Encode.string model.message) )
]

View File

@ -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) )
]

View File

@ -37,7 +37,6 @@ type Status
| Delivered
orderDecoder : Decoder Order_
orderDecoder =
decode Order_
@ -49,7 +48,6 @@ orderDecoder =
|> optional "complete" (Decode.nullable Decode.bool) (Just False)
orderEncoder : Order_ -> Encode.Value
orderEncoder model =
Encode.object
@ -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
@ -92,6 +91,3 @@ statusEncoder model =
Delivered ->
Encode.string "delivered"

View File

@ -38,7 +38,6 @@ type Status
| Sold
petDecoder : Decoder Pet
petDecoder =
decode Pet
@ -50,7 +49,6 @@ petDecoder =
|> optional "status" (Decode.nullable statusDecoder) Nothing
petEncoder : Pet -> Encode.Value
petEncoder model =
Encode.object
@ -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
@ -93,6 +92,3 @@ statusEncoder model =
Sold ->
Encode.string "sold"

View File

@ -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) )
]

View File

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

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
apiResponseEncoder : ApiResponse -> Encode.Value
apiResponseEncoder model =
Encode.object
@ -43,4 +42,3 @@ apiResponseEncoder model =
, ( "type", withDefault Encode.null (map Encode.string model.type_) )
, ( "message", withDefault Encode.null (map Encode.string model.message) )
]

View File

@ -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) )
]

View File

@ -37,7 +37,6 @@ type Status
| Delivered
orderDecoder : Decoder Order_
orderDecoder =
Decode.succeed Order_
@ -49,7 +48,6 @@ orderDecoder =
|> optional "complete" (Decode.nullable Decode.bool) (Just False)
orderEncoder : Order_ -> Encode.Value
orderEncoder model =
Encode.object
@ -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
@ -92,6 +91,3 @@ statusEncoder model =
Delivered ->
Encode.string "delivered"

View File

@ -38,7 +38,6 @@ type Status
| Sold
petDecoder : Decoder Pet
petDecoder =
Decode.succeed Pet
@ -50,14 +49,13 @@ petDecoder =
|> optional "status" (Decode.nullable statusDecoder) Nothing
petEncoder : Pet -> Encode.Value
petEncoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
, ( "category", withDefault Encode.null (map categoryEncoder model.category) )
, ( "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) )
, ( "status", withDefault Encode.null (map statusEncoder model.status) )
]
@ -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
@ -93,6 +92,3 @@ statusEncoder model =
Sold ->
Encode.string "sold"

View File

@ -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) )
]

View File

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