Skip to content
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

[Elm] Template improvements #661

Merged
merged 6 commits into from
Aug 6, 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
2 changes: 1 addition & 1 deletion bin/elm-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g elm -o samples/client/petstore/elm $@"
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g elm -t modules/openapi-generator/src/main/resources/elm -o samples/client/petstore/elm $@"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so as using -t allows us to regenerate the samples without doing another "mvn clean package" after updating the templates.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I also checked that it's used in other scripts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels a bit weird that it sort of overloads the build, but it makes sense to add it if that's also the case for the other generators.


java $JAVA_OPTS -jar $executable $ags
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Date
import Date.Extra exposing (fromIsoString, toFormattedString)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Result


type alias DateOnly =
Expand All @@ -24,11 +25,8 @@ dateOnlyEncoder model =
decodeIsoString : String -> Decoder DateOnly
decodeIsoString str =
case fromIsoString str of
Just date ->
Result.Ok date ->
Decode.succeed date

Nothing ->
Decode.fail <|
"Cannot convert "
++ str
++ " to DateOnly"
Result.Err msg ->
Decode.fail msg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like Decode.fail <| "Cannot decode ISO string: " ++ msg makes clear that it's the decoder that failed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message in the new date-extra is

Failed to create a Date from string 'foo': Invalid ISO 8601 format

Do you still want to add "Cannot decode ISO string: "?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the thing is this is a Decoder error, not a Date error. But the type already indicates that so I think we can stick with the Decode.fail msg.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Date
import Date.Extra exposing (fromIsoString, toIsoString)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Result


type alias DateTime =
Expand All @@ -24,11 +25,8 @@ dateTimeEncoder model =
decodeIsoString : String -> Decoder DateTime
decodeIsoString str =
case fromIsoString str of
Just date ->
Result.Ok date ->
Decode.succeed date

