Skip to content

Commit

Permalink
refactor: rename asString to encode in Path
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Jan 31, 2022
1 parent d88c0c4 commit 09b2a18
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zhttp/http/Cookie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ final case class Cookie(
expires.map(e => s"Expires=$e"),
maxAge.map(a => s"Max-Age=${a.toString}"),
domain.map(d => s"Domain=$d"),
path.map(p => s"Path=${p.asString}"),
path.map(p => s"Path=${p.encode}"),
if (isSecure) Some("Secure") else None,
if (isHttpOnly) Some("HttpOnly") else None,
sameSite.map(s => s"SameSite=${s.asString}"),
Expand Down
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zhttp/http/HttpError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object HttpError {
final case class Forbidden(msg: String = "Forbidden") extends HttpError(Status.FORBIDDEN, msg)

final case class NotFound(path: Path)
extends HttpError(Status.NOT_FOUND, s"""The requested URI "${path.asString}" was not found on this server\n""")
extends HttpError(Status.NOT_FOUND, s"""The requested URI "${path.encode}" was not found on this server\n""")

final case class MethodNotAllowed(msg: String = "Method Not Allowed")
extends HttpError(Status.METHOD_NOT_ALLOWED, msg)
Expand Down
10 changes: 5 additions & 5 deletions zio-http/src/main/scala/zhttp/http/PathModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ private[zhttp] trait PathModule { module =>
val Root = !!

sealed trait Path { self =>
final override def toString: String = this.encode

final def /(name: String): Path = Path(self.toList :+ name)

final def /:(name: String): Path = append(name)

final def append(name: String): Path = if (name.isEmpty) self else Path.Cons(name, self)

final def asString: String = {
final def drop(n: Int): Path = Path(self.toList.drop(n))

final def encode: String = {
def loop(self: Path): String = {
self match {
case Path.End => ""
Expand All @@ -25,8 +29,6 @@ private[zhttp] trait PathModule { module =>
if (result.isEmpty) "/" else result
}

final def drop(n: Int): Path = Path(self.toList.drop(n))

final def initial: Path = self match {
case Path.End => self
case Path.Cons(_, path) => path
Expand Down Expand Up @@ -58,8 +60,6 @@ private[zhttp] trait PathModule { module =>
final def take(n: Int): Path = Path(self.toList.take(n))

def toList: List[String]

final override def toString: String = this.asString
}

object Path {
Expand Down
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zhttp/http/URL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ object URL {
def asString(url: URL): String = {

def path: String = {
val encoder = new QueryStringEncoder(s"${url.path.asString}${url.fragment.fold("")(f => "#" + f.raw)}")
val encoder = new QueryStringEncoder(s"${url.path.encode}${url.fragment.fold("")(f => "#" + f.raw)}")
url.queryParams.foreach { case (key, values) =>
if (key != "") values.foreach { value => encoder.addParam(key, value) }
}
Expand Down
8 changes: 4 additions & 4 deletions zio-http/src/test/scala/zhttp/http/PathSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ object PathSpec extends DefaultRunnableSpec with HExitAssertion {
) +
suite("asString")(
test("a, b, c") {
val path = Path("a", "b", "c").asString
val path = Path("a", "b", "c").encode
assert(path)(equalTo("/a/b/c"))
} +
test("Path()") {
val path = Path().asString
val path = Path().encode
assert(path)(equalTo("/"))
} +
test("!!") {
val path = !!.asString
val path = !!.encode
assert(path)(equalTo("/"))
},
) +
Expand Down Expand Up @@ -69,7 +69,7 @@ object PathSpec extends DefaultRunnableSpec with HExitAssertion {
} +
suite("default")(
test("extract path 'name' /: name") {
val path = collect { case "name" /: name => name.asString }
val path = collect { case "name" /: name => name.encode }
assert(path(Path("name", "a", "b", "c")))(isSome(equalTo("/a/b/c")))
} +
test("extract paths 'name' /: a /: b /: 'c' /: !!") {
Expand Down

1 comment on commit 09b2a18

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 920204.76

Please sign in to comment.