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

Conversation

laughedelic
Copy link
Contributor

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

This PR fixes #458 and improves doc comments and line spacing in the Elm template. (see also swagger-api/swagger-codegen#8152 for the context).

There are still some extra newlines that are hard to get rid of. They appear because the {{>blah}} includes bring the last newline in the parent template. I don't know if this can be somehow improved on the Mustache side, but in general it's quite hard to produce some code that immediately complies with the formatting guideline (elm-format).

@laughedelic
Copy link
Contributor Author

e4fa432 also solves swagger-api/swagger-codegen#8151

@wing328
Copy link
Member

wing328 commented Jul 27, 2018

They appear because the {{>blah}} includes bring the last newline in the parent template.

@laughedelic thanks for the PR 👍

Would removing the last newline in the parent (partial) template help? If you can point me to the line number in the template and show me the actual vs expected output, I can help take a look.

cc @trenneman

@laughedelic
Copy link
Contributor Author

laughedelic commented Jul 27, 2018

Would removing the last newline in the parent (partial) template help? If you can point me to the line number in the template and show me the actual vs expected output, I can help take a look.

As I understand it, the extra newline comes from the child template. So if you have an {{>child}} at the last line of the parent template, the result will have \n\n at the end, coming from the parent and the child.

Here is an example piece of template:

And the produced result:

apiResponseDecoder : Decoder ApiResponse
apiResponseDecoder =
decode ApiResponse
|> optional "code" (Decode.nullable Decode.int) Nothing
|> optional "type" (Decode.nullable Decode.string) Nothing
|> 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) )
]

One extra newline is added after inlining the aliasDecoder, same with the aliasEncoder, and then one more in the end, because the modelTypeAlias is itself inlined in the model template.

Ah, and here is the expected output:

apiResponseDecoder : Decoder ApiResponse 
 apiResponseDecoder = 
     decode ApiResponse 
         |> optional "code" (Decode.nullable Decode.int) Nothing 
         |> optional "type" (Decode.nullable Decode.string) Nothing 
         |> 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) ) 
         ] 

(no empty lines in the end and only 2 between the definitions)

@wing328
Copy link
Member

wing328 commented Jul 30, 2018

@laughedelic @trenneman I suggest we merge this and deal with the extra newline in a separate PR.

Copy link
Contributor

@eriktim eriktim left a comment

Choose a reason for hiding this comment

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

I've been struggling a lot with newlines as well. It's a PITA with mustache.
A few remarks, but it looks OK other than that (and the remaining newline issue).

@@ -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.

++ 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.

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

@laughedelic
Copy link
Contributor Author

Hi! I replied to the review comments and now I'm not sure if there's still anything left to change.

@wing328
Copy link
Member

wing328 commented Aug 5, 2018

If no more question/feedback on this PR, I'll merge it into master tomorrow (Monday)

@wing328 wing328 merged commit 05db32f into OpenAPITools:master Aug 6, 2018
@andys8
Copy link
Contributor

andys8 commented Aug 6, 2018

Nice!

@wing328
Copy link
Member

wing328 commented Aug 23, 2018

Hi all, I've filed #879 to improve the code format of the Elm client by removing the EOF from 2 mustache files. Please review to see if it looks good to you.

@wing328
Copy link
Member

wing328 commented Aug 26, 2018

#879 has been reviewed and merged into master.

Shall we use https://github.com/jfmengels/elm-lint to improve the auto-generated Elm code? (I've no experience using it)

@eriktim
Copy link
Contributor

eriktim commented Aug 26, 2018

@wing328 You typically use elm-format for that. You want to add this as a post-processing step?

@andys8
Copy link
Contributor

andys8 commented Aug 26, 2018

elm-format does formatting and minor changes (like changing import order). elm-analyse is a longer (static code analysis). We could avoid the two dependencies by making sure the templates themselves are formatted correctly. But it could also be a benefit to make it sure by using elm-format.

@wing328
Copy link
Member

wing328 commented Aug 26, 2018

Thanks for the suggestion guys. I will see if I can come up with a solution to run elm-format for each auto-generated Elm files. Will keep you guys posted.

@eriktim
Copy link
Contributor

eriktim commented Aug 26, 2018

That would help. However, my preference would be to format it correctly from the start, without any post-processing.

@wing328
Copy link
Member

wing328 commented Aug 30, 2018

That would help. However, my preference would be to format it correctly from the start, without any post-processing.

Totally agree but we found that it's not easy to do so with mustache.

For Go, we're trying to use gofmt -w and the result looks good so far: #929

I've created #933 to track the code format improvement

A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
* Add elm template parameter to the samples testing script

* Update elm-date-extra package; fixes OpenAPITools#458

* Update generated elm samples

* Use Elm doc comments; remove some unneccessary newlines

* Update generated Elm samples

* Remove unnecessary parenthesis around non-optional container type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[elm-client] Update dependency: elm-date-extra
4 participants