From 936a27cc83a081cd9b39f5c66d33dc4b40e6822f Mon Sep 17 00:00:00 2001 From: Guillaume Poirier-Morency Date: Fri, 13 Nov 2015 11:29:39 -0500 Subject: [PATCH] Fix #138: Interpret scopes as rules The rule compilation code has been refactored to be reused for both scopes and rules. --- src/valum-router.vala | 52 ++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/valum-router.vala b/src/valum-router.vala index b6bdb27a2..45c04b733 100644 --- a/src/valum-router.vala +++ b/src/valum-router.vala @@ -171,26 +171,22 @@ namespace Valum { } /** - * Create a Route for a given callback from a rule. - * - * Rule are scoped from the {@link Router.scope} fragment stack and - * compiled down to {@link GLib.Regex}. + * Compile a rule into a regular expression. * - * Rule start matching after the first '/' character of the request URI - * path. + * Parameters are compiled down to named captures and any other + * character goes through {@link Regex.escape_string}. * - * @since 0.3 + * The 'null' rule is a special rule that captures anything '.*'. * - * @param method method matching this rule - * @param rule rule or 'null' to capture all possible paths - * @param cb handling callback + * The compilation process is contextualized for this {@link Valum.Router} + * to honor its defined types. * - * @throws RegexError if the rule is incorrectly formed or a type is - * undefined in the 'types' mapping + * @since 0.3 * - * @return a builder upon the created {@link Route} object + * @param rule + * @return a regular expression pattern */ - public Builder rule (string method, string? rule, owned HandlerCallback cb) throws RegexError { + public string compile_rule (string? rule) throws RegexError { var params = /(<(?:\w+:)?\w+>)/.split_full (rule == null ? "" : rule); var pattern = new StringBuilder (); @@ -216,7 +212,31 @@ namespace Valum { } } - return this.regex (method, new Regex (pattern.str), (owned) cb); + return pattern.str; + } + + /** + * Create a Route for a given callback from a rule. + * + * Rule are scoped from the {@link Router.scope} fragment stack and + * compiled down to {@link GLib.Regex}. + * + * Rule start matching after the first '/' character of the request URI + * path. + * + * @since 0.3 + * + * @param method method matching this rule + * @param rule rule or 'null' to capture all possible paths + * @param cb handling callback + * + * @throws RegexError if the rule is incorrectly formed or a type is + * undefined in the 'types' mapping + * + * @return a builder upon the created {@link Route} object + */ + public Builder rule (string method, string? rule, owned HandlerCallback cb) throws RegexError { + return this.regex (method, new Regex (compile_rule (rule)), (owned) cb); } /** @@ -246,7 +266,7 @@ namespace Valum { // scope the route foreach (var scope in this.scopes.head) { - pattern.append (Regex.escape_string ("%s/".printf (scope))); + pattern.append_printf ("%s/", compile_rule (scope)); } pattern.append (regex.get_pattern ());