Giter VIP home page Giter VIP logo

autodsl's Introduction

#[DEPRECATED] Unfortunately, since Kotlin 1.4.x this library doesn't work anymore. It has a strong dependency on me.eugeniomarletti.kotlin.metadata:kotlin-metadata lib which doesn't work anymore in 1.4.x to extract Kotlin Metadata.

If you think this feature should be back into 1.4.x then please let me know in the Issue section, probably we could invest some time trying to achieve the same with the new Kotlin Backend.

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 JCenter repository:
repositories {
    jcenter()
}
Add the dependencies

Download

dependencies {
    api "io.github.juanchosaravia.autodsl:annotation:latest_version"
    kapt "io.github.juanchosaravia.autodsl:processor:latest_version"
}

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

arthur7257 avatar jsglobant avatar juanchosaravia avatar manosbatsis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

autodsl's Issues

Problem with nested class

autodsl can't generate code properly when it marks a nested class.

A minimal example is

package me.ctknight.uploadmanager

import com.autodsl.annotation.AutoDsl

class Test {
  @AutoDsl
  class NestClass(val whatever: String)
}

it generates

// Code generated by AutoDsl. Do not edit.
package me.ctknight.uploadmanager

import com.autodsl.annotation.AutoDslMarker
import kotlin.String
import kotlin.Unit
import kotlin.properties.Delegates

fun nestClass(block: NestClassAutoDslBuilder.() -> Unit): Test.NestClass = NestClassAutoDslBuilder().apply(block).build()

@AutoDslMarker
class NestClassAutoDslBuilder() {
    var whatever: String by Delegates.notNull()

    fun withWhatever(whatever: String): NestClassAutoDslBuilder = this.apply { this.whatever = whatever}

    fun build(): Test.NestClass = NestClass(whatever)
}

and compiler complaints about NestClass not found.
e: NestClassAutoDslBuilder.kt: (17, 35): Unresolved reference: NestClass

I think solutions can be

  1. import the nested class

or

  1. call the constructor with fully qualified name just like return type.

I personally prefer the second one.

Android Sample App: compilation fails with latest Kotlin and Gradle version

When updating the included sample app to the latest Kotlin and Gradle version, one gets compilation errors:

...\autodsl\samples\android-autodsl\app\build\tmp\kapt3\stubs\debug\com\autodsl\sample\AnimSequence.java:7: error: There was an error while processing your annotated classes. error = 
public final class AnimSequence implements com.autodsl.sample.Anim {
             ^...\autodsl\samples\android-autodsl\app\build\tmp\kapt3\stubs\debug\com\autodsl\sample\AnimTogether.java:7: error: There was an error while processing your annotated classes. error = 
public final class AnimTogether implements com.autodsl.sample.Anim {
             ^...\autodsl\samples\android-autodsl\app\build\tmp\kapt3\stubs\debug\com\autodsl\sample\TranslateX.java:7: error: There was an error while processing your annotated classes. error = 
public final class TranslateX extends com.autodsl.sample.TranslateAnim {
             ^

Changed files are

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip\
  zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

build.gradle

buildscript {
    ext.kotlin_version = '1.4.10'
    ext.autodsl_version = '0.0.11'
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        ...
    }
}

Is this already known? Is there a fix or workaround?

Support Mixins(?)

Hey @juanchosaravia, I'd like to apply @AutoDsl on classes contained within a separate Gradle/Maven module or dependency, i.e. not in the current source.

Is something this in the roadmap? Should i try patching up a PR?

For the latter please note i'll probably have to:

  • Add an @AutoDslMixin annotation
  • Add a dependency to my kotlin-utils' kapt module

The implementation would allow something like:

@AutoDslMixin(
      dslName = "whatever", // Default: "person"
      dslSource = Person::class)
class PersonMixin

about selead class

sealed class Test {
    @AutoDsl
    data class TestOne(val route: Route) : Test()

    @AutoDsl
    data class TestTwo(val route: Route) : Test()
}

actual the auto dsl not has support to selead class, there is any plan to add this support?

About collection handling

Hey!

Really nice idea, library looks prettry good as well, I had found my self doing the same boleirplate code that this library smothly generate :)

I only have one question why use + plus symbol for collections, IMHO it looks verbose.

 friends {
        +person {
            name = "Arturo"
            age = 28
        }
        +person {
            name = "Tiwa"
            age = 30
        }
    }

over

 friends {
        person {
            name = "Arturo"
            age = 28
        }
        person {
            name = "Tiwa"
            age = 30
        }
    }

(In my code I only instance builder with empty mutable collection and then add each item so generated dsl looks cleaner)

Not nullable property with default value will not work

I tried to extend test Person class:
@AutoDsl // indicates to create an associated DSL for this class class Person( val name: String, val age: Int, val address: Address?, val friends: List<Person>?, @AutoDslCollection(concreteType = TreeSet::class) // specify concrete type val keys: Set<String>?, val items: Map<String, String> = emptyMap() )
It doesn't work. Is it a bug?

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.