简体   繁体   English

Groovy 语法,命名块?

[英]Groovy syntax, named block?

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 并且有一些我根本不了解的常规语法,据我所知没有记录,因为我不知道它叫什么。

What are 'grails', 'views', and 'gsp' in the code below?下面代码中的“grails”、“views”和“gsp”是什么?

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

Thanks!谢谢! ps I feel like an idiot for not being able to figure this out... ps我觉得自己像个白痴,无法弄清楚这一点......

This is an example of DSL (domain specific language) code.这是 DSL(领域特定语言)代码的示例。 In Groovy, many DSLs are implemented as Groovy code, although sometimes it can look pretty funky.在 Groovy 中,许多 DSL 被实现为 Groovy 代码,尽管有时它看起来很时髦。 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:此代码块作为 Groovy 代码运行,如果添加省略的可选括号,则更清楚它是代码:

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')
         // ...
      })
   })
})

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.如果您在控制台中或作为其他 Groovy 代码的一部分或在 Grails 应用程序中运行它,它将失败,因为没有将闭包作为参数的 'grails' 方法,或类似的 'views' 或 'gsp' 方法,以及也没有setEncoding(String)方法。 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 代码运行时,代码通常在一个闭包中运行,该闭包的delegate设置为一个 DSL 帮助程序类,该类处理结果methodMissingpropertyMissing调用。

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 有效,则执行相应的工作,否则抛出MissingMethodException / MissingPropertyException ,或特定于 DSL 的异常,或以其他方式处理问题。

In this case, there's a configuration DSL handler that translates these method calls into config property calls.在这种情况下,有一个配置 DSL 处理程序将这些方法调用转换为配置属性调用。

There are several other DSLs in use in a Grails app that work the same way. Grails 应用程序中还使用了其他几种以相同方式工作的 DSL。 The mapping and constraints blocks in domain classes, the grails.project.dependency.resolution block in BuildConfig.groovy , etc. All are evaluated as Groovy code, and the missing method/property calls configure GORM mappings, constraint definitions, plugin dependencies, etc.域类中的mappingconstraints块, BuildConfig.groovygrails.project.dependency.resolution块等。 都被评估为 Groovy 代码,缺少的方法/属性调用配置 GORM 映射、约束定义、插件依赖等.

Programming Groovy 2 is a particularly good Groovy book for learning DSLs. Programming Groovy 2是一本特别好的用于学习 DSL 的 Groovy 书籍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM