Christian Dräger
12/28/2020, 10:44 AM// lets assume i have some basic crud repository like:
interface MyFunkyRepository : CrudRepository<MyFunkyEntity, String>
@Entity(name = "funky")
@Table(name = "funky")
class MyFunkyEntity(
    @Id
    var id: String = UUID.randomUUID().toString(),
    @Column(name = "title", nullable = false)
    var title: String,
    @Column(name = "description", nullable = false)
    var description: String,
)
// and i have a service that is using the repository
@Service
class MyFunkyService(
    private val myFunkyRepository: MyFunkyRepository,
) {
    fun funkyAmount() = myFunkyRepository.count()
    fun addFunkiness(f: MyFunkyEntity) = myFunkyRepository.save(f)
}
// now i would like to write a little internal DSL to add entries to the repository by using the service
@DslMarker
annotation class MyFunkyDsl
@MyFunkyDsl
fun <T> addFunkyStuff(init: MyFunkyEntity.() -> T): T {
    // TODO: somehow inject MyFunkyService and store fields set by init callback
}
// so i could just use 'addFunkyStuff' lambda anywhere in my code like this:
class UseDslExample {
    fun someFunction() {
        addFunkyStuff {
            title = "xyz"
            description = "abcd"
        }
    }
}thana
12/28/2020, 12:12 PMstaticChristian Dräger
12/28/2020, 3:58 PMmarzelwidmer
12/28/2020, 4:12 PMmarzelwidmer
12/28/2020, 4:13 PM// Kotlin DSL
countries(
    country {
        name = "Switzerland"
        capital = "Bern"
        population = 8_603_900
        currency = "CHF"
    },
    country {
        name = "Spain"
        capital = "Madrid"
        population = 47_100_396
        currency = "EUR"
    }
)marzelwidmer
12/28/2020, 4:14 PMFluxToSOAPChristian Dräger
12/28/2020, 4:19 PM