Groovy语法,命名为block? [英] Groovy syntax, named block?

查看:76
本文介绍了Groovy语法,命名为block?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习grails,但我完全不了解某些普通的语法,而且据我所知,该语法是未记载的,因为我不知道它叫什么.

I'm starting to learn grails and there is some groovy syntax that I don't get at all, and is undocumented as far as I can tell because I don't know what it's called.

以下代码中的"grails","views"和"gsp"是什么?

What are 'grails', 'views', and 'gsp' in the code below?

    grails {
      views {
        gsp { 
          encoding = 'UTF-8'
          // ...
        }
      }
    }

谢谢!p.s.我觉得自己像个白痴,无法解决这个问题……

Thanks! p.s. I feel like an idiot for not being able to figure this out...

推荐答案

这是DSL(域特定语言)代码的示例.在Groovy中,许多DSL被实现为Groovy代码,尽管有时看起来很时髦.此代码块作为Groovy代码运行,如果添加省略的可选括号,则更清楚地表明它是该代码:

This is an example of DSL (domain specific language) code. In Groovy, many DSLs are implemented as Groovy code, although sometimes it can look pretty funky. This code block is run as Groovy code, and it's more clear that it's code if you add in the omitted optional parentheses:

grails({
   views({
      gsp({ 
         encoding = 'UTF-8'
         // ...
      })
   })
})

还有更多,如果我们将属性设置调用替换为等效的方法调用

and more so if we replace the property set call to the equivalent method call

grails({
   views({
      gsp({ 
         setEncoding('UTF-8')
         // ...
      })
   })
})

如果您在控制台中,作为其他Groovy代码的一部分或在Grails应用程序中运行此命令,它将失败,因为没有"grails"方法将闭包作为参数,或者没有类似的"views"或"gsp"方法,也没有 setEncoding(String)方法.但是,当以DSL代码的形式运行时,该代码通常在闭包中运行,该闭包的 delegate 设置为DSL辅助类,该类处理产生的 methodMissing propertyMissing 通话.

If you ran this in a console or as part of other Groovy code or in a Grails app it would fail, because there's no 'grails' method taking a closure as an argument, or a similar 'views' or 'gsp' method, and there's no setEncoding(String) method either. But when run as DSL code, often the code is run inside a closure whose delegate is set to a DSL helper class that handles the resulting methodMissing and propertyMissing calls.

此帮助程序查看方法名称以及方法参数的数量和/或类型(或属性名称和值类型),如果它们对DSL有效,它将执行相应的工作,否则抛出 MissingMethodException / MissingPropertyException ,或特定于DSL的异常,或以其他方式处理问题.

This helper looks at the method name and the number and/or types of the method arguments (or property name and value type) and if they are valid for the DSL, it does the corresponding work, otherwise it throws a MissingMethodException/MissingPropertyException, or a DSL-specific exception, or handles the problem some other way.

在这种情况下,有一个配置DSL处理程序,可以将这些方法调用转换为config属性调用.

In this case, there's a configuration DSL handler that translates these method calls into config property calls.

Grails应用程序中正在使用其他几种DSL,它们的工作方式相同.域类中的 mapping 和constraints 块, BuildConfig.groovy`中的 grails.project.dependency.resolution 块,等等.所有这些都经过评估作为Groovy代码,缺少的方法/属性调用将配置GORM映射,约束定义,插件依赖性等.

There are several other DSLs in use in a Grails app that work the same way. The mapping and constraintsblocks in domain classes, thegrails.project.dependency.resolutionblock inBuildConfig.groovy`, etc. All are evaluated as Groovy code, and the missing method/property calls configure GORM mappings, constraint definitions, plugin dependencies, etc.

Programming Groovy 2 是一本特别好的Groovy书籍,用于学习DSL.

Programming Groovy 2 is a particularly good Groovy book for learning DSLs.

这篇关于Groovy语法,命名为block?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