Skip to content

DSL Code Generation

ozangunalp edited this page Aug 2, 2018 · 4 revisions

Installation

See latest version in maven central.

Maven

<!-- The dependency for the code generator -->
<dependency>
  <groupId>io.doov</groupId>
  <artifactId>doov-generator</artifactId>
  <version>LATEST</version>
</dependency>
<!-- The dependency on your model (see previous step) -->
<dependency>
  <groupId>io.doov</groupId>
  <artifactId>doov-sample-base</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

Gradle

dependencies {
  // The dependency on the model subproject (see previous step)
  compile project(':doov-sample-base')
}

Sample project

This documentation is based on the sample project in dOOv. You can replace the package name io.doov.sample by your package name as com.example.myapp and classes name Sample with MyApp.

Usage

Based on previous section Domain Model Annotation, we now have an annotated class named io.doov.sample.model.SampleModel with an EMAIL field ID from io.doov.sample.field.SampleFieldId.

The code generator will produce a specific DSL for your domain model.

Maven code generator

  <plugin>
    <groupId>io.doov</groupId>
    <artifactId>doov-generator</artifactId>
    <executions>
      <execution>
        <id>doov-generate-model</id>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
          <packageFilter>io.doov.sample</packageFilter>
          <fieldClass>io.doov.sample.field.SampleFieldId</fieldClass>
          <sourceClass>io.doov.sample.model.SampleModel</sourceClass>
        </configuration>
      </execution>
    </executions>
  </plugin>
  
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <generatedSourcesDirectory>src/generated/java</generatedSourcesDirectory>
    </configuration>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <configuration>
      <filesets>
        <fileset>
          <directory>${basedir}/src/generated</directory>
        </fileset>
      </filesets>
    </configuration>
  </plugin>

Then execute install on the project.

mvn clean install

The resulting classes will be in src/generated/java

Gradle code generator

Apply dOOv code generator plugin

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'io.doov', name: 'doov-gradle-generator', version: '2.+'
    }
}

apply plugin: 'io.doov.generator'

Configure the plugin execution

doovCodeGen {
  doovSampleModel {
    packageFilter = 'io.doov.sample'
    fieldClass = 'io.doov.sample.field.SampleFieldId'
    sourceClass = 'io.doov.sample.model.SampleModel'
  }
}

Then you can run the gradle build,

./gradlew build

or directly the task by its name.

./gradlew doovSampleModel

The resulting classes will be in build/doov/doovSampleModel

Resulting classes

You have generated:

  • io.doov.sample.field.dsl.DslSampleModel: your new DSL for your model :), that is the class you are going to use to write validation rules
  • io.doov.sample.field.SampleFieldInfo: internal class for field informations
  • io.doov.sample.model.SampleModelWrapper: internal class for the field accessors

Next

See Validation Rules