dispatch_compiler (dispatch_compiler v1.2.0)
View SourceCompile dispatch rules to an Erlang module.
The dispatch compiler takes all the list of dispatch rules and creates an Erlang module that matches those rules.
The Erlang module exports a single function: match/2
.
This function takes the binary request path, split on "/"
and return the matched dispatch rule and list of bindings.
The dispatch function looks like:
match([], Context) ->
{ok, {{home, [], controller_page, [...]}, []}};
match([<<"page">>, Id, Slug], Context) ->
{ok, {{page, ["page", id, slug], controller_page, [...]}, [{id,Id}, {slug,Slug}]}};
match([<<"lib">> | Star], Context) when Star =/= [] ->
{ok, {{lib, ["lib", '*'], controller_file, [...]}, [{'*',Star}]}};
match(_, _Context) ->
fail.
Rules can also have conditions on their arguments. The condition are matched using the runtime dispatch_compiler:bind/3
function.
match([<<"id">>, Foo] = Path, Context) ->
case dispatch_compiler:runtime_bind(Path, ["id", id], Context) of
{ok, Bindings} ->
{ok, {{id, ["id", id], controller_id, [...]}, Bindings}};
fail ->
match1(Path, Context)
end;
match(Path, Context) ->
match1(Path, Context).
match1(..., Context) ->
...
match1(_, _Context) ->
fail.
Summary
Functions
Compile Erlang module.
Compile and load Erlang module.
Equivalent to Module:match(Tokens, Context).
Runtime callback for argument binding with checks on the arguments. The checks can be a function, module-function, or regexp.
Types
-type dispatch_rule() :: {Name :: atom(), dispatch_rule_path(), Handler :: any(), HandlerArgs :: list()}.
-type dispatch_rule_path() :: [dispatch_rule_token()].
-type dispatch_rule_token() :: binary() | '*' | atom() | {atom(), {atom(), atom()}} | {atom(), {atom(), atom(), list()}} | {atom(), match_function()} | {atom(), RegExp :: iodata() | unicode:charlist()} | {atom(), RegExp :: iodata() | unicode:charlist(), Options :: re_options()}.
-type re_compile_option() :: unicode | anchored | caseless | dollar_endonly | dotall | extended | firstline | multiline | no_auto_capture | dupnames | ungreedy | {newline, re_nl_spec()} | bsr_anycrlf | bsr_unicode | no_start_optimize | ucp | never_utf.
-type re_compile_options() :: [re_compile_option()].
-type re_nl_spec() :: cr | crlf | lf | nul | anycrlf | any.
-type re_option() :: anchored | global | notbol | noteol | notempty | notempty_atstart | report_errors | {offset, non_neg_integer()} | {match_limit, non_neg_integer()} | {match_limit_recursion, non_neg_integer()} | {capture, ValueSpec :: re_capture()} | {capture, ValueSpec :: re_capture(), Type :: index | list | binary} | re_compile_option().
-type re_options() :: [re_option()].
Functions
-spec compile(atom(), [dispatch_rule()]) -> {ok, atom(), binary()}.
Compile Erlang module.
-spec compile_load(ModuleName, DLs) -> Result when ModuleName :: atom(), DLs :: [dispatch_rule()], Result :: ok.
Compile and load Erlang module.
-spec match(Module, Tokens, Context) -> Result when Module :: atom(), Tokens :: [binary()], Context :: any(), Result :: {ok, {dispatch_rule(), [binding()]}} | fail.
Equivalent to Module:match(Tokens, Context).
Launch Module
match
function.
-spec runtime_bind(Pattern, Path, Context) -> Result when Pattern :: list(), Path :: [binary()], Context :: any(), Result :: {ok, [binding()]} | fail.
Runtime callback for argument binding with checks on the arguments. The checks can be a function, module-function, or regexp.