Coder Social home page Coder Social logo

autodsl's Introduction

AutoDsl for Kotlin

Auto-generates DSL (Domain Specific Language) for your Kotlin projects using annotations.

CircleCI Download

Table of Contents

  1. Documentation
  2. Download

Create expressive, immutable and type-safe DSL without boilerplate code:

person {
    name = "Juan"
    age = 34
    newAddress {
        street = "200 Celebration Bv"
        zipCode = 34747
        location {
            lat = 100.0
            lng = 100.0
        }
    }
    friends {
        +person {
            name = "Arturo"
            age = 28
        }
        +person {
            name = "Tiwa"
            age = 30
        }
    }
}

To generate the previous DSL you just need to provide your desired classes with @AutoDsl:

@AutoDsl
class Person(
    val name: String,
    val age: Int,
    val address: Address?,
    val friends: List<Person>?
)

@AutoDsl("newAddress") // set custom name for DSL
data class Address(      // can be used in data classes
    val street: String,
    val zipCode: Int,
    val location: Location?
)

@AutoDsl
class Location {
    val lat: Double
    val lng: Double

    constructor() {
        lat = 0.0
        lng = 0.0
    }

    // in multiple constructors you can specify which one to use.
    @AutoDslConstructor
    constructor(lat: Double, lng: Double) {
        this.lat = lat
        this.lng = lng
    }
}

AutoDsl will be generating a builder class and extension function for the annotated class providing this super expressive DSL.

For required parameters like name the DSL will throw an exception at runtime indicating exactly which field is missed. To make it optional just set the property as nullable with the question mark like friends. The value will be null in case it's not set.

Note: Default parameters in constructor is not currently supported as there is no way to get that value in the process to generate the code. There is a workaround that you can use explained in the wiki page.

Documentation

Visit the Wiki for a full list of features and more details: AutoDsl-Wiki

For more Examples

Download

Add repository:
repositories {
    maven { url "https://dl.bintray.com/juanchosaravia/autodsl" }
}
Add the dependencies
dependencies {
    api "com.juanchosaravia.autodsl:annotation:latest_version"
    kapt "com.juanchosaravia.autodsl:processor:latest_version"
}

Pending Features

  • Support external builders with new annotation @ManualDsl(type=MyBuilder::class).
  • Custom names for builders to improve integration with Java.

Debug

If you want to debug the processor do the following steps:

  1. Run this command:
    ./gradlew clean :app:build --no-daemon -Dorg.gradle.debug=true -Dkotlin.compiler.execution.strategy="in-process" -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=5005\,server=y\,suspend=n"
    
  2. In IntelliJ Idea go to Tools > Edit Configurations > press "+" icon in the left top corner. Add a new "Remote". Set a Name and check the "Single instance only" flag to true.
  3. Press "Debug" button to run the newly created "Remote" configuration.

Publish

  • Update version in release-brintray.gradle file:
libraryVersion = 'x.y.z'
  • Setup bintray user and pass in local.properties:
bintray.user=username
bintray.apikey=apikey
  • Run:
./gradlew :annotation:bintrayUpload
./gradlew :processor:bintrayUpload

License

Copyright 2018 Juan Ignacio Saravia

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.  

autodsl's People

Contributors

juanchosaravia avatar arthur7257 avatar

Watchers

Al B. avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.