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

Refactor: rename asString to encode in Path #927

Merged
merged 2 commits into from
Feb 1, 2022
Merged
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ abstract class HttpRunnableSpec extends DefaultRunnableSpec { self =>
Http.fromFunctionZIO[Client.ClientRequest](params =>
for {
port <- DynamicServer.getPort
url = s"ws://localhost:$port${params.url.path.asString}"
url = s"ws://localhost:$port${params.url.path.encode}"
headerConv = params.addHeader(DynamicServer.APP_ID, id).getHeaders.toList.map(h => SHeader(h._1, h._2))
res <- send(basicRequest.get(uri"$url").copy(headers = headerConv).response(asWebSocketUnsafe))
} yield res,
Expand Down