-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Route: Only encode placeholders in type parameter
Previously, static path elements were encoded in `Route`'s `HList`. This prevented conditional routes such as the following: ```scala if (isProduction) Root / "api" / "v2.0" else Root ``` Change the implementation such that only placeholders (e.g. `Arg[String]` or `Fragment[Int]`) are encoded in `Route`'s type parameter. Also, simplify the design by using tuples instead of `HList`s. As a consequence, the compile-time and run-time footprint is reduced. **Detailed changes:** - Use tuples instead of `HList`s - Eliminate all type casts - Drop all external dependencies (Shapeless, Cats) - Argument and fragment placeholders are now encoded in `Route`'s type parameter - Change fragment delimiter from `#` to `$` - Route parsing does not fail anymore when additional path elements are specified - Drop `ParamOpt[T]` in favour of `Param[Option[T]]` - Optional route arguments can now be populated with `None` instead of `Option.empty[T]` - Remove all type aliases - Custom `Codec`s can read/emit optional arguments Closes #14. Closes #28.
- Loading branch information
Showing
20 changed files
with
478 additions
and
538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
package trail | ||
|
||
import scala.annotation.implicitNotFound | ||
case class Arg[T]()(implicit val codec: Codec[T]) { | ||
override def equals(o: Any): Boolean = | ||
o match { | ||
case a: Arg[T] => a.codec.equals(codec) | ||
case _ => false | ||
} | ||
|
||
@implicitNotFound("${T} cannot be used as a path element. Define an instance StaticElement[${T}].") | ||
class PathElement[T, U](val f: T => U) | ||
override def hashCode(): Int = ("trail.Arg", codec).hashCode() | ||
} | ||
|
||
case class Param[T](name: String)(implicit val codec: Codec[T]) { | ||
override def equals(o: Any): Boolean = | ||
o match { | ||
case p: Param[T] => p.name.equals(name) && p.codec.equals(codec) | ||
case _ => false | ||
} | ||
|
||
class StaticElement[T](f: T => String) extends PathElement[T, String](f) | ||
override def hashCode(): Int = ("trail.Param", name, codec).hashCode() | ||
} | ||
|
||
object PathElement { | ||
implicit def argElement[T] = new PathElement[Arg[T], Arg[T]](identity) | ||
case class Fragment[T]()(implicit val codec: Codec[T]) { | ||
override def equals(o: Any): Boolean = | ||
o match { | ||
case f: Fragment[T] => f.codec.equals(codec) | ||
case _ => false | ||
} | ||
|
||
implicit object StringElement extends StaticElement[String ](identity) | ||
implicit object BooleanElement extends StaticElement[Boolean](_.toString) | ||
implicit object IntElement extends StaticElement[Int ](_.toString) | ||
implicit object LongElement extends StaticElement[Long ](_.toString) | ||
override def hashCode(): Int = ("trail.Fragment", codec).hashCode() | ||
} |
Oops, something went wrong.