Skip to content

Commit

Permalink
rename apiName to traitName
Browse files Browse the repository at this point in the history
  • Loading branch information
cornerman committed Jun 25, 2024
1 parent 37d1a4c commit 31fe558
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ val router = Router[ByteBuffer, Future].route[Api](ApiImpl)

Use it to route requests to your Api implementation:
```scala
val result = router(Request[ByteBuffer](Method(apiName = "Api", methodName = "fun"), bytes))
val result = router(Request[ByteBuffer](Method(traitName = "Api", methodName = "fun"), bytes))
// Now result contains the serialized Int result returned by the method ApiImpl.fun
```

Expand Down Expand Up @@ -223,7 +223,7 @@ In the above examples, we used the type `ByteBuffer` to select the serialization

Sloth derives all information about an API from a scala trait. For example:
```scala
// @Name("apiName")
// @Name("traitName")
trait Api {
// @Name("funName")
def fun(a: Int, b: String)(c: Double): F[Int]
Expand All @@ -240,7 +240,7 @@ When calling `router.route[Api](impl)`, a macro generates a function that maps a

```scala
{ (method: sloth.Method) =>
if (method.apiName = "Api") method.methodName match {
if (method.traitName = "Api") method.methodName match {
case "fun" => Some({ payload =>
// deserialize payload
// call Api implementation impl with arguments
Expand Down
2 changes: 1 addition & 1 deletion http4sClient/src/main/scala/HttpRpcTransport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ case class HttpRequestConfig(
) {
def toRequest[F[_]](method: sloth.Method, entityBody: EntityBody[F]): Request[F] = Request[F](
method = Method.POST,
uri = baseUri / method.apiName / method.methodName,
uri = baseUri / method.traitName / method.methodName,
httpVersion = httpVersion,
headers = headers,
body = entityBody,
Expand Down
8 changes: 4 additions & 4 deletions http4sServer/src/main/scala/HttpRpcRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ object HttpRpcRoutes {

HttpRoutes[F] { request =>
request.pathInfo.segments match {
case Vector(apiName, methodName) =>
val method = sloth.Method(apiName.decoded(), methodName.decoded())
case Vector(traitName, methodName) =>
val method = sloth.Method(traitName.decoded(), methodName.decoded())
val result = router(request).get(method).traverse { f =>
request.as[PickleType].flatMap { payload =>
f(payload) match {
Expand Down Expand Up @@ -54,8 +54,8 @@ object HttpRpcRoutes {

HttpRoutes[F] { request =>
request.pathInfo.segments match {
case Vector(apiName, methodName) =>
val method = sloth.Method(apiName.decoded(), methodName.decoded())
case Vector(traitName, methodName) =>
val method = sloth.Method(traitName.decoded(), methodName.decoded())
val result = router(request).get(method).traverse { f =>
request.as[String].flatMap { payload =>
f(payload) match {
Expand Down
2 changes: 1 addition & 1 deletion jsdomClient/src/main/scala/HttpRpcTransport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object HttpRpcTransport {
def apply[F[_]: Async](config: F[HttpRequestConfig]): RequestTransport[String, F] = new RequestTransport[String, F] {
override def apply(request: Request[String]): F[String] = for {
config <- config
url = s"${config.baseUri}/${request.method.apiName}/${request.method.methodName}"
url = s"${config.baseUri}/${request.method.traitName}/${request.method.methodName}"
requestArgs = new dom.RequestInit { headers = config.headers.toJSDictionary; method = dom.HttpMethod.POST; body = request.payload }
result <- Async[F].fromThenable(Async[F].delay[js.Thenable[String]](dom.fetch(url, requestArgs).`then`[String](_.text())))
} yield result
Expand Down
4 changes: 2 additions & 2 deletions sloth/src/main/scala-2/internal/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Translator[C <: Context](val c: C) {

object implicits {
implicit val liftMethod: Liftable[Method] =
Liftable[Method]{ r => q"new _root_.sloth.Method(${r.apiName}, ${r.methodName})" }
Liftable[Method]{ r => q"new _root_.sloth.Method(${r.traitName}, ${r.methodName})" }
}

def abort(msg: String) = c.abort(c.enclosingPosition, msg)
Expand Down Expand Up @@ -209,7 +209,7 @@ object RouterMacro {
val impl = $impl

implRouter.orElse { method =>
if (method.apiName == $traitPathPart) {
if (method.traitName == $traitPathPart) {
method.methodName match {
case ..$methodCases
case _ => None
Expand Down
4 changes: 2 additions & 2 deletions sloth/src/main/scala-3/internal/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import scala.quoted.runtime.StopMacroExpansion
private implicit val toExprMethod: ToExpr[Method] = new ToExpr[Method] {
def apply(path: Method)(using Quotes): Expr[Method] = {
import quotes.reflect._
'{ Method(${Expr(path.apiName)}, ${Expr(path.methodName)}) }
'{ Method(${Expr(path.traitName)}, ${Expr(path.methodName)}) }
}
}

Expand Down Expand Up @@ -318,7 +318,7 @@ object RouterMacro {

'{
${prefix}.orElse { method =>
if (method.apiName == ${Expr(traitPathPart)}) {
if (method.traitName == ${Expr(traitPathPart)}) {
${Match(
'{method.methodName}.asTerm,
methodCases('{method}.asTerm) :+ CaseDef(Wildcard(), None, '{ None }.asTerm)
Expand Down
6 changes: 3 additions & 3 deletions sloth/src/main/scala/Request.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package sloth

case class Method(apiName: String, methodName: String)
case class Method(traitName: String, methodName: String)

case class Request[T](method: Method, payload: T) {
@deprecated("Use .method instead", "0.8.0")
def path: List[String] = List(method.apiName, method.methodName)
def path: List[String] = List(method.traitName, method.methodName)
}
object Request {
@deprecated("""Use Request(Method("Api", "method"), payload) instead""", "0.8.0")
def apply[T](path: List[String], payload: T): Request[T] = Request(methodFromList(path), payload)

private[sloth] def methodFromList(path: List[String]) = path match {
case List(traitName, methodName) => Method(apiName = traitName, methodName = methodName)
case List(traitName, methodName) => Method(traitName = traitName, methodName = methodName)
case _ => Method("", "")
}
}

0 comments on commit 31fe558

Please sign in to comment.