pwall567 / json-kotlin-schema-codegen

Code generation for JSON Schema (Draft 07)
MIT License
78 stars 14 forks source link

DSL Support #42

Open Dogacel opened 3 months ago

Dogacel commented 3 months ago

I think it makes sense to optionally generate DSLs as well. I was thinking about storing my k8s configuration on kotlin DSL, found about this codegen however it only generates kotlin, halfway there.

pwall567 commented 3 months ago

I don't what DSL you're referring to here. The code generator creates Kotlin and Java output, and it also produces Typescript interfaces (although with much more limited feature coverage).

But the output is all controlled by Mustache templates, so if you want to generate some other form of code you can provide your own templates.

This is not well documented, but if you create a template and parse it using the above-linked Mustache processor, you can then supply it to the code generator:

        val codeGenerator = CodeGenerator()
        codeGenerator.template = customTemplate

The "context object" for the template is a Target object, and you may need to investigate the source of this class, or examine the existing templates (in src/main/resources) to discover the properties you need to take part in template expansion.

I'm sorry if this is not very helpful, but your requirement is not what I had considered a primary use case for the code generator.

Dogacel commented 3 months ago

@pwall567

https://kotlinlang.org/docs/type-safe-builders.html

This is what I meant.

    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"}

            // an element with attributes and text content
            a(href = "https://kotlinlang.org") {+"Kotlin"}

            // mixed content
            p {
                +"This is some"
                b {+"mixed"}
                +"text. For more see the"
                a(href = "https://kotlinlang.org") {+"Kotlin"}
                +"project"
            }
            p {+"some text"}

            // content generated by
            p {
                for (arg in args)
                    +arg
            }
        }
    }