Skip to content

Commit 692dcda

Browse files
committed
allow for custom literals, for better overriding #23
1 parent 611f642 commit 692dcda

File tree

8 files changed

+143
-25
lines changed

8 files changed

+143
-25
lines changed

docs/code/example/example-customising-output-02.kt

+4-12
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,17 @@ import dev.adamko.kxstsgen.*
88
import kotlinx.serialization.builtins.serializer
99
import dev.adamko.kxstsgen.core.*
1010

11-
@Serializable
12-
data class ItemHolder(
13-
val item: Item,
14-
)
15-
1611
@Serializable
1712
data class Item(
18-
val count: UInt? = 0u,
13+
val price: Double,
14+
val count: Int,
1915
)
2016

2117
fun main() {
2218
val tsGenerator = KxsTsGenerator()
2319

2420
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-
)
21+
Double.serializer().descriptor to TsLiteral.Custom("customDouble")
2922

30-
println(tsGenerator.generate(ItemHolder.serializer()))
23+
println(tsGenerator.generate(Item.serializer()))
3124
}
32-

docs/code/example/example-customising-output-03.kt

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@ import dev.adamko.kxstsgen.*
88
import kotlinx.serialization.builtins.serializer
99
import dev.adamko.kxstsgen.core.*
1010

11-
12-
@Serializable
13-
@JvmInline
14-
value class Tick(val value: UInt)
15-
1611
@Serializable
1712
data class ItemHolder(
1813
val item: Item,
19-
val tick: Tick?,
2014
)
2115

2216
@Serializable
2317
data class Item(
2418
val count: UInt? = 0u,
19+
val score: Int? = 0,
2520
)
2621

2722
fun main() {
@@ -33,7 +28,8 @@ fun main() {
3328
typeRef = TsTypeRef.Declaration(id = TsElementId("uint"), parent = null, nullable = false)
3429
)
3530

31+
tsGenerator.descriptorOverrides += Int.serializer().descriptor to TsLiteral.Custom("customInt")
32+
3633
println(tsGenerator.generate(ItemHolder.serializer()))
3734
}
3835

39-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.exampleCustomisingOutput04
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+
@JvmInline
18+
value class Phase(val value: Int)
19+
20+
@Serializable
21+
data class ItemHolder(
22+
val item: Item,
23+
val tick: Tick?,
24+
val phase: Phase?,
25+
)
26+
27+
@Serializable
28+
data class Item(
29+
val count: UInt? = 0u,
30+
val score: Int? = 0,
31+
)
32+
33+
fun main() {
34+
val tsGenerator = KxsTsGenerator()
35+
36+
tsGenerator.descriptorOverrides +=
37+
UInt.serializer().descriptor to TsDeclaration.TsTypeAlias(
38+
id = TsElementId("kotlin.UInt"),
39+
typeRef = TsTypeRef.Declaration(id = TsElementId("uint"), parent = null, nullable = false)
40+
)
41+
42+
tsGenerator.descriptorOverrides += Int.serializer().descriptor to TsLiteral.Custom("customInt")
43+
44+
println(tsGenerator.generate(ItemHolder.serializer()))
45+
}
46+
47+

docs/code/test/CustomisingOutputTest.kt