Nothing ->
Decode.fail <|
"Cannot convert "
++ str
++ " to DateTime"
Result.Err msg ->
Decode.fail msg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{classVarName}}Decoder : Decoder {{classname}}
{{classVarName}}Decoder =
decode {{classname}}
{{#allVars}}{{^discriminatorValue}} |> {{>fieldDecoder}}
{{/discriminatorValue}}{{/allVars}}
{{#allVars}}
{{^discriminatorValue}} |> {{>fieldDecoder}}
{{/discriminatorValue}}
{{/allVars}}
11 changes: 5 additions & 6 deletions modules/openapi-generator/src/main/resources/elm/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import Json.Decode as Decode
basePath : String
basePath =
"{{basePath}}"


{{#operations}}
{{#operation}}
{-
{{notes}}


{{#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}}"
Expand All @@ -28,7 +29,5 @@ basePath =
, withCredentials = False
}
|> Http.request


{{/operation}}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"elm-lang/core": "5.1.1 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/http": "1.0.0 <= v < 2.0.0",
"justinmimbs/elm-date-extra": "2.0.3 <= v < 3.0.0"
"justinmimbs/elm-date-extra": "3.0.0 <= v < 4.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
{{{appName}}}
{{{appDescription}}}

{{#version}}OpenAPI spec version: {{{version}}}{{/version}}
{{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
{{#version}}
OpenAPI spec version: {{{version}}}
{{/version}}
{{#infoEmail}}
Contact: {{{infoEmail}}}
{{/infoEmail}}

NOTE: This file is auto generated by the openapi-generator.
https://github.com/openapitools/openapi-generator.git
Expand Down
11 changes: 3 additions & 8 deletions modules/openapi-generator/src/main/resources/elm/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ module Data.{{classname}} exposing ({{#models}}{{#model}}{{classname}}{{#hasChil
import Json.Decode.Pipeline exposing (decode, optional, required)
import Json.Encode as Encode
import Maybe exposing (map, withDefault)


{{#models}}
{{#model}}


{{#description}}
{-
{{{description}}}
{-| {{{description}}}
-}
{{/description}}
{{#isEnum}}{{>modelTypeUnion}}{{/isEnum}}{{^isEnum}}{{#hasChildren}}{{>modelTypeDiscriminator}}{{/hasChildren}}{{^hasChildren}}{{#isAlias}}{{>modelTypePrimitive}}{{/isAlias}}{{^isAlias}}{{#isArrayModel}}{{>modelTypeArray}}{{/isArrayModel}}{{^isArrayModel}}{{>modelTypeAlias}}{{/isArrayModel}}{{/isAlias}}{{/hasChildren}}{{/isEnum}}
{{/model}}
{{^-last}}


{{/-last}}
{{/models}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@


type alias {{classname}} =
{ {{#vars}}{{^-first}} , {{/-first}}{{name}} : {{^required}}Maybe {{/required}}{{#isContainer}}(List {{/isContainer}}{{#isEnum}}{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{dataType}}{{/isEnum}}{{#isContainer}}){{/isContainer}}
{ {{#vars}}{{^-first}} , {{/-first}}{{name}} : {{^required}}Maybe {{#isContainer}}({{/isContainer}}{{/required}}{{#isContainer}}List {{/isContainer}}{{#isEnum}}{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{dataType}}{{/isEnum}}{{^required}}{{#isContainer}}){{/isContainer}}{{/required}}
{{/vars}} }
{{#vars}}
{{#isEnum}}
Expand All @@ -25,4 +23,4 @@ type alias {{classname}} =

{{>unionEncoder}}
{{/isEnum}}
{{/vars}}
{{/vars}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


type alias {{classname}}
= {{dataType}}

Expand All @@ -11,4 +9,4 @@ type alias {{classname}}

{{classVarName}}Encoder : {{classname}} -> Encode.Value
{{classVarName}}Encoder =
{{vendorExtensions.x-encoder}}
{{vendorExtensions.x-encoder}}
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.0.0-SNAPSHOT
3.2.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore/elm/elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"elm-lang/core": "5.1.1 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/http": "1.0.0 <= v < 2.0.0",
"justinmimbs/elm-date-extra": "2.0.3 <= v < 3.0.0"
"justinmimbs/elm-date-extra": "3.0.0 <= v < 4.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
6 changes: 1 addition & 5 deletions samples/client/petstore/elm/src/Data/ApiResponse.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.

OpenAPI spec version: 1.0.0


NOTE: This file is auto generated by the openapi-generator.
https://github.com/openapitools/openapi-generator.git
Expand All @@ -19,11 +18,8 @@ import Json.Encode as Encode
import Maybe exposing (map, withDefault)


{-
Describes the result of uploading an image resource
{-| Describes the result of uploading an image resource
-}


type alias ApiResponse =
{ code : Maybe Int
, type_ : Maybe String
Expand Down
6 changes: 1 addition & 5 deletions samples/client/petstore/elm/src/Data/Category.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.

OpenAPI spec version: 1.0.0


NOTE: This file is auto generated by the openapi-generator.
https://github.com/openapitools/openapi-generator.git
Expand All @@ -19,11 +18,8 @@ import Json.Encode as Encode
import Maybe exposing (map, withDefault)


{-
A category for a pet
{-| A category for a pet
-}


type alias Category =
{ id : Maybe Int
, name : Maybe String
Expand Down
6 changes: 1 addition & 5 deletions samples/client/petstore/elm/src/Data/Order_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.

OpenAPI spec version: 1.0.0


NOTE: This file is auto generated by the openapi-generator.
https://github.com/openapitools/openapi-generator.git
Expand All @@ -20,11 +19,8 @@ import Json.Encode as Encode
import Maybe exposing (map, withDefault)


{-
An order for a pets from the pet store
{-| An order for a pets from the pet store
-}


type alias Order_ =
{ id : Maybe Int
, petId : Maybe Int
Expand Down
8 changes: 2 additions & 6 deletions samples/client/petstore/elm/src/Data/Pet.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.

OpenAPI spec version: 1.0.0


NOTE: This file is auto generated by the openapi-generator.
https://github.com/openapitools/openapi-generator.git
Expand All @@ -21,16 +20,13 @@ import Json.Encode as Encode
import Maybe exposing (map, withDefault)


{-
A pet for sale in the pet store
{-| A pet for sale in the pet store
-}


type alias Pet =
{ id : Maybe Int
, category : Maybe Category
, name : String
, photoUrls : (List String)
, photoUrls : List String
, tags : Maybe (List Tag)
, status : Maybe Status
}
Expand Down
6 changes: 1 addition & 5 deletions samples/client/petstore/elm/src/Data/Tag.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.

OpenAPI spec version: 1.0.0


NOTE: This file is auto generated by the openapi-generator.
https://github.com/openapitools/openapi-generator.git
Expand All @@ -19,11 +18,8 @@ import Json.Encode as Encode
import Maybe exposing (map, withDefault)


{-
A tag for a pet
{-| A tag for a pet
-}


type alias Tag =
{ id : Maybe Int
, name : Maybe String
Expand Down
6 changes: 1 addition & 5 deletions samples/client/petstore/elm/src/Data/User.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.

OpenAPI spec version: 1.0.0


NOTE: This file is auto generated by the openapi-generator.
https://github.com/openapitools/openapi-generator.git
Expand All @@ -19,11 +18,8 @@ import Json.Encode as Encode
import Maybe exposing (map, withDefault)


{-
A User who is purchasing from the pet store
{-| A User who is purchasing from the pet store
-}


type alias User =
{ id : Maybe Int
, username : Maybe String
Expand Down
10 changes: 4 additions & 6 deletions samples/client/petstore/elm/src/DateOnly.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Date
import Date.Extra exposing (fromIsoString, toFormattedString)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Result


type alias DateOnly =
Expand All @@ -24,11 +25,8 @@ dateOnlyEncoder model =
decodeIsoString : String -> Decoder DateOnly
decodeIsoString str =
case fromIsoString str of
Just date ->
Result.Ok date ->
Decode.succeed date

Nothing ->
Decode.fail <|
"Cannot convert "
++ str
++ " to DateOnly"
Result.Err msg ->
Decode.fail msg
10 changes: 4 additions & 6 deletions samples/client/petstore/elm/src/DateTime.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Date
import Date.Extra exposing (fromIsoString, toIsoString)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Result


type alias DateTime =
Expand All @@ -24,11 +25,8 @@ dateTimeEncoder model =
decodeIsoString : String -> Decoder DateTime
decodeIsoString str =
case fromIsoString str of
Just date ->
Result.Ok date ->
Decode.succeed date

Nothing ->
Decode.fail <|
"Cannot convert "
++ str
++ " to DateTime"
Result.Err msg ->
Decode.fail msg
Loading