-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* --wip-- [skip ci] * implementation for apply * trailing slash fix * refactor: split path file * test: restructure tests * test: add failing test for Path * refactor: re-write path using Vector * refactor: remove LeadingSlash * fix: encoding error in Http * feat: implement `toString` using `Encode` * fix: update cookie spec Co-authored-by: Tushar Mathur <[email protected]>
- Loading branch information
1 parent
b30c68f
commit cc17a70
Showing
14 changed files
with
398 additions
and
250 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 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package zhttp.http | ||
|
||
final case class Path(segments: Vector[String], trailingSlash: Boolean) { self => | ||
def /(segment: String): Path = copy(segments :+ segment) | ||
|
||
def /:(name: String): Path = copy(name +: segments) | ||
|
||
def drop(n: Int): Path = copy(segments.drop(n)) | ||
|
||
def dropLast(n: Int): Path = copy(segments.reverse.drop(n)) | ||
|
||
def encode: String = { | ||
val ss = segments.filter(_.nonEmpty).mkString("/") | ||
ss match { | ||
case "" if trailingSlash => "/" | ||
case "" if !trailingSlash => "" | ||
case ss => "/" + ss + (if (trailingSlash) "/" else "") | ||
} | ||
} | ||
|
||
def initial: Path = copy(segments.init) | ||
|
||
def isEmpty: Boolean = segments.isEmpty && !trailingSlash | ||
|
||
def isEnd: Boolean = segments.isEmpty | ||
|
||
def last: Option[String] = segments.lastOption | ||
|
||
def nonEmpty: Boolean = !isEmpty | ||
|
||
def reverse: Path = copy(segments.reverse) | ||
|
||
def startsWith(other: Path): Boolean = segments.startsWith(other.segments) | ||
|
||
def take(n: Int): Path = copy(segments.take(n)) | ||
|
||
def toList: List[String] = segments.toList | ||
|
||
override def toString: String = encode | ||
} | ||
|
||
object Path { | ||
val empty: Path = Path(Vector.empty, false) | ||
|
||
/** | ||
* Decodes a path string into a Path. Can fail if the path is invalid. | ||
*/ | ||
def decode(path: String): Path = { | ||
val segments = path.split("/").toVector.filter(_.nonEmpty) | ||
segments.isEmpty match { | ||
case true if path.endsWith("/") => Path(Vector.empty, true) | ||
case true => Path(Vector.empty, false) | ||
case _ => Path(segments, path.endsWith("/")) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package zhttp.http | ||
|
||
private[zhttp] trait PathSyntax { module => | ||
val !! : Path = Path.empty | ||
|
||
object /: { | ||
def unapply(path: Path): Option[(String, Path)] = { | ||
for { | ||
head <- path.segments.headOption | ||
tail = path.segments.drop(1) | ||
} yield (head, path.copy(segments = tail)) | ||
} | ||
} | ||
|
||
object / { | ||
def unapply(path: Path): Option[(Path, String)] = { | ||
if (path.segments.length == 1) { | ||
Some(!! -> path.segments.last) | ||
} else if (path.segments.length >= 2) { | ||
val init = path.segments.init | ||
val last = path.segments.last | ||
Some(path.copy(segments = init) -> last) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.