+31-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ class CustomisingOutputTest : FunSpec({
4141
dev.adamko.kxstsgen.example.exampleCustomisingOutput02.main()
4242
}.normalizeJoin()
4343

44+
test("expect actual matches TypeScript") {
45+
actual.shouldBe(
46+
"""
47+
|export interface Item {
48+
| price: customDouble;
49+
| count: number;
50+
|}
51+
""".trimMargin()
52+
.normalize()
53+
)
54+
}
55+
56+
// TS_COMPILE_OFF
57+
// test("expect actual compiles").config(tags = tsCompile) {
58+
// actual.shouldTypeScriptCompile()
59+
// }
60+
}
61+
62+
context("ExampleCustomisingOutput03") {
63+
val actual = captureOutput("ExampleCustomisingOutput03") {
64+
dev.adamko.kxstsgen.example.exampleCustomisingOutput03.main()
65+
}.normalizeJoin()
66+
4467
test("expect actual matches TypeScript") {
4568
actual.shouldBe(
4669
"""
@@ -50,6 +73,7 @@ class CustomisingOutputTest : FunSpec({
5073
|
5174
|export interface Item {
5275
| count?: UInt | null;
76+
| score?: customInt | null;
5377
|}
5478
|
5579
|export type UInt = uint;
@@ -64,9 +88,9 @@ class CustomisingOutputTest : FunSpec({
6488
// }
6589
}
6690

67-
context("ExampleCustomisingOutput03") {
68-
val actual = captureOutput("ExampleCustomisingOutput03") {
69-
dev.adamko.kxstsgen.example.exampleCustomisingOutput03.main()
91+
context("ExampleCustomisingOutput04") {
92+
val actual = captureOutput("ExampleCustomisingOutput04") {
93+
dev.adamko.kxstsgen.example.exampleCustomisingOutput04.main()
7094
}.normalizeJoin()
7195

7296
test("expect actual matches TypeScript") {
@@ -75,14 +99,18 @@ class CustomisingOutputTest : FunSpec({
7599
|export interface ItemHolder {
76100
| item: Item;
77101
| tick: Tick | null;
102+
| phase: Phase | null;
78103
|}
79104
|
80105
|export interface Item {
81106
| count?: UInt | null;
107+
| score?: customInt | null;
82108
|}
83109
|
84110
|export type Tick = UInt;
85111
|
112+
|export type Phase = customInt;
113+
|
86114
|export type UInt = uint;
87115
""".trimMargin()
88116
.normalize()

docs/customising-output.md

+53-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,41 @@ export type Double = double; // assume that 'double' will be provided by another
6666

6767
<!--- TEST TS_COMPILE_OFF -->
6868

69+
Instead of changing the output to be a type alias, a custom 'literal' type can be set instead.
70+
71+
```kotlin
72+
import kotlinx.serialization.builtins.serializer
73+
import dev.adamko.kxstsgen.core.*
74+
75+
@Serializable
76+
data class Item(
77+
val price: Double,
78+
val count: Int,
79+
)
80+
81+
fun main() {
82+
val tsGenerator = KxsTsGenerator()
83+
84+
tsGenerator.descriptorOverrides +=
85+
Double.serializer().descriptor to TsLiteral.Custom("customDouble")
86+
87+
println(tsGenerator.generate(Item.serializer()))
88+
}
89+
```
90+
91+
> You can get the full code [here](./code/example/example-customising-output-02.kt).
92+
93+
This produces no type alias, and `Double` is overridden to be `customDouble`.
94+
95+
```typescript
96+
export interface Item {
97+
price: customDouble;
98+
count: number;
99+
}
100+
```
101+
102+
<!--- TEST TS_COMPILE_OFF -->
103+
69104
### Override nullable elements
70105

71106
Even though UInt is nullable, it should be overridden by the UInt defined in `descriptorOverrides`.
@@ -82,6 +117,7 @@ data class ItemHolder(
82117
@Serializable
83118
data class Item(
84119
val count: UInt? = 0u,
120+
val score: Int? = 0,
85121
)
86122

87123
fun main() {
@@ -93,12 +129,14 @@ fun main() {
93129
typeRef = TsTypeRef.Declaration(id = TsElementId("uint"), parent = null, nullable = false)
94130
)
95131

132+
tsGenerator.descriptorOverrides += Int.serializer().descriptor to TsLiteral.Custom("customInt")
133+
96134
println(tsGenerator.generate(ItemHolder.serializer()))
97135
}
98136

99137
```
100138

101-
> You can get the full code [here](./code/example/example-customising-output-02.kt).
139+
> You can get the full code [here](./code/example/example-customising-output-03.kt).
102140
103141
```typescript
104142
export interface ItemHolder {
@@ -107,6 +145,7 @@ export interface ItemHolder {
107145

108146
export interface Item {
109147
count?: UInt | null;
148+
score?: customInt | null;
110149
}
111150

112151
export type UInt = uint;
@@ -129,15 +168,21 @@ import dev.adamko.kxstsgen.core.*
129168
@JvmInline
130169
value class Tick(val value: UInt)
131170

171+
@Serializable
172+
@JvmInline
173+
value class Phase(val value: Int)
174+
132175
@Serializable
133176
data class ItemHolder(
134177
val item: Item,
135178
val tick: Tick?,
179+
val phase: Phase?,
136180
)
137181

138182
@Serializable
139183
data class Item(
140184
val count: UInt? = 0u,
185+
val score: Int? = 0,
141186
)
142187

143188
fun main() {
@@ -149,26 +194,32 @@ fun main() {
149194
typeRef = TsTypeRef.Declaration(id = TsElementId("uint"), parent = null, nullable = false)
150195
)
151196

197+
tsGenerator.descriptorOverrides += Int.serializer().descriptor to TsLiteral.Custom("customInt")
198+
152199
println(tsGenerator.generate(ItemHolder.serializer()))
153200
}
154201

155202

156203
```
157204

158-
> You can get the full code [here](./code/example/example-customising-output-03.kt).
205+
> You can get the full code [here](./code/example/example-customising-output-04.kt).
159206
160207
```typescript
161208
export interface ItemHolder {
162209
item: Item;
163210
tick: Tick | null;
211+
phase: Phase | null;
164212
}
165213

166214
export interface Item {
167215
count?: UInt | null;
216+
score?: customInt | null;
168217
}
169218

170219
export type Tick = UInt;
171220

221+
export type Phase = customInt;
222+
172223
export type UInt = uint;
173224
```
174225

modules/kxs-ts-gen-core/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ kotlin {
2626
jvm {
2727
compilations.all {
2828
kotlinOptions {
29-
jvmTarget = "11"
29+
jvmTarget = "1.8"
3030
}
3131
}
3232
withJava()

modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/KxsTsSourceCodeGenerator.kt

+2
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ abstract class KxsTsSourceCodeGenerator(
200200
}
201201

202202
is TsLiteral.TsMap -> generateMapTypeReference(typeRef.element)
203+
204+
is TsLiteral.Custom -> typeRef.element.value
203205
}
204206

205207
is TsTypeRef.Declaration -> {

modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/tsElements.kt

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ sealed interface TsLiteral : TsElement {
120120
}
121121
}
122122

123+
@JvmInline
124+
value class Custom(val value: String) : TsLiteral
123125
}
124126

125127

0 commit comments

Comments
 (0)