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

Match to functional plug #1245

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 24 additions & 14 deletions lib/plug/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,31 @@ defmodule Plug.Router do
init_mode = Module.get_attribute(env.module, :plug_builder_opts)[:init_mode]

defs =
for {callback, {mod, opts}} <- router_to do
if init_mode == :runtime do
quote do
defp unquote(callback)(conn, _opts) do
unquote(mod).call(conn, unquote(mod).init(unquote(Macro.escape(opts))))
for {callback, {target, opts}} <- router_to do
case {init_mode, Atom.to_string(target)} do
{:runtime, "Elixir." <> _} ->
quote do
defp unquote(callback)(conn, _opts) do
unquote(target).call(conn, unquote(target).init(unquote(Macro.escape(opts))))
end
end

{_, "Elixir." <> _} ->
opts = target.init(opts)

quote do
defp unquote(callback)(conn, _opts) do
require unquote(target)
unquote(target).call(conn, unquote(Macro.escape(opts)))
end
end
end
else
opts = mod.init(opts)

quote do
defp unquote(callback)(conn, _opts) do
require unquote(mod)
unquote(mod).call(conn, unquote(Macro.escape(opts)))

_ ->
quote do
defp unquote(callback)(conn, _opts) do
unquote(target)(conn, unquote(Macro.escape(opts)))
end
end
end
end
end

Expand Down Expand Up @@ -575,6 +584,7 @@ defmodule Plug.Router do
router_to = Module.get_attribute(module, :plug_router_to)
callback = :"plug_router_to_#{map_size(router_to)}"
router_to = Map.put(router_to, callback, {to, init_opts})

Module.put_attribute(module, :plug_router_to, router_to)
{Macro.var(callback, nil), options}
end
Expand Down
17 changes: 17 additions & 0 deletions test/plug/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,18 @@ defmodule Plug.RouterTest do
forward "/plug/init_opts", to: plug, init_opts: opts, private: %{baz: :qux}

forward "/plug/forward_local", to: :forward_local
match "/plug/match_local", to: :match_local
forward "/plug/forward_local_opts", to: :forward_local, init_opts: opts, private: %{baz: :qux}
match "/plug/match_local_opts", to: :match_local, init_opts: opts, private: %{baz: :qux}

def forward_local(conn, opts) do
resp(conn, 200, "#{inspect(opts)}")
end

def match_local(conn, opts) do
resp(conn, 200, "#{inspect(opts)}")
end

match _ do
resp(conn, 404, "oops")
end
Expand Down Expand Up @@ -586,12 +592,23 @@ defmodule Plug.RouterTest do
assert conn.resp_body == "[]"
end

test "matches to a function plug" do
conn = call(Sample, conn(:get, "/plug/match_local"))
assert conn.resp_body == "[]"
end

test "forwards to a function plug with options" do
conn = call(Sample, conn(:get, "/plug/forward_local_opts"))
assert conn.private[:baz] == :qux
assert conn.resp_body == ":hello"
end

test "matches to a function plug with options" do
conn = call(Sample, conn(:get, "/plug/match_local_opts"))
assert conn.private[:baz] == :qux
assert conn.resp_body == ":hello"
end

test "emit start and stop event when router dispatches" do
start_router_id = {:start, :rand.uniform(100)}
stop_router_id = {:stop, :rand.uniform(100)}
Expand Down