Skip to content

acm0x/kotlin-script-engine-sample

Repository files navigation

Kotlin DSL and Script Enging Example

A simple project influenced by 12 days of Kotlin - Day 02 that shows

  1. How can you build DSL with kotlin syntax sugar
  2. How can you make that DSL a JSON-like.

Setup

dependencies {
    implementation(kotlin("scripting-jsr223-embeddable"))
}

DSL sugar

In short: trailing lambda, infix, unaryPlus.

All this operations just to drop brackets! But it is sufficient enough to build readable definitions.

sandwich {
   with type "toasted"
   bread = "baguette"
   
   fillings {
       +"cheese"
       +"ham"
   }
}

/!\ Hint: if you override String.unaryPlus() inside Sandwich class, you will have access to all its members.

Transferable DSL (JSON-like)

In short: Tried to answer a question: Can you replace JSON with DSL?

Pros: it is feasible to do following and s would be proper Sandwich object:

with(ScriptEngineManager().getEngineByExtension("kts")) {
        val s = eval(
            """import kon.*
sandwich {
    with type "toasted"
    bread = "baguette"
    
    fillings {
        +"cheese"
        +"ham"
    }
    """)
    }

Cons:

  • Extra line of imports (could be auto-added)
  • Obviously Not secure: the eval() would execute anything, no checks, no restrictions
  • if to be used on Android, would require heavyweight compiler-as-a-library.

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy