Skip to content

Commit 611f642

Browse files
authored
Element overriding (#30)
* 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
1 parent b2f134b commit 611f642

10 files changed

+496
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This file was automatically generated from customising-output.md by Knit tool. Do not edit.
2+
@file:Suppress("PackageDirectoryMismatch", "unused")
3+
package dev.adamko.kxstsgen.example.exampleCustomisingOutput01
4+
5+
import kotlinx.serialization.*
6+
import dev.adamko.kxstsgen.*
7+
8+
import kotlinx.serialization.builtins.serializer
9+
import dev.adamko.kxstsgen.core.*
10+
11+
@Serializable
12+
data class Item(
13+
val price: Double,
14+
val count: Int,
15+
)
16+
17+
fun main() {
18+
val tsGenerator = KxsTsGenerator()
19+
20+
tsGenerator.descriptorOverrides +=
21+
Double.serializer().descriptor to TsDeclaration.TsTypeAlias(
22+
id = TsElementId("Double"),
23+
typeRef = TsTypeRef.Declaration(
24+
id = TsElementId("double"),
25+
parent = null,
26+
nullable = false,
27+
)
28+
)
29+
30+
println(tsGenerator.generate(Item.serializer()))
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This file was automatically generated from customising-output.md by Knit tool. Do not edit.
2+
@file:Suppress("PackageDirectoryMismatch", "unused")
3+
package dev.adamko.kxstsgen.example.exampleCustomisingOutput02
4+
5+
import kotlinx.serialization.*
6+
import dev.adamko.kxstsgen.*
7+
8+
import kotlinx.serialization.builtins.serializer
9+
import dev.adamko.kxstsgen.core.*
10+
11+
@Serializable
12+
data class ItemHolder(
13+
val item: Item,
14+
)
15+
16+
@Serializable
17+
data class Item(
18+
val count: UInt? = 0u,
19+
)
20+
21+
fun main() {
22+
val tsGenerator = KxsTsGenerator()
23+
24+
tsGenerator.descriptorOverrides +=
25+
UInt.serializer().descriptor to TsDeclaration.TsTypeAlias(
26+
id = TsElementId("kotlin.UInt"),
27+
typeRef = TsTypeRef.Declaration(id = TsElementId("uint"), parent = null, nullable = false)
28+
)
29+
30+
println(tsGenerator.generate(ItemHolder.serializer()))
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file was automatically generated from customising-output.md by Knit tool. Do not edit.
2+
@file:Suppress("PackageDirectoryMismatch", "unused")
3+
package dev.adamko.kxstsgen.example.exampleCustomisingOutput03
4+
5+
import kotlinx.serialization.*
6+
import dev.adamko.kxstsgen.*
7+
8+
import kotlinx.serialization.builtins.serializer
9+
import dev.adamko.kxstsgen.core.*
10+
11+
12+
@Serializable
13+
@JvmInline
14+
value class Tick(val value: UInt)
15+
16+
@Serializable
17+
data class ItemHolder(
18+
val item: Item,
19+
val tick: Tick?,
20+
)
21+
22+
@Serializable
23+
data class Item(
24+
val count: UInt? = 0u,
25+
)
26+
27+
fun main() {
28+
val tsGenerator = KxsTsGenerator()
29+
30+
tsGenerator.descriptorOverrides +=
31+
UInt.serializer().descriptor to TsDeclaration.TsTypeAlias(
32+
id = TsElementId("kotlin.UInt"),
33+
typeRef = TsTypeRef.Declaration(id = TsElementId("uint"), parent = null, nullable = false)
34+
)
35+
36+
println(tsGenerator.generate(ItemHolder.serializer()))
37+
}
38+
39+

docs/code/knit-test.ftl

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<#--@formatter:off-->
12
<#-- @ftlvariable name="test.name" type="java.lang.String" -->
23
<#-- @ftlvariable name="test.package" type="java.lang.String" -->
34
// This file was automatically generated from ${file.name} by Knit tool. Do not edit.
@@ -24,7 +25,9 @@ class ${test.name} : FunSpec({
2425

2526
test("expect actual matches TypeScript") {
2627
actual.shouldBe(
28+
<#if case.param != "TS_COMPILE_OFF">
2729
// language=TypeScript
30+
</#if>
2831
"""
2932
<#list case.lines as line>
3033
|${line}
@@ -34,9 +37,16 @@ class ${test.name} : FunSpec({
3437
)
3538
}
3639

40+
<#if case.param == "TS_COMPILE_OFF">
41+
// TS_COMPILE_OFF
42+
// test("expect actual compiles").config(tags = tsCompile) {
43+
// actual.shouldTypeScriptCompile()
44+
// }
45+
<#else>
3746
test("expect actual compiles").config(tags = tsCompile) {
3847
actual.shouldTypeScriptCompile()
3948
}
49+
</#if>
4050
}
4151
<#sep>
4252

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// This file was automatically generated from customising-output.md by Knit tool. Do not edit.
2+
@file:Suppress("JSUnusedLocalSymbols")
3+
package dev.adamko.kxstsgen.example.test
4+
5+
import dev.adamko.kxstsgen.util.*
6+
import io.kotest.core.spec.style.*
7+
import io.kotest.matchers.*
8+
import kotlinx.knit.test.*
9+
10+
class CustomisingOutputTest : FunSpec({
11+
12+
tags(Knit)
13+
14+
context("ExampleCustomisingOutput01") {
15+
val actual = captureOutput("ExampleCustomisingOutput01") {
16+
dev.adamko.kxstsgen.example.exampleCustomisingOutput01.main()
17+
}.normalizeJoin()
18+
19+
test("expect actual matches TypeScript") {
20+
actual.shouldBe(
21+
"""
22+
|export interface Item {
23+
| price: Double;
24+
| count: number;
25+
|}
26+
|
27+
|export type Double = double; // assume that 'double' will be provided by another library
28+
""".trimMargin()
29+
.normalize()
30+
)
31+
}
32+
33+
// TS_COMPILE_OFF
34+
// test("expect actual compiles").config(tags = tsCompile) {
35+
// actual.shouldTypeScriptCompile()
36+
// }
37+
}
38+
39+
context("ExampleCustomisingOutput02") {
40+
val actual = captureOutput("ExampleCustomisingOutput02") {
41+
dev.adamko.kxstsgen.example.exampleCustomisingOutput02.main()
42+
}.normalizeJoin()
43+
44+
test("expect actual matches TypeScript") {
45+
actual.shouldBe(
46+
"""
47+
|export interface ItemHolder {
48+
| item: Item;
49+
|}
50+
|
51+
|export interface Item {
52+
| count?: UInt | null;
53+
|}
54+
|
55+
|export type UInt = uint;
56+
""".trimMargin()
57+
.normalize()
58+
)
59+
}
60+
61+
// TS_COMPILE_OFF
62+
// test("expect actual compiles").config(tags = tsCompile) {
63+
// actual.shouldTypeScriptCompile()
64+
// }
65+
}
66+
67+
context("ExampleCustomisingOutput03") {
68+
val actual = captureOutput("ExampleCustomisingOutput03") {
69+
dev.adamko.kxstsgen.example.exampleCustomisingOutput03.main()
70+
}.normalizeJoin()
71+
72+
test("expect actual matches TypeScript") {
73+
actual.shouldBe(
74+
"""
75+
|export interface ItemHolder {
76+
| item: Item;
77+
| tick: Tick | null;
78+
|}
79+
|
80+
|export interface Item {
81+
| count?: UInt | null;
82+
|}
83+
|
84+
|export type Tick = UInt;
85+
|
86+
|export type UInt = uint;
87+
""".trimMargin()
88+
.normalize()
89+
)
90+
}
91+
92+
// TS_COMPILE_OFF
93+
// test("expect actual compiles").config(tags = tsCompile) {
94+
// actual.shouldTypeScriptCompile()
95+
// }
96+
}
97+
})

0 commit comments

Comments
 (0)