-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
308 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2021 - 2023 Sporta Technologies PVT LTD & the ZIO HTTP contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package zio.http | ||
|
||
import scala.language.experimental.macros | ||
import scala.reflect.macros.blackbox | ||
|
||
trait UrlInterpolator { | ||
|
||
implicit class UrlInterpolatorHelper(val sc: StringContext) { | ||
def url(args: Any*): URL = macro UrlInterpolatorMacro.url | ||
} | ||
} | ||
|
||
private[http] object UrlInterpolatorMacro { | ||
def url(c: blackbox.Context)(args: c.Expr[Any]*): c.Expr[URL] = { | ||
import c.universe._ | ||
c.prefix.tree match { | ||
case Apply(_, List(Apply(_, Literal(Constant(p: String)) :: Nil))) => | ||
val result = URL.decode(p) match { | ||
case Left(error) => c.abort(c.enclosingPosition, s"Invalid URL: ${error.getMessage}") | ||
case Right(url) => | ||
if (url.isAbsolute) { | ||
val uri = url.encode | ||
q"_root_.zio.http.URL.fromAbsoluteURI(new _root_.java.net.URI($uri)).get" | ||
} else { | ||
val uri = url.encode | ||
q"_root_.zio.http.URL.fromRelativeURI(new _root_.java.net.URI($uri)).get" | ||
} | ||
} | ||
c.Expr[URL](result) | ||
case Apply(_, List(Apply(_, staticPartLiterals))) => | ||
val staticParts = staticPartLiterals.map { case Literal(Constant(p: String)) => p } | ||
val injectedPartExamples = | ||
args.map { arg => | ||
val typ = arg.actualType | ||
if (typ =:= c.typeOf[String]) { | ||
"string" | ||
} else if (typ =:= c.typeOf[Byte]) { | ||
"123" | ||
} else if (typ =:= c.typeOf[Short]) { | ||
"1234" | ||
} else if (typ =:= c.typeOf[Int]) { | ||
"1234" | ||
} else if (typ =:= c.typeOf[Long]) { | ||
"1234" | ||
} else if (typ =:= c.typeOf[Boolean]) { | ||
"true" | ||
} else if (typ =:= c.typeOf[Float]) { | ||
"1.23" | ||
} else if (typ =:= c.typeOf[Double]) { | ||
"1.23" | ||
} else if (typ =:= c.typeOf[java.util.UUID]) { | ||
"123e4567-e89b-12d3-a456-426614174000" | ||
} else { | ||
c.abort(c.enclosingPosition, s"Unsupported type in url interpolator: $typ") | ||
} | ||
} | ||
val exampleParts = staticParts.zipAll(injectedPartExamples, "", "").flatMap { case (a, b) => List(a, b) } | ||
val example = exampleParts.mkString | ||
URL.decode(example) match { | ||
case Left(error) => | ||
c.abort(c.enclosingPosition, s"Invalid URL: ${error.getMessage}") | ||
case Right(url) => | ||
val parts = | ||
staticParts.map { s => Literal(Constant(s)) } | ||
.zipAll(args.map(_.tree), Literal(Constant("")), Literal(Constant(""))) | ||
.flatMap { case (a, b) => List(a, b) } | ||
|
||
val concatenated = | ||
parts.foldLeft[Tree](q"""""""") { case (acc, part) => | ||
q"$acc + $part" | ||
} | ||
|
||
val result = if (url.isAbsolute) { | ||
q"_root_.zio.http.URL.fromAbsoluteURI(new _root_.java.net.URI($concatenated)).get" | ||
} else { | ||
q"_root_.zio.http.URL.fromRelativeURI(new _root_.java.net.URI($concatenated)).get" | ||
} | ||
|
||
c.Expr[URL](result) | ||
} | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
zio-http/src/main/scala-3/zio/http/UrlInterpolator.scala
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,114 @@ | ||
/* | ||
* Copyright 2021 - 2023 Sporta Technologies PVT LTD & the ZIO HTTP contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package zio.http | ||
|
||
import scala.quoted.* | ||
|
||
trait UrlInterpolator { | ||
|
||
extension(inline sc: StringContext) { | ||
inline def url(inline args: Any*): URL = ${ UrlInterpolatorMacro.url('sc, 'args) } | ||
} | ||
|
||
} | ||
|
||
private[http] object UrlInterpolatorMacro { | ||
|
||
def url(sc: Expr[StringContext], args: Expr[Seq[Any]])(using Quotes): Expr[URL] = { | ||
import quotes.reflect.* | ||
import report.* | ||
|
||
val ctx = sc.valueOrAbort | ||
val staticParts = ctx.parts | ||
|
||
val argExprs = args match { | ||
case Varargs(exprs) => exprs | ||
case _ => errorAndAbort(s"Unexpected arguments", args) | ||
} | ||
|
||
val result = if (argExprs.isEmpty) { | ||
URL.decode(staticParts.mkString) match { | ||
case Left(error) => errorAndAbort(s"Invalid URL: $error", sc) | ||
case Right(url) => | ||
val uri = Expr(url.encode) | ||
if (url.isAbsolute) { | ||
'{ URL.fromAbsoluteURI(new java.net.URI($uri)).get } | ||
} else { | ||
'{ URL.fromRelativeURI(new java.net.URI($uri)).get } | ||
} | ||
} | ||
} else { | ||
val injectedPartExamples = | ||
argExprs.map { arg => | ||
val typ = arg.asTerm.tpe.asType | ||
typ match { | ||
case '[String] => | ||
"string" | ||
case '[Byte] => | ||
"123" | ||
case '[Short] => | ||
"1234" | ||
case '[Int] => | ||
"1234" | ||
case '[Long] => | ||
"1234" | ||
case '[Boolean] => | ||
"true" | ||
case '[Float] => | ||
"1.23" | ||
case '[Double] => | ||
"1.23" | ||
case '[java.util.UUID] => | ||
"123e4567-e89b-12d3-a456-426614174000" | ||
case _ => | ||
errorAndAbort(s"Injected field ${arg.show} has an unsupported type", arg) | ||
} | ||
} | ||
|
||
val exampleParts = staticParts.zipAll(injectedPartExamples, "", "").flatMap { case (a, b) => List(a, b) } | ||
val example = exampleParts.mkString | ||
|
||
URL.decode(example) match { | ||
case Left(error) => | ||
errorAndAbort(s"Invalid URL: $error", sc) | ||
case Right(url) => | ||
val parts = | ||
staticParts.map { s => Expr(s) } | ||
.zipAll(argExprs, Expr(""), Expr("")) | ||
.flatMap { case (a, b) => List(a, b) } | ||
|
||
val concatenated = | ||
parts.foldLeft[Expr[String]](Expr("")) { case (acc, part) => | ||
'{$acc + $part} | ||
} | ||
|
||
if (url.isAbsolute) { | ||
'{ | ||
URL.fromAbsoluteURI(new java.net.URI($concatenated)).get | ||
} | ||
} else { | ||
'{ | ||
URL.fromRelativeURI(new java.net.URI($concatenated)).get | ||
} | ||
} | ||
} | ||
} | ||
|
||
result | ||
} | ||
|
||
} |
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.