Version 0.0.45

1. Boost up module via use token obj at _build_route()
2. Remove unused methods (_match and_compile)
3. Update tests and benchmark
parent 2ccaf11d
Pipeline #161788811 passed with stage
in 1 minute and 17 seconds
lib/.precomp
.precomp
benchmark/lib/.precomp
{
"perl" : "6.*",
"perl" : "6.d",
"name" : "Router::Right",
"version" : "0.0.44",
"version" : "0.0.45",
"description" : "Framework-agnostic URL routing engine for web applications in Raku",
"depends" : [ "P5quotemeta" ],
"license" : "Artistic-2.0",
......
#!/usr/bin/env perl6
use v6.d;
use lib './lib','../lib';
use v6;
use Router::Right;
use Router::Boost;
use Text::Table::Simple;
......@@ -17,7 +17,7 @@ sub word {
return $word;
}
sub MAIN(Int $routes = 10) {
sub MAIN(Int $routes = 100) {
my @paths;
my Str $fn = './dictionary.txt';
if $fn.IO.e {
......@@ -29,21 +29,22 @@ sub MAIN(Int $routes = 10) {
my Int $num_words = 3.rand.floor + 1;
my Str $path = q{/} ~ (1..$num_words).map({ word() }).join(q{/});
my Int $idx = @paths.elems + 1;
@paths.push($path);
if $debug {
( 'Added route: ' ~ $path).say;
}
$idx++;
$r.add( :name( 'name' ~ $idx ), :path( $path), :payload( %(controller => 'Foo') ) );
$r.add( :name( 'name' ~ $idx ), :path( $path ), :payload( %(controller => 'Foo_' ~ $idx) ) );
$rb.add( $path, 'name' ~ $idx );
@paths.push($path);
my Str $path_with_id = $path ~ '/{id}';
@paths.push( $path_with_id );
if $debug {
( 'Added route: ' ~ $path_with_id).say;
}
$r.add( :name( 'name_id' ~ $idx ), :path($path_with_id), :payload( %(controller => 'Foo') ) );
$r.add( :name( 'name_id' ~ $idx ), :path($path_with_id), :payload( %(controller => 'FooId_' ~ $idx) ) );
$rb.add( $path_with_id, 'name_id' ~ $idx );
$path_with_id ~~ s/\{id\}/$idx/;
@paths.push($path_with_id);
$idx++;
}
if @paths {
my UInt $i = 0;
......@@ -51,7 +52,8 @@ sub MAIN(Int $routes = 10) {
my %results = timethese($routes, {
'Router::Right::match' => sub {
my %h = $r."match"(@paths[$i++], Str);
q{.}.print;
# %h.gist.say;
# q{.}.print;
},
'Router::Boost::match' => sub {
my %h = $rb."match"( @paths[$j++] );
......
......@@ -163,47 +163,27 @@ submethod _add( Str $name, Str $path, %payload, Str $options? ) {
payload => 'route <' ~ $name ~ '> already defined',
).throw if $name && %!name_index{$name};
$!regexpr = Nil;
my $class = self;
my %p = %payload;
my ($methods, $_path) = self._split_route_path( $path );
my @methods = self._methods( $options, $methods );
my $index = self._group_index( $_path );
my @route = self._build_route( $_path );
my $re = '[' ~ @route[1] ~ ' $ { $class._setmobj($/); }]';
my token R { ^ <{$re}> };
my %rhandle =
name => $name,
path => $_path,
payload => %p,
methods => @methods,
route => @route[0],
regexpr => @route[1],
regexpr => &R,
index => $index,
;
@!routes[$index].push( %rhandle );
%!name_index{$name} = %rhandle if $name;
}
method _compile returns Regex {
@!routes or return rx/<!>/;
my Router::Right $class = self;
my Str $regexpr;
my UInt $match = $!match;
$regexpr = (
(@!routes.map({ $_[0] }))
.map({
'[' ~
$_<regexpr> ~
' $ { $class._setmatch(' ~
$_<index> ~
'); $class._setmobj($/); }]'
})
).join(q{|});
rx/^ <{$regexpr}> /;
}
method _setmatch( UInt $v ) returns Bool {
$!match = +($v);
True;
}
method _setmobj( Match $m ) returns Bool {
$!m_obj = $m;
True;
......@@ -220,12 +200,14 @@ method match( Str $path, Str $method? ) returns Hash {
for ($idx..(@!routes.elems-1)) {
my $route = (@!routes[$idx]);
if $route {
next if !$route[0]<regexpr>;
my Str $re =
'[' ~
$route[0]<regexpr> ~
' $ { $class._setmobj($/); }]';
if $path ~~ rx/^ <{$re}>/ {
next if !($route[0]<regexpr> ~~ Regex);
#my Str $re =
# '[' ~
# $route[0]<regexpr> ~
# ' $ { $class._setmobj($/); }]';
#if $path ~~ rx:r/^ <{$re}> / {
if $path ~~ $route[0]<regexpr> {
for @$route -> %r {
if $method {
if (%r<methods>) {
......@@ -264,50 +246,6 @@ method match( Str $path, Str $method? ) returns Hash {
%ret;
}
method _match( Str $path, Str $method? ) returns Hash {
my %ret;
my Regex $re = $!regexpr ||= self._compile;
$!error = Nil;
$!match = Nil;
$!m_obj = Nil;
if $path ~~ $re {
my $route = (@!routes[$!match])
or X::AdHoc.new(
payload => 'no route for match index ' ~ $!match,
).throw;
my %matched_route;
if $method {
for @$route -> %r {
my $allowed = %r<methods>;
if ($allowed) {
%matched_route =
%r if (
$allowed.map({ $_ if $method.uc eq $_ })
).elems;
}
else {
%matched_route = %r;
}
last if %matched_route;
}
}
else {
%matched_route = $route[0];
}
if !%matched_route {
$!error = METHOD_NOT_ALLOWED;
}
else {
%ret = %matched_route<payload>;
%ret.push($!m_obj.hash) if $!m_obj;
}
}
else {
$!error = NOT_FOUND;
}
%ret;
}
method url( Str $name, Hash $argh ) returns Str {
X::AdHoc.new( payload => 'no url name supplied' ).throw if !$name;
my @path;
......
......@@ -3,7 +3,7 @@ use Test;
use Router::Right;
plan 10;
plan 9;
my %h;
my @a;
......@@ -159,13 +159,13 @@ subtest {
is($url, '/test-this-url', 'url method test no.3');
}, 'check url method';
subtest {
plan 1;
ok(
$r._compile ~~ Regex,
'_compile returns Regexp',
);
}, 'check _compile method';
# subtest {
# plan 1;
# ok(
# $r._compile ~~ Regex,
# '_compile returns Regexp',
# );
#}, 'check _compile method';
subtest {
plan 9;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment