summaryrefslogtreecommitdiffstats
path: root/src/main/scala/router.scala
blob: 9df7a2a42ea1553f6c908839b8588c5a4b80a54f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package router

import scala.collection.mutable.ArrayBuffer
import scala.util.matching.Regex

class Router[T] {
  val routes = new ArrayBuffer[Route[T]]()

  def addRoute (
    path:        String,
    target:      T,
    defaults:    Map[String, String] = Map(),
    validations: Map[String, Regex]  = Map()
  ) {
    routes += new Route(path, defaults, validations, target)
  }

  def insertRoute (
    path:        String,
    target:      T,
    defaults:    Map[String, String] = Map(),
    validations: Map[String, Regex]  = Map(),
    at:          Int                 = 0
  ) {
    routes insert (
      at min routes.length,
      new Route(path, defaults, validations, target)
    )
  }

  def route (path: String): Option[Match[T]] = {
    def testRoutes (
      components: Seq[String],
      routes:     List[Route[T]]
    ): Option[Match[T]] = routes match {
      case r :: rs => r.route(components) match {
        case Some(found) => Some(found)
        case None        => testRoutes(components, rs)
      }
      case _ => None
    }
    testRoutes(path.split("/"), routes.toList)
  }

  private class AmbiguousRouteMapping(
    mapping: Map[String, String],
    paths:   Seq[String]
  ) extends RuntimeException {
    override def getMessage (): String = {
      "Ambiguous path descriptor (specified keys " +
      mapping.keys.mkString(", ")                  +
      "): could match paths "                      +
      paths.mkString(", ")
    }
  }

  def uriFor (mapping: Map[String, String]): Option[String] = {
    // first remove all routes that can't possibly match
    // - if the route requires a variable component that doesn't exist in the
    //   mapping, then it can't match
    // - if the route contains a value for a variable component that doesn't
    //   pass the validation for that component, it can't match
    // - if the route contains a default value, and that component also exists
    //   in the mapping, then the values must match
    val possible = routes.flatMap(r => {
      r.pathWithMapping(mapping) match {
        case Some(path) => Some(r -> path)
        case None       => None
      }
    })

    possible.toList match {
      case Nil              => None
      case (r, path) :: Nil => Some(path)
      case rs               => {
        // then try to disambiguate the remaining possibilities
        // - we want the route with the fewest number of "extra" items in the
        //   mapping, after removing defaults and variable path components
        val possibleByRemainder = possible.groupBy { case (r, path) => {
          (mapping.keys.toSet -- r.defaults.keys.toSet -- r.variables).size
        } }
        val found = possibleByRemainder(possibleByRemainder.keys.min)
        found.toList match {
          case Nil              => None
          case (r, path) :: Nil => Some(path)
          case rs               =>
            throw new AmbiguousRouteMapping(mapping, rs.map(_._1.path))
        }
      }
    }
  }
}

class Route[T] (
  val path:        String,
  val defaults:    Map[String, String],
  val validations: Map[String, Regex],
  val target:      T
) {
  import Route._

  lazy val variables = components.flatMap(getVariableName)

  def route(
    parts:      Seq[String],
    components: Seq[String]         = components,
    mapping:    Map[String, String] = defaults
  ): Option[Match[T]] = {
    if (components.filter(!isOptional(_)).length == 0 && parts.length == 0) {
      Some(new Match[T](path, mapping, target))
    }
    else if (components.length == 0 || parts.length == 0) {
      None
    }
    else {
      components.head match {
        case Optional(name) => {
          if (validate(name, parts.head)) {
            route(parts.tail, components.tail, mapping + (name -> parts.head))
          }
          else {
            route(parts, components.tail, mapping)
          }
        }
        case Variable(name) => {
          if (validate(name, parts.head)) {
            route(parts.tail, components.tail, mapping + (name -> parts.head))
          }
          else {
            None
          }
        }
        case literal => parts.head match {
          case `literal` => route(parts.tail, components.tail, mapping)
          case _         => None
        }
      }
    }
  }

  override def toString = path

  def pathWithMapping (mapping: Map[String, String]): Option[String] = {
    val requiredDefaults = defaults.keys.filter { k =>
      mapping.isDefinedAt(k) && !variables.contains(k)
    }
    if (requiredDefaults.forall(k => defaults(k) == mapping(k)) &&
        requiredVariables.forall(mapping.isDefinedAt)) {
      val boundComponents = components.flatMap {
        case Optional(v) => {
          val component = (mapping get v).flatMap(validComponentValue(v, _))
          defaults get v match {
            case Some(default) => {
              component.flatMap {
                case `default` => None
                case c         => Some(c)
              }
            }
            case None => component
          }
        }
        case Variable(v) => validComponentValue(v, (mapping(v)))
        case literal     => Some(literal)
      }
      Some(boundComponents.mkString("/"))
    }
    else {
      None
    }
  }

  private lazy val components =
    path.split("/").filter(_.length > 0)

  private lazy val requiredVariables =
    components.filter(!isOptional(_)).flatMap(getVariableName)

  private lazy val hasVariable = variables.toSet

  private def isOptional (component: String) =
    Optional.findFirstIn(component).nonEmpty

  private def isVariable (component: String) =
    Variable.findFirstIn(component).nonEmpty

  private def getVariableName (component: String) = component match {
    case Variable(name) => Some(name)
    case _              => None
  }

  private def validate (name: String, component: String) =
    validations get name match {
      case Some(rx) => rx.findFirstIn(component).nonEmpty
      case None     => true
    }

  private def validComponentValue (name: String, component: String) =
    if (validate(name, component)) { Some(component) } else { None }
}

object Route {
  private val Optional = """^\?:(.*)$""".r
  private val Variable = """^\??:(.*)$""".r
}

class Match[T] (
  val path:    String,
  val mapping: Map[String, String],
  val target:  T
)