Test

dispatch_compiler

Compiles dispatch rules to an Erlang module for quick matching.

The dispatch compiler takes a list of dispatch rules and creates an Erlang module that matches those rules.

The compiled Erlang module exports a single function: match/2.

Dispatch rules

Dispatch rules are lists of tokens with some extra information:

{name, ["foo", "bar", id], controller_name, [controller,options]}

The path parts can be one of the following:

It is also possible to define functions to perform runtime checks on the tokens.

Functions can be defined as:

Functions must return one of the following

Usage

First compile the dispatch rules to an Erlang module:

Rules = [
    {test, ["a", v], foo, []},
    {wildcard, ["w", '*'], foo, []}
],
ok = dispatch_compiler:compile_load('mydispatch', Rules).

Now the compiled module can be used to match (the undefined will be passed as Context to any functions in the dispatch rules):

1> mydispatch:match([<<"a">>, <<"b">>], undefined).
{ok, { {test, ["a", v], foo, []}, [{v,<<"b">>}]}}

The return value contains the matched dispatch rule and any bound variables. The first matching rule is returned.

Another example showing a matching wildcard:

2> mydispatch:match([<<"w">>, <<"b">>, <<"c">>], undefined).
{ok, { {wildcard, ["w", '*'], foo, []}, [{'*',[<<"b">>, <<"c">>]}]}}

If no dispatch rule could be matched, then fail is returned:

3> mydispatch:match([<<"a">>, <<"b">>, <<"c">>], undefined).
fail

Tests

Run make test to run the tests.