Andrey Hihlovskiy
Professional blog on groovy, gradle, Java, Javascript and other stuff.
Tag Archives: DSL
groovy: switch statement and closure comprehension – nice for DSL
September 4, 2013
Posted by on It is rather easy to extend groovy switch statement with our own DSL:
def isGreaterThan(a, b) { a > b } def isGreaterThan(b) { return { a -> isGreaterThan(a, b) } } def isLessThan(a, b) { a < b } def isLessThan(b) { return { a -> isLessThan(a, b) } } def x = 5 def y = 6 switch(x) { case isGreaterThan(y): println "$x is greater than $y" break case isLessThan(y): println "$x is less than $y" break default: println "$x equals $y" }
The trick here is that single-argument versions of IsGreaterThan, IsLessThan return closures. Switch-statement “understands” closures: it passes it’s argument (x in our case) as a parameter to the closure and expects boolean result being returned from the closure.Same thing can be done via function currying, but it looks not so nice, as with function overload.
By the way, DSL stands for “Domain Specific Language”. See more information here: http://en.wikipedia.org/wiki/Domain-specific_language
Groovy DSL == thermonuclear way of writing XML
August 7, 2013
Posted by on import groovy.xml.MarkupBuilder String createEAD(Closure closure) { def writer = new StringWriter() def xml = new MarkupBuilder(writer) xml.mkp.xmlDeclaration(version: '1.0', encoding: 'UTF-8') xml.'ead:ead'('xmlns:ead': 'urn:isbn:1-931666-22-9') { closure.delegate = new Object() { def text(Map attrs, content) { def a = attrs.keySet().find { it in ['bold', 'italic', 'underline'] } if(a && attrs[a]) { xml.'ead:emph' render: a, { text attrs.findAll({ it.key != a }), content } } else text content } def text(content) { if(content instanceof String) xml.mkp.yield content else if(content instanceof Closure) content() } } closure() } return writer.toString() } println createEAD { text bold: true, italic: true, { text 'Hello, ' text underline: true, 'world!' } }
expected output:
<?xml version='1.0' encoding='UTF-8'?> <ead:ead xmlns:ead='urn:isbn:1-931666-22-9'> <ead:emph render='bold'> <ead:emph render='italic'>Hello, <ead:emph render='underline'>world!</ead:emph> </ead:emph> </ead:emph> </ead:ead>
Recent Comments