English 中文(简体)
Groovy DSL: 设置关闭的属性
原标题:Groovy DSL: setting properties in closure

我想在我的 DSL 中执行一条规则的动态旗帜。 这里我要它看起来像:

Shipping("Standard") {
    active: true
    description: "some text"

    rules {
      ... define rules here
    }
}

我如何让一切沿着几个教程运行:

Script dslScript = new GroovyShell().parse(new File("Standard"))

dslScript.metaClass.Shipping = { String name, Closure cl ->
  ShippingDelegate delegate = new ShippingDelegate()
  delegate.name = name
  cl.delegate = delegate
  cl.setResolveStrategy Closure.DELEGATE_FIRST
  cl()
}

dslScript.run()

航运门户简单明了:

class ShippingDelegate {

  String name

  void rules(Closure cl) {
    ... do stuff here
  }
}

一切顺利,没有抱怨, 但我如何能进入活动或描述?

这个语法实际上会做什么? 它看起来像一个地图任务,但没有。 或者说, 精巧的编译者会把它当作一个不完整的永久操作员吗?

问题回答

我可以建议您对 DSL 作小改动, 以便您能够简化设计吗?

< 坚固 > 编辑后, 您的示例中并不清楚您是否有多个航运实例。 在我第二次尝试中, 我假设答案是 " 是 "/ 强 "

class ShippingRules {
    boolean active
    String description
    String name


    ShippingRules(String name) {
        this.name=name
    }

    def rules(Closure c) {
        c.delegate=this
        c()
    }
}



abstract class ShippingRulesScript extends Script {
    def shipppingRules =[]

    def shipping(String name, Closure c) {
        def newRules=new ShippingRules(name)
        shipppingRules << newRules
        c.delegate=newRules
        c()
    }
}

def cfg= new CompilerConfiguration(
    scriptBaseClass:ShippingRulesScript.name
)
Script dslScript = new GroovyShell(cfg).parse(new File("Standard"))

dslScript.run()

DSL 应修改为此 :

shipping("Standard") {
    active= true
    description= "some text"

    rules {
      ... define rules here
    }
}
shipping("International") {
    active= true
    description= "some text"

    rules {
      ... define rules here
    }
}

一.e. 将资本输给航运,使用分配,而不是冒号。

然后您就可以从您的 dslScript 航运变量中检索到航运规则。

disioner: I cant test my code right now, 所以代码中可能有一些打字, 但一般的想法是: 使用一个基础类, 向脚本提供您的规则和属性 。

I asked a similar question on Google+, see here.
The summary is: you can use the map syntax only on constructors (ctors) and as function parameters.

有趣的是,它没有提出例外。





相关问题
Which name for a "smart" dictionary (hashtable)?

I m looking for a good name for a custom dictionary which automatically initializes the value for a requested key if it doesn t exist, using a delegate. The indexer implementation should help ...

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project. I have defined a class: Class clsPayment Public Name End Class Set objPayment = New clsPayment objPayment.Name = "...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Python dictionary simple way to add a new key value pair

Say you have, foo = bar d = { a-key : a-value } And you want d = { a-key : a-value , foo : bar } e = { foo :foo} I know you can do, d[ foo ] = foo #Either of the following for e e = { foo :foo}...

Storing ints in a Dictionary

As I understand, in Objective-C you can only put Objects into dictionaries. So if I was to create a dictionary, it would have to have all objects. This means I need to put my ints in as NSNumber, ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签