Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Better route validation #113

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 54 additions & 52 deletions cask/src/cask/internal/DispatchTrie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,44 @@ object DispatchTrie{
def construct[T, V](index: Int,
inputs: collection.Seq[(collection.IndexedSeq[String], T, Boolean)])
(validationGroups: T => Seq[V]): DispatchTrie[T] = {
val continuations = mutable.Map.empty[String, mutable.Buffer[(collection.IndexedSeq[String], T, Boolean)]]

val continuations = mutable.Map.empty[String, (String, mutable.Buffer[(collection.IndexedSeq[String], T, Boolean)])]
val terminals = mutable.Buffer.empty[(collection.IndexedSeq[String], T, Boolean)]

for((path, endPoint, allowSubpath) <- inputs) {
if (path.length < index) () // do nothing
else if (path.length == index) {
terminals.append((path, endPoint, allowSubpath))
} else if (path.length > index){
val buf = continuations.getOrElseUpdate(path(index), mutable.Buffer.empty)
buf.append((path, endPoint, allowSubpath))
else if (path.length == index) terminals.append((path, endPoint, allowSubpath))
else if (path.length > index){
val segment = path(index)
val buf = continuations.getOrElseUpdate(
if (segment.startsWith(":")) ":" else segment,
(segment, mutable.Buffer.empty)
)
buf._2.append((path, endPoint, allowSubpath))
}
}

for(group <- inputs.flatMap(t => validationGroups(t._2)).distinct) {
val groupTerminals = terminals.flatMap{case (path, v, allowSubpath) =>
validateGroups(inputs, terminals, continuations.map{case (k, (k2, v)) => (k, v)})(validationGroups)

DispatchTrie[T](
current = terminals.headOption.map(x => x._2 -> x._3),
children = continuations
.map{ case (k, (k2, vs)) => (k, (k2, construct(index + 1, vs)(validationGroups)))}
.toMap
)
}

def validateGroups[T, V](inputs: collection.Seq[(collection.IndexedSeq[String], T, Boolean)],
terminals0: collection.Seq[(collection.IndexedSeq[String], T, Boolean)],
continuations0: collection.Map[String, collection.Seq[(collection.IndexedSeq[String], T, Boolean)]])
(validationGroups: T => Seq[V]) = {
for (group <- inputs.flatMap(t => validationGroups(t._2)).distinct) {
val terminals = terminals0.flatMap { case (path, v, allowSubpath) =>
validationGroups(v)
.filter(_ == group)
.map{group => (path, v, allowSubpath, group)}
.map { group => (path, v, allowSubpath, group) }
}

val groupContinuations = continuations
val continuations = continuations0
.map { case (k, vs) =>
k -> vs.flatMap { case (path, v, allowSubpath) =>
validationGroups(v)
Expand All @@ -35,50 +51,35 @@ object DispatchTrie{
}
.filter(_._2.nonEmpty)

validateGroup(groupTerminals, groupContinuations)
}

DispatchTrie[T](
current = terminals.headOption.map(x => x._2 -> x._3),
children = continuations
.map{ case (k, vs) => (k, construct(index + 1, vs)(validationGroups))}
.toMap
)
}

def validateGroup[T, V](terminals: collection.Seq[(collection.Seq[String], T, Boolean, V)],
continuations: mutable.Map[String, mutable.Buffer[(collection.IndexedSeq[String], T, Boolean, V)]]) = {
val wildcards = continuations.filter(_._1(0) == ':')

def renderTerminals = terminals
.map{case (path, v, allowSubpath, group) => s"$group${renderPath(path)}"}
.mkString(", ")
val wildcards = continuations.filter(_._1(0) == ':')

def renderContinuations = continuations.toSeq
.flatMap(_._2)
.map{case (path, v, allowSubpath, group) => s"$group${renderPath(path)}"}
def render(values: collection.Seq[(collection.IndexedSeq[String], T, Boolean, V)]) = values
.map { case (path, v, allowSubpath, group) => s"$group /" + path.mkString("/") }
.sorted
.mkString(", ")

if (terminals.length > 1) {
throw new Exception(
s"More than one endpoint has the same path: $renderTerminals"
)
}
def renderTerminals = render(terminals)
def renderContinuations = render(continuations.toSeq.flatMap(_._2))

if (wildcards.size >= 1 && continuations.size > 1) {
throw new Exception(
s"Routes overlap with wildcards: $renderContinuations"
)
}
if (terminals.length > 1) {
throw new Exception(
s"More than one endpoint has the same path: $renderTerminals"
)
}

if (wildcards.size >= 1 && continuations.size > 1) {
throw new Exception(
s"Routes overlap with wildcards: $renderContinuations"
)
}

if (terminals.headOption.exists(_._3) && continuations.size == 1) {
throw new Exception(
s"Routes overlap with subpath capture: $renderTerminals, $renderContinuations"
)
if (terminals.headOption.exists(_._3) && continuations.size == 1) {
throw new Exception(
s"Routes overlap with subpath capture: $renderTerminals, $renderContinuations"
)
}
}
}

def renderPath(p: collection.Seq[String]) = " /" + p.mkString("/")
}

/**
Expand All @@ -90,7 +91,7 @@ object DispatchTrie{
* segments)
*/
case class DispatchTrie[T](current: Option[(T, Boolean)],
children: Map[String, DispatchTrie[T]]){
children: Map[String, (String, DispatchTrie[T])]){
final def lookup(remainingInput: List[String],
bindings: Map[String, String])
: Option[(T, Map[String, String], Seq[String])] = {
Expand All @@ -101,11 +102,12 @@ case class DispatchTrie[T](current: Option[(T, Boolean)],
current.map(x => (x._1, bindings, head :: rest))
case head :: rest =>
if (children.size == 1 && children.keys.head.startsWith(":")){
children.values.head.lookup(rest, bindings + (children.keys.head.drop(1) -> head))
val (k, (k2, v)) = children.head
v.lookup(rest, bindings + (k2.drop(1) -> head))
}else{
children.get(head) match{
case None => None
case Some(continuation) => continuation.lookup(rest, bindings)
case Some((k2, continuation)) => continuation.lookup(rest, bindings)
}
}

Expand All @@ -114,6 +116,6 @@ case class DispatchTrie[T](current: Option[(T, Boolean)],

def map[V](f: T => V): DispatchTrie[V] = DispatchTrie(
current.map{case (t, v) => (f(t), v)},
children.map { case (k, v) => (k, v.map(f))}
children.map { case (k, (k2, v)) => (k, (k2, v.map(f)))}
)
}
Loading
Loading