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

Support plug: &fun/1 #282

Closed
wants to merge 3 commits into from
Closed

Conversation

wojtekmach
Copy link
Contributor

There was a clause:

plug_fn when is_function(plug_fn) -> {plug_fn, []}

which supported any function but in reality it only allowed 2-arity functions.

This patch adds support for 1-arity functions too. This matches Plug.run/3:

iex> conn = Plug.Test.conn(:get, "/")
iex> plug = fn conn -> Plug.Conn.send_resp(conn, 200, "hi") end
iex> Plug.run(conn, [plug]).resp_body
"hi"

as well as projects along the lines of:

Bypass.stub(bypass, "GET", "/", fn conn -> ... end)

(wink wink)

There was a clause:

    plug_fn when is_function(plug_fn) -> {plug_fn, []}

which supported any function but in reality it only allowed 2-arity
functions.

This patch adds support for 1-arity functions too. This matches
`Plug.run/3`:

    iex> conn = Plug.Test.conn(:get, "/")
    iex> plug = fn conn -> Plug.Conn.send_resp(conn, 200, "hi") end
    iex> Plug.run(conn, [plug]).resp_body
    "hi"

as well as projects along the lines of:

    Bypass.stub(bypass, "GET", "/", fn conn -> ... end)

(wink wink)
@mtrudel
Copy link
Owner

mtrudel commented Dec 29, 2023

The Plug spec specifically says arity 2 only; is there also a gap in Plug's docs in this respect?

@wojtekmach
Copy link
Contributor Author

I think that’s a gap yeah, I’ll send docs update and check back here.

@wojtekmach
Copy link
Contributor Author

I don't think the docs update in that spot would be useful because I believe the vast majority of Plug usage is in the form of:

plug Mod
plug :fun
plug :fun, opts

and these need to be 2-arity.

As mentioned above, &fun/1 is supported by Plug.run so I think this is legit, however mentioning it in that spot would just confuse things IMO.

I understand if you'd rather not pursue this, I'm fine doing fn conn, _ -> or the fn conn, _ -> f.(conn) end trick in my own code.

@wojtekmach
Copy link
Contributor Author

FWIW I opened elixir-plug/plug#1201.

@wojtekmach wojtekmach closed this Dec 29, 2023
@mtrudel
Copy link
Owner

mtrudel commented Dec 30, 2023

Yep, looks like this all got squared away correctly. Thanks for the effort!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants