Skip to content

Commit 88f79c2

Browse files
authored
Match to function plug (#1245)
1 parent faff77d commit 88f79c2

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

lib/plug/router.ex

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,22 +267,31 @@ defmodule Plug.Router do
267267
init_mode = Module.get_attribute(env.module, :plug_builder_opts)[:init_mode]
268268

269269
defs =
270-
for {callback, {mod, opts}} <- router_to do
271-
if init_mode == :runtime do
272-
quote do
273-
defp unquote(callback)(conn, _opts) do
274-
unquote(mod).call(conn, unquote(mod).init(unquote(Macro.escape(opts))))
270+
for {callback, {target, opts}} <- router_to do
271+
case {init_mode, Atom.to_string(target)} do
272+
{:runtime, "Elixir." <> _} ->
273+
quote do
274+
defp unquote(callback)(conn, _opts) do
275+
unquote(target).call(conn, unquote(target).init(unquote(Macro.escape(opts))))
276+
end
277+
end
278+
279+
{_, "Elixir." <> _} ->
280+
opts = target.init(opts)
281+
282+
quote do
283+
defp unquote(callback)(conn, _opts) do
284+
require unquote(target)
285+
unquote(target).call(conn, unquote(Macro.escape(opts)))
286+
end
275287
end
276-
end
277-
else
278-
opts = mod.init(opts)
279-
280-
quote do
281-
defp unquote(callback)(conn, _opts) do
282-
require unquote(mod)
283-
unquote(mod).call(conn, unquote(Macro.escape(opts)))
288+
289+
_ ->
290+
quote do
291+
defp unquote(callback)(conn, _opts) do
292+
unquote(target)(conn, unquote(Macro.escape(opts)))
293+
end
284294
end
285-
end
286295
end
287296
end
288297

@@ -575,6 +584,7 @@ defmodule Plug.Router do
575584
router_to = Module.get_attribute(module, :plug_router_to)
576585
callback = :"plug_router_to_#{map_size(router_to)}"
577586
router_to = Map.put(router_to, callback, {to, init_opts})
587+
578588
Module.put_attribute(module, :plug_router_to, router_to)
579589
{Macro.var(callback, nil), options}
580590
end

test/plug/router_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,18 @@ defmodule Plug.RouterTest do
206206
forward "/plug/init_opts", to: plug, init_opts: opts, private: %{baz: :qux}
207207

208208
forward "/plug/forward_local", to: :forward_local
209+
match "/plug/match_local", to: :match_local
209210
forward "/plug/forward_local_opts", to: :forward_local, init_opts: opts, private: %{baz: :qux}
211+
match "/plug/match_local_opts", to: :match_local, init_opts: opts, private: %{baz: :qux}
210212

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

217+
def match_local(conn, opts) do
218+
resp(conn, 200, "#{inspect(opts)}")
219+
end
220+
215221
match _ do
216222
resp(conn, 404, "oops")
217223
end
@@ -587,12 +593,23 @@ defmodule Plug.RouterTest do
587593
assert conn.resp_body == "[]"
588594
end
589595

596+
test "matches to a function plug" do
597+
conn = call(Sample, conn(:get, "/plug/match_local"))
598+
assert conn.resp_body == "[]"
599+
end
600+
590601
test "forwards to a function plug with options" do
591602
conn = call(Sample, conn(:get, "/plug/forward_local_opts"))
592603
assert conn.private[:baz] == :qux
593604
assert conn.resp_body == ":hello"
594605
end
595606

607+
test "matches to a function plug with options" do
608+
conn = call(Sample, conn(:get, "/plug/match_local_opts"))
609+
assert conn.private[:baz] == :qux
610+
assert conn.resp_body == ":hello"
611+
end
612+
596613
test "emit start and stop event when router dispatches" do
597614
start_router_id = {:start, :rand.uniform(100)}
598615
stop_router_id = {:stop, :rand.uniform(100)}

0 commit comments

Comments
 (0)