在 Scala 中使用运算符 orElse 的正确方法是什么?
What is correct way to use operator orElse in Scala?
我想写两个服务,然后用orElse让两个服务组合在一起,也就是service_one或者service_two。他们都是
部分函数。
服务一是:
val usersService = HttpService {
case request @ GET -> Root / "users" / IntVar(userId) =>
Ok("test")
}
服务二是:
val versionService = HttpService{
case req @ GET -> Root / "version" => {
val jsonmap = ("origin" -> req.remoteAddr.getOrElse("unknown ip"))
Ok(compact(render(jsonmap)))
}
}
然后我想结合在一起
val service = userService orElse versionService //the error happens here.
错误是:
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\ServiceApp.scala:46: value orElse is not a member of org.http4s.HttpService
[error] val service = usersService orElse versionService
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
如何组合 then make then 的工作方式与如果 case 匹配第一个服务的路径,则第一个服务工作,如果 case 匹配第二个服务的路径。第二个服务工作。
ps:关注人:
HttpService 对象定义如下:
type HttpService = Service[Request, Response]
object HttpService {
/** Alternative application which lifts a partial function to an `HttpService`,
* answering with a [[Response]] with status [[Status.NotFound]] for any requests
* where the function is undefined.
*/
def apply(pf: PartialFunction[Request, Task[Response]], default: HttpService = empty): HttpService =
Service.lift(req => pf.applyOrElse(req, default))
...
}
orElse
与PartialFunction
一起使用,例如:
val t1: PartialFunction[Int, String] = {case 1 => "I am 1"}
val t2: PartialFunction[Int, String] = {case 2 => "I am 2"}
val t = t1 orElse t2
t(1)
> I am 1
t(2)
> I am 2
作为 HttpService
应用方法签名接受 PartialFunction
,我想你可以这样做:
val usersService: PartialFunction[Request, Task[Response]] = {
case request @ GET -> Root / "users" / IntVar(userId) =>
Ok("test")
}
val versionService: PartialFunction[Request, Task[Response]] = {
case req @ GET -> Root / "version" => {
val jsonmap = ("origin" -> req.remoteAddr.getOrElse("unknown ip"))
Ok(compact(render(jsonmap)))
}
}
val service = HttpService {
usersService orElse versionService
}
我想写两个服务,然后用orElse让两个服务组合在一起,也就是service_one或者service_two。他们都是 部分函数。
服务一是:
val usersService = HttpService {
case request @ GET -> Root / "users" / IntVar(userId) =>
Ok("test")
}
服务二是:
val versionService = HttpService{
case req @ GET -> Root / "version" => {
val jsonmap = ("origin" -> req.remoteAddr.getOrElse("unknown ip"))
Ok(compact(render(jsonmap)))
}
}
然后我想结合在一起
val service = userService orElse versionService //the error happens here.
错误是:
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\ServiceApp.scala:46: value orElse is not a member of org.http4s.HttpService
[error] val service = usersService orElse versionService
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
如何组合 then make then 的工作方式与如果 case 匹配第一个服务的路径,则第一个服务工作,如果 case 匹配第二个服务的路径。第二个服务工作。
ps:关注人:
HttpService 对象定义如下:
type HttpService = Service[Request, Response]
object HttpService {
/** Alternative application which lifts a partial function to an `HttpService`,
* answering with a [[Response]] with status [[Status.NotFound]] for any requests
* where the function is undefined.
*/
def apply(pf: PartialFunction[Request, Task[Response]], default: HttpService = empty): HttpService =
Service.lift(req => pf.applyOrElse(req, default))
...
}
orElse
与PartialFunction
一起使用,例如:
val t1: PartialFunction[Int, String] = {case 1 => "I am 1"}
val t2: PartialFunction[Int, String] = {case 2 => "I am 2"}
val t = t1 orElse t2
t(1)
> I am 1
t(2)
> I am 2
作为 HttpService
应用方法签名接受 PartialFunction
,我想你可以这样做:
val usersService: PartialFunction[Request, Task[Response]] = {
case request @ GET -> Root / "users" / IntVar(userId) =>
Ok("test")
}
val versionService: PartialFunction[Request, Task[Response]] = {
case req @ GET -> Root / "version" => {
val jsonmap = ("origin" -> req.remoteAddr.getOrElse("unknown ip"))
Ok(compact(render(jsonmap)))
}
}
val service = HttpService {
usersService orElse versionService
}