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

Add post processing to files generated by Haskell generators #968

Merged
merged 11 commits into from
Sep 29, 2018

Conversation

wing328
Copy link
Member

@wing328 wing328 commented Sep 5, 2018

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, 3.3.x, 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

If the environment variable HFMT_PATH (e.g. $HOME/.local/bin/hfmt) is defined, use it to format the Haskell code.

TODO: run hfmt to format Haskell servant code

cc @jonschoning, @algas

@jonschoning
Copy link
Contributor

btw, are you generating the sample with bin/haskell-http-client-petstore.sh or bin/openapi3/haskell-http-client-petstore.sh ?

@jonschoning
Copy link
Contributor

jonschoning commented Sep 5, 2018

not sure if i like having hfmt (and requiring/directing the user to install the hfmt tool) alter the output of the templates, but i'll look it over and see if it's more or less readable than the current output, a least for the haskell-http-client generator

@wing328
Copy link
Member Author

wing328 commented Sep 5, 2018

@jonschoning thanks for the feedback. The formatting done by hfmt can be switched off by removing the environment variable "HFMT_PATH".

I agree with you that some users may not like the output of hfmt so the formatting should remain optional.

@wing328 wing328 added the WIP Work in Progress label Sep 5, 2018
@wing328
Copy link
Member Author

wing328 commented Sep 13, 2018

UPDATE: added hfmt to format Haskell Servant code, minor bug fix ("object" comparison) and enhancement (add "binary" type mapping) via 5d1dbde

@wing328 wing328 removed the WIP Work in Progress label Sep 13, 2018
@jonschoning
Copy link
Contributor

jonschoning commented Sep 13, 2018

After looking at some of the differences, I'm not really liking this change for two reasons:

  • running modifications (i.e. hfmt) on the output of the templates introduces uncertainty into the final output
  • hfmt sometimes makes very long lines where before there was nice separation
  • the code that is generated is already very readable, and i'm not sure the changes result in better readability, in some cases it reduces readability.
  • i would rather make modifications to the templates to fix any readability issues, and so that the output of the templates is deterministic.

if the current output of the source templates was really bad, this would be an improvement, but since the output of the templates have already been written carefully to be pretty readable, i think this just makes things harder to read than easier.

(coerce -> loginUser) :<|>
(coerce -> logoutUser) :<|>
(coerce -> updateUser)) = client (Proxy :: Proxy OpenAPIPetstoreAPI)
((coerce -> addPet) :<|> (coerce -> deletePet) :<|> (coerce -> findPetsByStatus) :<|> (coerce -> findPetsByTags) :<|> (coerce -> getPetById) :<|> (coerce -> updatePet) :<|> (coerce -> updatePetWithForm) :<|> (coerce -> uploadFile) :<|> (coerce -> deleteOrder) :<|> (coerce -> getInventory) :<|> (coerce -> getOrderById) :<|> (coerce -> placeOrder) :<|> (coerce -> createUser) :<|> (coerce -> createUsersWithArrayInput) :<|> (coerce -> createUsersWithListInput) :<|> (coerce -> deleteUser) :<|> (coerce -> getUserByName) :<|> (coerce -> loginUser) :<|> (coerce -> logoutUser) :<|> (coerce -> updateUser)) =
Copy link
Contributor

Choose a reason for hiding this comment

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

this is basically unreadable

@wing328
Copy link
Member Author

wing328 commented Sep 14, 2018

@jonschoning thanks for reviewing the output. What about using stylish-haskell instead, which allows users to provide a config [1] and seems to be more popular [2]?

Ref:
[1] https://github.com/jaspervdj/stylish-haskell#configuration
[2] https://www.reddit.com/r/haskell/comments/7bhsu0/what_code_formatter_are_you_using/

UPDAET: I've updated the samples using stylish-haskell via 1f19a5f

@wing328
Copy link
Member Author

wing328 commented Sep 20, 2018

UPDATE: I've renamed the environment variable to "HASKELL_POST_PROCESS_FILE" to make it consistent across all generators. The user can define it with code formatter of their choice or using other tools to post process the auto-generated Haskell files.

@jonschoning
Copy link
Contributor

jonschoning commented Sep 20, 2018

stylish-haskell has better formatting, but it appears to not handle all cases and generates errors:

