问题描述
我是 Scala 和 playframework 的新手.有人可以将下面的以下片段翻译成简单的英语吗?对于上下文,可以在这里找到:http://www.playframework.org/documentation/2.0.4/ScalaSecurity
I'm new to scala and the playframework. Can somebody please translate the following snippet below into plain english? For context its found here: http://www.playframework.org/documentation/2.0.4/ScalaSecurity
/**
* This method shows how you could wrap the withAuth method to also fetch your user
* You will need to implement UserDAO.findOneByUsername
*/
def withUser(f: User => Request[AnyContent] => Result) = withAuth { username => implicit request =>
UserDAO.findOneByUsername(username).map { user =>
f(user)(request)
}.getOrElse(onUnauthorized(request))
}
推荐答案
第 1 部分:首先让我们解决柯里化语法:
withUser
是一个接受类型为 User => 的柯里化函数
.它接受一个 f
的方法.请求[任何内容] =>结果User
对象并返回另一个接受 Request
并返回一个 Result
的函数.分解它,如果 f
是那个函数,那么:
withUser
is a method that takes a curried function f
of type User => Request[AnyContent] => Result
. It takes a User
object and returns another function that takes a Request
and returns a Result
. Breaking it down, if f
is that function then:
val g = f(user) // g is a function
val result = g(request) // returns a result
// same as:
val result = f(user)(request)
实际上f
就像一个接受两个参数的函数,但不是调用f(a, b)
,而是调用f(a)(b)
.
Practically speaking f
is just like a function that takes two parameters but instead of calling f(a, b)
you call f(a)(b)
.
withAuth
也是一个接受柯里化函数的方法.它与 withUser
的类型几乎相同.
withAuth
is also a method that takes a curried function. It has almost the same type as withUser
.
第 2 部分:现在如何使用这些方法:
正如此处所述,play通过告诉它如何转换Request
对象转换为 Result
对象.
As explained here, play makes you defined your application logic by telling it how to transform Request
objects into Result
objects.
withAuth
是一个辅助函数,负责为您进行身份验证并方便地检索用户名.所以你像这样使用它:
withAuth
is a helper function that takes care of the authentication for you and conveniently retrieves the username. So you use it like this:
def index = withAuth { username => implicit request =>
Ok(html.index(username))
}
它返回一个接受 Request
并返回一个 Result
的函数,这正是游戏所需要的.但它需要的是一个柯里化函数(需要一个用户名)并返回一个函数(需要一个请求).请求参数被标记为隐式,因此它可以隐式传递给任何需要隐式请求参数的函数/方法调用.出于本说明的目的,只需忽略 implicit
关键字.
It returns a function that takes a Request
and returns a Result
, which is what play needs. But what it takes is a curried function (that takes a username) and return a function (that takes a request). The request parameter is marked as implicit so it can be passed implicitly to any function/method call that needs an implicit request parameter. For the purpose of this explanation, just ignore the implicit
keyword.
第 3 部分:withUser
好吧,它的签名类似于 withAuth
并且目标是以相同的方式使用它,除了第一个参数是 User
而不是 .所以它必须采用 User =>请求 =>结果
.请求特征采用一个类型参数,指示其内容的类型.这是AnyContent
.所以 withUser
参数的正确类型是 User =>请求[任何内容] =>结果
.这意味着您可以像这样使用它:
Well, its signature is similar to withAuth
and the goal is for it to be used in the same way except the first parameter will be a User
instead of a String
. So it has to take a User => Request => Result
. The request trait takes a type parameter which indicates the type of its content. Here it is AnyContent
. So the correct type for the argument of withUser
is User => Request[AnyContent] => Result
. That means you will be able to use it like this:
withUser { user => implicit request =>
// do something with user and request to return a result
}
如果你看一下withUser
的定义,它所做的就是调用withAuth
:
If you look at the definition of withUser
, all it does is to call withAuth
:
def withUser(f: User => Request[AnyContent] => Result) = withAuth {
// ...
}
因此它将返回与 withAuth
相同的类型,这意味着它将返回一个将 Request
转换为 Result
的函数(参见部分2 以上).这意味着我们将能够像这样使用它:
So it will return the same type as withAuth
which means it will return a function that turns a Request
into a Result
(see Part 2 above). Which means we will be able to use it like this:
def index = withUser { user => implicit request =>
Ok(html.index(user))
}
作为 withAuth
的参数传递的是一个柯里化函数.我引入了中间 val
以便您可以遵循类型:
What is passed as an argument of withAuth
is a curried function. I introduced intermediate val
so that you can follow the types:
username => // first param is the username as a String
implicit request => // second param is the request
// here we have to return a Result...
// we lookup the user - we may not have one:
val userOption: Option[User] = UserDAO.findOneByUsername(username)
// f is the code block that will be provided inside withUser { f }
// Part 1 explains the f(user)(request) syntax
// We call f to get a result, in the context of the Option monad
val resultOption: Option[Result] = userOption.map(user => f(user)(request))
// if we have a result, return it; otherwise return an error.
resultOption.getOrElse(onUnauthorized(request))
这篇关于需要以下 Scala 片段的简单英文翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!