dispatch_compiler (dispatch_compiler v1.2.0)

View Source

Compile 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

binding/0

-type binding() :: {atom(), binary() | [binary() | any()]}.

dispatch_rule/0

-type dispatch_rule() :: {Name :: atom(), dispatch_rule_path(), Handler :: any(), HandlerArgs :: list()}.

dispatch_rule_path/0

-type dispatch_rule_path() :: [dispatch_rule_token()].

dispatch_rule_token/0

-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()}.

match_function/0

-type match_function() ::
          fun((binary(), any()) -> boolean() | {ok, any()}) | fun((binary()) -> boolean() | {ok, any()}).

re_capture/0

-type re_capture() ::
          all | all_but_first | all_names | first | none |
          (ValueList :: [integer() | string() | atom()]).

re_compile_option/0

-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.

re_compile_options/0

-type re_compile_options() :: [re_compile_option()].

re_nl_spec/0

-type re_nl_spec() :: cr | crlf | lf | nul | anycrlf | any.

re_option/0

-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().

re_options/0

-type re_options() :: [re_option()].

Functions

compile(ModuleName, DLs)

-spec compile(atom(), [dispatch_rule()]) -> {ok, atom(), binary()}.

Compile Erlang module.

compile_load(ModuleName, DLs)

-spec compile_load(ModuleName, DLs) -> Result
                      when ModuleName :: atom(), DLs :: [dispatch_rule()], Result :: ok.

Compile and load Erlang module.

match(Module, Tokens, Context)

-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 Modulematch function.

runtime_bind(Pattern, Path, Context)

-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.