Skip to content

Commit

Permalink
Element overriding (#30)
Browse files Browse the repository at this point in the history
* allow for overriding of elements (partial support for #23 and #22), and update knit to allow for TS Compile to be disabled

* make TsMapTypeConverter more clear, split inline/non-inline

* code tidy

* fix bug where overrides weren't consistently found and applied if the target was nullable
  • Loading branch information
aSemy authored Apr 13, 2022
1 parent b2f134b commit 611f642
Show file tree
Hide file tree
Showing 10 changed files with 496 additions and 56 deletions.
31 changes: 31 additions & 0 deletions docs/code/example/example-customising-output-01.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This file was automatically generated from customising-output.md by Knit tool. Do not edit.
@file:Suppress("PackageDirectoryMismatch", "unused")
package dev.adamko.kxstsgen.example.exampleCustomisingOutput01

import kotlinx.serialization.*
import dev.adamko.kxstsgen.*

import kotlinx.serialization.builtins.serializer
import dev.adamko.kxstsgen.core.*

@Serializable
data class Item(
val price: Double,
val count: Int,
)

fun main() {
val tsGenerator = KxsTsGenerator()

tsGenerator.descriptorOverrides +=
Double.serializer().descriptor to TsDeclaration.TsTypeAlias(
id = TsElementId("Double"),
typeRef = TsTypeRef.Declaration(
id = TsElementId("double"),
parent = null,
nullable = false,
)
)

println(tsGenerator.generate(Item.serializer()))
}
32 changes: 32 additions & 0 deletions docs/code/example/example-customising-output-02.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file was automatically generated from customising-output.md by Knit tool. Do not edit.
@file:Suppress("PackageDirectoryMismatch", "unused")
package dev.adamko.kxstsgen.example.exampleCustomisingOutput02

import kotlinx.serialization.*
import dev.adamko.kxstsgen.*

import kotlinx.serialization.builtins.serializer
import dev.adamko.kxstsgen.core.*

@Serializable
data class ItemHolder(
val item: Item,
)

@Serializable
data class Item(
val count: UInt? = 0u,
)

fun main() {
val tsGenerator = KxsTsGenerator()

tsGenerator.descriptorOverrides +=
UInt.serializer().descriptor to TsDeclaration.TsTypeAlias(
id = TsElementId("kotlin.UInt"),
typeRef = TsTypeRef.Declaration(id = TsElementId("uint"), parent = null, nullable = false)
)

println(tsGenerator.generate(ItemHolder.serializer()))
}

39 changes: 39 additions & 0 deletions docs/code/example/example-customising-output-03.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file was automatically generated from customising-output.md by Knit tool. Do not edit.
@file:Suppress("PackageDirectoryMismatch", "unused")
package dev.adamko.kxstsgen.example.exampleCustomisingOutput03

import kotlinx.serialization.*
import dev.adamko.kxstsgen.*

import kotlinx.serialization.builtins.serializer
import dev.adamko.kxstsgen.core.*


@Serializable
@JvmInline
value class Tick(val value: UInt)

@Serializable
data class ItemHolder(
val item: Item,
val tick: Tick?,
)

@Serializable
data class Item(
val count: UInt? = 0u,
)

fun main() {
val tsGenerator = KxsTsGenerator()

tsGenerator.descriptorOverrides +=
UInt.serializer().descriptor to TsDeclaration.TsTypeAlias(
id = TsElementId("kotlin.UInt"),
typeRef = TsTypeRef.Declaration(id = TsElementId("uint"), parent = null, nullable = false)
)

println(tsGenerator.generate(ItemHolder.serializer()))
}


10 changes: 10 additions & 0 deletions docs/code/knit-test.ftl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<#--@formatter:off-->
<#-- @ftlvariable name="test.name" type="java.lang.String" -->
<#-- @ftlvariable name="test.package" type="java.lang.String" -->
// This file was automatically generated from ${file.name} by Knit tool. Do not edit.
Expand All @@ -24,7 +25,9 @@ class ${test.name} : FunSpec({

test("expect actual matches TypeScript") {
actual.shouldBe(
<#if case.param != "TS_COMPILE_OFF">
// language=TypeScript
</#if>
"""
<#list case.lines as line>
|${line}
Expand All @@ -34,9 +37,16 @@ class ${test.name} : FunSpec({
)
}

<#if case.param == "TS_COMPILE_OFF">
// TS_COMPILE_OFF
// test("expect actual compiles").config(tags = tsCompile) {
// actual.shouldTypeScriptCompile()
// }
<#else>
test("expect actual compiles").config(tags = tsCompile) {
actual.shouldTypeScriptCompile()
}
</#if>
}
<#sep>

Expand Down
97 changes: 97 additions & 0 deletions docs/code/test/CustomisingOutputTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// This file was automatically generated from customising-output.md by Knit tool. Do not edit.
@file:Suppress("JSUnusedLocalSymbols")
package dev.adamko.kxstsgen.example.test

import dev.adamko.kxstsgen.util.*
import io.kotest.core.spec.style.*
import io.kotest.matchers.*
import kotlinx.knit.test.*

class CustomisingOutputTest : FunSpec({

tags(Knit)

context("ExampleCustomisingOutput01") {
val actual = captureOutput("ExampleCustomisingOutput01") {
dev.adamko.kxstsgen.example.exampleCustomisingOutput01.main()
}.normalizeJoin()

test("expect actual matches TypeScript") {
actual.shouldBe(
"""
|export interface Item {
| price: Double;
| count: number;
|}
|
|export type Double = double; // assume that 'double' will be provided by another library
""".trimMargin()
.normalize()
)
}

// TS_COMPILE_OFF
// test("expect actual compiles").config(tags = tsCompile) {
// actual.shouldTypeScriptCompile()
// }
}

context("ExampleCustomisingOutput02") {
val actual = captureOutput("ExampleCustomisingOutput02") {
dev.adamko.kxstsgen.example.exampleCustomisingOutput02.main()
}.normalizeJoin()

test("expect actual matches TypeScript") {
actual.shouldBe(
"""
|export interface ItemHolder {
| item: Item;
|}
|
|export interface Item {
| count?: UInt | null;
|}
|
|export type UInt = uint;
""".trimMargin()
.normalize()
)
}

// TS_COMPILE_OFF
// test("expect actual compiles").config(tags = tsCompile) {
// actual.shouldTypeScriptCompile()
// }
}

context("ExampleCustomisingOutput03") {
val actual = captureOutput("ExampleCustomisingOutput03") {
dev.adamko.kxstsgen.example.exampleCustomisingOutput03.main()
}.normalizeJoin()

test("expect actual matches TypeScript") {
actual.shouldBe(
"""
|export interface ItemHolder {
| item: Item;
| tick: Tick | null;
|}
|
|export interface Item {
| count?: UInt | null;
|}
|
|export type Tick = UInt;
|
|export type UInt = uint;
""".trimMargin()
.normalize()
)
}

// TS_COMPILE_OFF
// test("expect actual compiles").config(tags = tsCompile) {
// actual.shouldTypeScriptCompile()
// }
}
})
Loading

0 comments on commit 611f642

Please sign in to comment.