https://kotlinlang.org logo
#dsl
Title
# dsl
v

Venkat

03/12/2021, 11:33 AM
Based on this explanation https://kotlinlang.org/docs/type-safe-builders.html It looks like I can write html (almost similar style) inside kotlin code using DSL and it converts to actual html code, but I'm curious that is there any shortcut to create html DSL object out of string. For example, I have the below string (which exactly looks like html DSL object).
"""
html {
head {
title {+"XML encoding with Kotlin"}
}
body {
h1 {+"XML encoding with Kotlin"}
p  {+"this format can be used as an alternative markup to XML"}
}
}
"""
is there any easy way to convert this to Kotlin DSL object ? or it needs to parsed line by line?
m

Matteo Mirk

03/12/2021, 12:36 PM
Why do you need to start from a string? Using an (internal) DSL in Kotlin is beneficial because it’s actual code so it’s type-safe and checked by the compiler. If you put that code inside a string (to be parsed and compiled) you lose immediate type-safety in the IDE and add an unnecessary parsing step
v

Venkat

03/12/2021, 12:49 PM
If I want to store html dsl in a file and read back
It's not specific to html but generally .. I thought dsl will remove the parsing work
m

Matteo Mirk

03/12/2021, 12:59 PM
I think it’s a bad idea, really, because at that point why don’t you just save the HTML or a template on disk? The purpose of using a DSL is to stay in the code, to remain under compiler checks and safety. Let me repeat this once again: with a textual representation of the DSL you will lose this ability during development and it would be equivalent to managing HTML or a template: the checks will be shifted at runtime, instead of compile-time. Also, in the specific case of the HTML DSL, you’ll be doing extra work because the file would have to be parsed, compiled and executed at runtime in order to generate HTML…text, again.
v

Venkat

03/12/2021, 1:07 PM
So we can create html file via the meaningful dsl but it's possible to create dsl from already existing file
The reason I asked this question is, I have a data which is similar to kind of html. I'll get this data from outside to my code as a string and I also need to generate and send it to outside world as string. That generation part could be easily achieved by DSL, but the parsing part is not achievable .... That means I have to parse line by line
m

Matteo Mirk

03/12/2021, 2:06 PM
I honestly don’t understand your use case… 🙂
So, you have some data in an external file, you need to read and convert it to HTML to be sent out, is this right? When you’ve read your data you can use the HTML DSL in your code to produce the output. I still don’t get it why you would want to store the DSL part of your code in a file
v

Venkat

03/12/2021, 2:16 PM
Yeah I took HTML just as an example. You can serialize the data class to json and deserialize it back to data class obj in Kotlin. In the same way serialize dsl obj to string and deserialize back to dsl obj .. it could be any kind of dsl..
m

Matteo Mirk

03/12/2021, 2:40 PM
A DSL is not an object, you’re confusing things up 🙂 What you posted above (minus the “”") is a DSL, that is a mini-language on top of Kotlin syntax. What it produces is an object, that could have been constructed in other ways, without necessarily using a DSL. What you want to serialize instead is a piece of code. Fine, you can save it to a text file, but it’s not the same as de/serializing JSON data. If you want to deserialize it, you will need to compile and execute it at runtime, it’s not the same thing as parsing structured data. Honestly I still can’t understand why you want to do this… what do you want to achieve? What benefit will it bring to your program?
6 Views