72fb555c6e4e7aa7f847919a2b7ba9116e2d4861 — Josef Pospíšil 2 years ago 6303b16 master
Make routes ordered
3 files changed, 30 insertions(+), 14 deletions(-)

M test/suite001.janet
A test/suite002.janet
M trolley.janet
M test/suite001.janet => test/suite001.janet +6 -7
@@ 1,5 1,7 @@
(use spork/test)
(use trolley)
(use /trolley)

(start-suite 0)

(def config "Routes' config for all tests"
  {"/" :root

@@ 7,21 9,18 @@
   "/real-thing.json" :real-thing
   "/involved/:id/example/:example-id/detail/:detail-id" :involved})

(start-suite 0)

  (compile-routes config)
  "compile routes")

  (all |(= :core/peg (type $))
       (keys (compile-routes config)))
  "keys pegs")
       (map first (compile-routes config)))
  "items last pegs")

    (sort (values (compile-routes config)))
    (sort (map last (compile-routes config)))
    (sort @[:home :real-thing :involved :root]))
  "all values")

A test/suite002.janet => test/suite002.janet +14 -0
@@ 0,0 1,14 @@
(use spork/test)
(use /trolley)

(start-suite 1)

(def config
  {"/stats/download.css" :download
   "/stats/:interval" :interval})
    (deep= (lookup (compile-routes config) "/stats/download.css")
           [:download @{}])
    "lookup download")


M trolley.janet => trolley.janet +10 -7
@@ 15,8 15,11 @@
(def grammar
  "PEG grammar to match routes with"
    {:sep sep :pref pref :path ~(some ,chars)
     :param '(* :pref :path) :capture-path '(<- :path)
    {:sep sep
     :pref pref
     :path ~(some ,chars)
     :param '(* :pref :path)
     :capture-path '(<- :path)
     :main ~(some (* :sep
                     (+ (if :param ,(<-: :param :pref :capture-path))
                        (if :path ,(<-: :path :capture-path))

@@ 46,16 49,16 @@
(defn compile-routes
  "Compiles PEG grammar for all routes"
  (let [res @{}]
    (loop [[route action] :pairs routes]
      (when (string? route) (put res (compile-route route) action)))
  (def res @[])
  (loop [[route action] :pairs routes]
      (when (string? route) (array/push res [(compile-route route) action])))

(defn lookup
  "Looks up uri in routes and returns action and params for the matched route"
  [compiled-routes uri]
  (var matched [])
  (loop [[grammar action] :pairs compiled-routes :while (empty? matched)]
  (loop [[grammar action] :in compiled-routes :while (empty? matched)]
    (when-let [args (extract-args grammar uri)] (set matched [action args])))