使用 Curry 定义 Grails 标签

发布于 2024-08-03 13:02:01 字数 770 浏览 8 评论 0原文

我有一个 grails 标签库 TpTagLib,在其中我想定义 4 个新标签,它们仅在一个常量值上有所不同,所以我尝试使用 curry。 但有一个例外: groovy.lang.MissingPropertyException: No such property: attr for class: TpTagLib

有谁知道为什么会发生此异常? 这是代码:

def ifPermsTag = { permissions, attr, body ->
    def user = attr?.user ?: session.userInstance
    if( !user ) return false
    if( !securityService.hasPermissions(user,permissions) ) return false
        out << body()
    return true
}


def canAdminRequestmaps = ifPermsTag.curry(Permission.CAN_ADMIN_REQUESTMAPS)
def canAdminCorporations = ifPermsTag.curry(Permission.CAN_ADMIN_CORPS)
def canAdminUsers = ifPermsTag.curry(Permission.CAN_ADMIN_USERS)    
def canAdminDevices = ifPermsTag.curry(Permission.CAN_ADMIN_DEVICES)    

I have a grails tag library TpTagLib and in it I want to define 4 new tags that differ only in one constant value, so I tried to use curry.
But there is an exception: groovy.lang.MissingPropertyException: No such property: attr for class: TpTagLib

Does anyone have any idea why this exception occurs?
Here is the code:

def ifPermsTag = { permissions, attr, body ->
    def user = attr?.user ?: session.userInstance
    if( !user ) return false
    if( !securityService.hasPermissions(user,permissions) ) return false
        out << body()
    return true
}


def canAdminRequestmaps = ifPermsTag.curry(Permission.CAN_ADMIN_REQUESTMAPS)
def canAdminCorporations = ifPermsTag.curry(Permission.CAN_ADMIN_CORPS)
def canAdminUsers = ifPermsTag.curry(Permission.CAN_ADMIN_USERS)    
def canAdminDevices = ifPermsTag.curry(Permission.CAN_ADMIN_DEVICES)    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-08-10 13:02:01

很酷的技术。您只需将 ifPermsTag 设为私有,这样它就不会被视为可用标签方法的候选者:

private ifPermsTag = { permissions, attr, body ->
...
}

标签可以没有参数,或者“attr”参数,或者“attr”和“body”参数,但其他签名无效。

Cool technique. You just need to make ifPermsTag private so it's not considered a candidate to be a usable tag method:

private ifPermsTag = { permissions, attr, body ->
...
}

Tags can have no parameters, or an 'attr' parameter, or an 'attr' and 'body' parameters but other signatures are invalid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文