Groogle

when Google met Groovy

... or why use Groovy to build a DSL

Presentation

Hi!

I’m Jorge Aguilera,

@pvidasoftware (PuraVida Software)

Last +-10 years using Groovy

Know your audience

  • raise your hand if you work/know Groovy?

  • raise your hand if you work/know Google API?

  • raise your hand if you know what a DSL is?

Agenda

  • DSLs

  • Groogle: origin, features and live demo

  • Why Groovy

  • Real use case of DSL

What’s a DSL?

DSL: Domain Specific Language

DSLs are small languages, focused on a particular aspect of a software system. You can’t build a whole program with a DSL, but you often use multiple DSLs in a system mainly written in a general purpose language.
https://martinfowler.com/books/dsl.html
— Martin Fowler

SQL

The (maybe) most famous DSL

select title, author, pages from books

Gradle

repositories {
    mavenCentral()
}
dependencies{
    api 'org.log4j:log4j:1.2.3'
}

Fluent

IntegrationFlows.from("example")
    .channel(this.inputChannel())
    .filter((Integer p) -> p > 0)
    .transform(Object::toString)
    .channel(MessageChannels.queue())
    .get();

Groogle, the origin

Google + Groovy = Groogle

  • started as a (lot of) Groovy Scripts for Google Calendar for a friend of mine

  • a collection of DSL for Google APIs

  • (the idea) simple to use for non-programmer users

  • spoiler: my friend never used it

Google "vs" Groogle

google

Groogle DSL

1

Groogle I

  • Open Source, https://gitlab.com/groogle

  • Groovy 3.0.10

  • Maven Central (com.puravida-software.groogle)

  • Drive, Sheet, Gmail, Calendar, People, Photos (started)

  • BigQuery ("in progress")

Groogle II

Live coding

A live coding session is better than 1000 slides

( 🤞🤞🤞 )

Groogle DSL

  • Drive (createFolder, withFolder, uploadFrom, downloadTo, removeFromDrive …​)

  • Sheet (createSpreadSheet, withSpreadSheet, createSheet, withSheet, …​)

  • Gmail (sendEmail)

  • Calendar (eachCalendarInList, withPrimaryCalendar, newCalendar)

  • BigQuery (rows, eachRow)

  • Photos (TODO!!)

Why Groovy for DSL?

  • Command chains

  • Operator overloading

  • Enhancing classes

  • Missing method/property

  • @DelegatesTo

  • and more …​

Command chains

Java
please(show).the(square_root).of(100)
paint( wall).with( List.of(red, green)).and(yellow)
Groovy
please show the square_root of 100
paint wall with red, green and yellow

Operator overloading

Various operators in Groovy are mapped onto regular method calls on objects.

So you can overwrite these methods, i.e:

def tobacco = new Tobaco()
def tomatoes = new Tomatoes()

def tomacco = tobacco + tomatoes  (1)

Tobaco.plus( object ){
    return new Tomacco(nicotine:90)
}
1™ Homer Simpson

Enhancing classes

  • extension and categories

use(TimeCategory)  {
    println 1.minute.from.now
    println 10.hours.ago

    def someDate = new Date()
    println someDate - 3.months
}

Categories are lexically bound.

Missing method/property

(do you remember A1='Title' example?)

def methodMissing(String name, def args) {
    return "this is me"
}

def propertyMissing(String name, value) {
    storage[name] = value
}

def propertyMissing(String name) {
    storage[name]
}

Closures

  • Similar to Java lambda, it’s an anonymous block of code

  • Can receive arguments, return values, and be assigned

  • Delegation: owner, delegate and this

  • Delegation Strategy: OWNER_FIRST, DELEGATE_FIRST, OWNER_ONLY, DELEGATE_ONLY

@DelegatesTo

With @DelegatesTo we can instruct to the compiler which class will execute the Closure

  • static compile

  • documentation

  • IDE completion

@DelegatesTo DSL

email dsl

@DelegatesTo Spec

email spec

@DelegatesTo Implementation

email

Recap

  • DSL can improve your API/Product

  • Groovy has a lot of feature to help you

  • @DelegatesTo as an assistant completion

  • Have fun and learn from everything

Ok, nice, but show me a real use case …​

Real use case: Nextflow DSL.

  • nextflow.io

  • Nextflow enables scalable and reproducible scientific workflows using software containers.

  • Write (a pipeline) once and run wherever (local, k8s, AWS, Google, Azure,…​)

nextflow

Nextflow

  • DSL to orchestrate workflows

  • Open Source, community driven

  • Groovy 3.0.10 (4.x coming soon)

  • GPars and others

nextflow stats

Questions ?