-
Notifications
You must be signed in to change notification settings - Fork 5
Demonstrate generated Elixir code #74
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic work!
alias ElixirApiWeb.EmployeesResource | ||
alias ElixirApiWeb.LocationsResource | ||
|
||
def build(config) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to also generate typespecs for the resources
|
||
# add aliases here | ||
|
||
def create(conn, _params) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great start!
One obvious issue I'm seeing is that after the developer implements the missing parts they can't regenerate the thing without undoing their work.
The answer to this is ofc better modularity
We should make the thing modular enough that The controller code is actually dumb enough to not need any manual changes ever. That would require us to agree on some conventions as to what the code in the controller action should look like and what should it call into and what the error handling should look like.
same thing is obiously true about the views, the tests, the router, etc.
There are many ways to achieve this separation between generated and nongenerated code.
It's not ease to think of optimal patterns for this from the top of my head.
Another thing that comes to my mind when trying to think of the patterns is that
if we make the generator smart enough to know that If an action/test/view already exists, then It should not regenerate it then it would be much easier to make the separation natural and not requiring splitting things into many files. Wdyt? I know it's not easy to make a foolproof mechanism like that without actually parsing the code (or using mysterious nonstandard extended regexes), so it might not be worth to introduce this complexity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to get the most value from the spec->code workflow then we have to enable painfree, nondestructive generation. I'm stating the obvious here - generation of a new endpoint should not destroy a whole controller, etc
@@ -0,0 +1,32 @@ | |||
defmodule ElixirApiWeb.BlockedTimesControllerTest do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's important to think about the 3 layers of tests that we have here to make sure we're not testing the shape of the data multiple times needlesly. The resource tests are generated. The view tests should be very dumb (are they even necessery?)
In the controller tests we could use some openApi magic to verify the responses. Also If we make the controllers dumb enough to not need any human changes then the tests could probably be fully generated too.
Then we would test logic in the core modules, and the controllers, resources data shapes would all come from openapi
Motivation and Context
I'd like to demonstrate our BE developers how generated Elixir code looks like.
Type of changes
What types of changes does your code introduce? Put an
x
in all the boxes that apply:Details
Code was generated out of
examples/openapi-codegen-test-api.yaml
schema usingexamples/codegen/run.sh
script.To regenerate the code by yourself, install Node >= 16.x on your machine, and then install
@fresha/openapi-codegen-cli
Usage
examples/codegen/elixir_api/lib/elixir_api_web/views/blocked_times_view.ex
lineswith
examples/codegen/elixir_api/lib/elixir_api_web/controllers/blocked_times_controller.ex
lineswith
Now, you should see list of blocked time resources returned.