Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Domain-Specific Languages (DSLs) to Document API — the Kotlin DSL story

Lana
June 05, 2023

Using Domain-Specific Languages (DSLs) to Document API — the Kotlin DSL story

Lana

June 05, 2023
Tweet

More Decks by Lana

Other Decks in Technology

Transcript

  1. @_Unsolved_ June 06, 2023 Using Domain-Specific Languages (DSLs) to Document

    API — the Kotlin DSL story Lana Novikova, Writerside, JetBrains
  2. What is a DSL? DSL programming language higher level of

    abstraction specific class of problems particular domain
  3. External vs Internal DSLs External DSL Internal DSL Is a

    separate language Uses host language grammar, syntax and parser Has custom features Uses host language features
  4. Simplistic documentation DSL val content = StardustDslContentProvider { p {

    +"Hello DSL"} list { li { +"List item 1" } li { +"List item 2" } } this } A paragraph A list
  5. Simplistic documentation DSL val content = StardustDslContentProvider { table {

    tr { td { + "Parameter name" } td { + "Parameter type" } } tr { td { + "Value 1" } td { + "Value 2" } } this } A table
  6. Simplistic documentation DSL xml(""" <procedure title="Title"> <step>Step 1</step> <step>Step 2</step>

    <step>Step 3</step> </procedure> """.trimIndent() ) Other language injection
  7. Simplistic documentation DSL runConfigurations.forEach { runConfiguration -> chapter(id = runConfiguration.name,

    title = runConfiguration.name) { p { +runConfiguration.description} xml("""<include from="lib_fleet.xml" element-id="common-optional-properties"/>""") table(headerStyle = Table.HeaderStyle.NONE) { runConfiguration.properties.forEach { property -> tr { td { code { +property.key } } td { +property.value } } } } } } Combine the two
  8. But what is so cool about it? Docs-as-code on a

    new level Separate data and presentation Update the representation once without changing data Combine declarative and imperative approach to docs Extensibility Use ANY abilities that come up to mind
  9. Separation of content and presentation Separation of content and presentation

    Separation of data and presentation Lightweight languages Kotlin DSL
  10. No, but it is good to have a choice Write

    docs using markup Extend it using code constructions
  11. Use practices brought to us by developers • Refactorings •

    Completions • Dead code detection • Parameters/variables • Extracting into • Changing visibility
  12. A few examples where (else) you can go: • Parse

    and integrate any format out there, including XML, JSON, YAML, custom serialization formats, and even take objects from application runtime
  13. A few examples where (else) you can go: • Parse

    and integrate any format out there, including XML, JSON, YAML, custom serialization formats, and even take objects from application runtime • Add mappings and enrich with additional content
  14. A few examples where (else) you can go: • Parse

    and integrate any format out there, including XML, JSON, YAML, custom serialization formats, and even take objects from application runtime • Add mappings and enrich with additional content • Cycles and conditional statements
  15. A few examples where (else) you can go: • Parse

    and integrate any format out there, including XML, JSON, YAML, custom serialization formats, and even take objects from application runtime • Add mappings and enrich with additional content • Cycles and conditional statements • Use any JVM libraries, for example, natural language processing or readability analysis lib
  16. A few examples where (else) you can go: • Parse

    and integrate any format out there, including XML, JSON, YAML, custom serialization formats, and even take objects from application runtime • Add mappings and enrich with additional content • Cycles and conditional statements • Use any JVM libraries, for example, natural language processing or readability analysis lib • Apply tests and assertions for autogenerated parts
  17. It helps you to start coding more smoothly I will

    help you learn Lambda functions variables conditionals nullable Extension functions
  18. me { name = "Lana Novikova" twitter = "_Unsolved_" linkedin

    = "svetlana-novikova" } Come to the writerside
  19. jetbrains.com Let’s stay in touch — @_Unsolved_ Come to the

    writerside @svetlnovikova https://www.linkedin.com/in/svetlana-novikova/