[main] ERROR o.o.c.l.HaskellHttpClientCodegen - Error running the command (stylish-haskell -i /home/jon/fs/git/openapi-generator/samples/client/petstore/haskell-http-client/lib/OpenAPIPetstore/Client.hs). Exit value: 1

I like the approach of letting the user choose the code formatter as they wish.
However, at least for haskell-http-client, I already put effort into making the templates generate well-formatted code. How about we let the users choose to use HASKELL_POST_PROCESS_FILE if they wish, but we let the /samples remain the unadjusted output from the templates?

@wing328
Copy link
Member Author

wing328 commented Sep 20, 2018

[main] ERROR o.o.c.l.HaskellHttpClientCodegen - Error running the command (stylish-haskell -i /home/jon/fs/git/openapi-generator/samples/client/petstore/haskell-http-client/lib/OpenAPIPetstore/Client.hs). Exit value: 1

I couldn't repeat the issue locally. Here are partial results:

[main] INFO  o.o.c.l.HaskellHttpClientCodegen - Successfully executed: /Users/williamcheng/.local/bin/stylish-haskell -i /Users/williamcheng/Code/graphql-generator/samples/client/petstore/haskell-http-client/lib/OpenAPIPetstore.hs
[main] INFO  o.o.codegen.AbstractGenerator - writing file /Users/williamcheng/Code/graphql-generator/samples/client/petstore/haskell-http-client/lib/OpenAPIPetstore/Client.hs
[main] INFO  o.o.c.l.HaskellHttpClientCodegen - Successfully executed: /Users/williamcheng/.local/bin/stylish-haskell -i /Users/williamcheng/Code/graphql-generator/samples/client/petstore/haskell-http-client/lib/OpenAPIPetstore/Client.hs
[main] INFO  o.o.codegen.AbstractGenerator - writing file /Users/williamcheng/Code/graphql-generator/samples/client/petstore/haskell-http-client/lib/OpenAPIPetstore/API.hs
[main] INFO  o.o.c.l.HaskellHttpClientCodegen - Successfully executed: /Users/williamcheng/.local/bin/stylish-haskell -i /Users/williamcheng/Code/graphql-generator/samples/client/petstore/haskell-http-client/lib/OpenAPIPetstore/API.hs
[main] INFO  o.o.codegen.AbstractGenerator - writing file /Users/williamcheng/Code/graphql-generator/samples/client/petstore/haskell-http-client/lib/OpenAPIPetstore/Core.hs

Here is how I set the env variable:

export HASKELL_POST_PROCESS_FILE="/Users/williamcheng/.local/bin/stylish-haskell -i"

How about we let the users choose to use HASKELL_POST_PROCESS_FILE if they wish, but we let the /samples remain the unadjusted output from the templates?

Definitely. I updated the samples to show you the difference made by the code formatter. The samples should be not be formatted automatically. I've updated this PR to restore the Haskell samples without using a code formatter.

@jonschoning
Copy link
Contributor

I like that.

I saw you used ./bin/haskell-http-client-petstore.sh to generate the samples, where previously the samples were generated with ./bin/openapi3/haskell-http-client-petstore.sh.

which one would you prefer we generate the samples with? oas2 or oas3?

@wing328
Copy link
Member Author

wing328 commented Sep 20, 2018

I still use oas2 version to update samples.

We may need to move some samples to another repository instead as the size of the repo will become too big if we include samples generated with OAS3 spec

If this change looks good to you, please feel free to approve and merge it.

@jonschoning
Copy link
Contributor

ok, i approve this then.. I will have to make some updates to the example app in the samples because it is compiling against the oas3 version.

@wing328 wing328 changed the title Use hfmt to format Haskell code Add post processing to files generated by Haskell generators Sep 21, 2018
@wing328 wing328 merged commit cbddb08 into master Sep 29, 2018
@wing328 wing328 deleted the haskell_code_format branch September 29, 2018 09:21
A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
…Tools#968)

* add hfmt support (without updating the sample)

* update haskell httpclient samples with hfmt

* add code format option to haskell servant, minor bug fixes

* update code samples with hfmt

* update samples using stylish-haskell

* rename env variable

* update haskell samples with stylish-haskell

* regenerate haskell samples without stylish-haskell

* regenerate haskell servant sample

* update example-app & tests-integration for OAS2 code
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.

None yet

2 participants