开发者

Groovy DSL: handling labels

开发者 https://www.devze.com 2023-01-14 08:35 出处:网络
I\'m implementing in Groovy a DSL for some existing file format. In this format we have a construct like

I'm implementing in Groovy a DSL for some existing file format. In this format we have a construct like


group basic_test {
   test vplan_testing  {
         dir: global_storage;
   };
};

And here I have problem with this dir: global_storage - groovy considers "dir:" as a label, so I can't handle it.

Do you have an idea how I can receive some callback (getProperty, invokeMissingMethod开发者_Python百科) for this construct?

Thank you!


I don't believe you can achieve that this way, you need to change your dsl a bit to be able to capture that information. Here's how you could achieve that:

class Foo {
    static plan = {
        vplan_testing {
            dir 'global_storage'
        }
    }
}

def closure = Foo.plan
closure.delegate = this
closure()

def methodMissing(String name, Object args) {   
    println "$name $args"     
    if(args[0] instanceof Closure) 
       args[0].call()
} 

The output will be

dir [global_storage]

or you could defined you dsl this way:

class Foo {
    static plan = {
        vplan_testing {
            test dir:'global_storage'
        }
    }
}

replace "test" by something meaningful to you domain. In this case the output would be

test [[dir:global_storage]]

Hope this helps

-ken

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号