Skip to content

Commit cad7b30

Browse files
committed
#1 add more tests/docs for lists and maps
1 parent bf7ae56 commit cad7b30

11 files changed

+321
-8
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file was automatically generated from lists.md by Knit tool. Do not edit.
2+
@file:Suppress("PackageDirectoryMismatch", "unused")
3+
package dev.adamko.kxstsgen.example.exampleListObjects01
4+
5+
import kotlinx.serialization.*
6+
import dev.adamko.kxstsgen.*
7+
8+
@Serializable
9+
data class Colour(
10+
val rgb: String
11+
)
12+
13+
@Serializable
14+
data class MyLists(
15+
val colours: List<Colour>,
16+
val colourGroups: Set<List<Colour>>,
17+
val colourGroupGroups: LinkedHashSet<List<List<Colour>>>,
18+
)
19+
20+
fun main() {
21+
val tsGenerator = KxsTsGenerator()
22+
println(tsGenerator.generate(MyLists.serializer()))
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This file was automatically generated from lists.md by Knit tool. Do not edit.
2+
@file:Suppress("PackageDirectoryMismatch", "unused")
3+
package dev.adamko.kxstsgen.example.exampleListObjects02
4+
5+
import kotlinx.serialization.*
6+
import dev.adamko.kxstsgen.*
7+
8+
@Serializable
9+
data class Colour(
10+
val rgb: String
11+
)
12+
13+
@Serializable
14+
data class MyLists(
15+
val listOfMaps: List<Map<String, Int>>,
16+
val listOfColourMaps: List<Map<String, Colour>>,
17+
)
18+
19+
fun main() {
20+
val tsGenerator = KxsTsGenerator()
21+
println(tsGenerator.generate(MyLists.serializer()))
22+
}

docs/code/example/example-list-primitive-01.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import dev.adamko.kxstsgen.*
88
@Serializable
99
data class MyLists(
1010
val strings: List<String>,
11-
val ints: List<Int>,
12-
val longs: List<Long>,
11+
val ints: Set<Int>,
12+
val longs: Collection<Long>,
1313
)
1414

1515
fun main() {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file was automatically generated from lists.md by Knit tool. Do not edit.
2+
@file:Suppress("PackageDirectoryMismatch", "unused")
3+
package dev.adamko.kxstsgen.example.exampleListPrimitive02
4+
5+
import kotlinx.serialization.*
6+
import dev.adamko.kxstsgen.*
7+
8+
@Serializable
9+
data class Colour(
10+
val rgb: String
11+
)
12+
13+
@Serializable
14+
data class MyLists(
15+
val colours: List<Colour>,
16+
val colourGroups: List<List<Colour>>,
17+
val colourGroupGroups: List<List<List<Colour>>>,
18+
)
19+
20+
fun main() {
21+
val tsGenerator = KxsTsGenerator()
22+
println(tsGenerator.generate(MyLists.serializer()))
23+
}

docs/code/example/example-map-primitive-03.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import kotlinx.serialization.*
66
import dev.adamko.kxstsgen.*
77

88
@Serializable
9-
data class Config(
10-
val properties: Map<String?, String?>
9+
class MapsWithLists(
10+
val mapOfLists: Map<String, List<String>>
1111
)
1212

1313
fun main() {
1414
val tsGenerator = KxsTsGenerator()
15-
println(tsGenerator.generate(Config.serializer()))
15+
println(tsGenerator.generate(MapsWithLists.serializer()))
1616
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This file was automatically generated from maps.md by Knit tool. Do not edit.
2+
@file:Suppress("PackageDirectoryMismatch", "unused")
3+
package dev.adamko.kxstsgen.example.exampleMapPrimitive04
4+
5+
import kotlinx.serialization.*
6+
import dev.adamko.kxstsgen.*
7+
8+
@Serializable
9+
@JvmInline
10+
value class Data(val content: String)
11+
12+
@Serializable
13+
class MyDataClass(
14+
val mapOfLists: Map<String, Data>
15+
)
16+
17+
fun main() {
18+
val tsGenerator = KxsTsGenerator()
19+
println(tsGenerator.generate(MyDataClass.serializer()))
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This file was automatically generated from maps.md by Knit tool. Do not edit.
2+
@file:Suppress("PackageDirectoryMismatch", "unused")
3+
package dev.adamko.kxstsgen.example.exampleMapPrimitive05
4+
5+
import kotlinx.serialization.*
6+
import dev.adamko.kxstsgen.*
7+
8+
@Serializable
9+
data class Config(
10+
val properties: Map<String?, String?>
11+
)
12+
13+
fun main() {
14+
val tsGenerator = KxsTsGenerator()
15+
println(tsGenerator.generate(Config.serializer()))
16+
}

docs/code/test/ListsTests.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,47 @@ class ListsTests {
2525
.normalize()
2626
)
2727
}
28+
29+
@Test
30+
fun testExampleListObjects01() {
31+
captureOutput("ExampleListObjects01") {
32+
dev.adamko.kxstsgen.example.exampleListObjects01.main()
33+
}.normalizeJoin()
34+
.shouldBe(
35+
// language=TypeScript
36+
"""
37+
|export interface MyLists {
38+
| colours: Colour[];
39+
| colourGroups: Colour[][];
40+
| colourGroupGroups: Colour[][][];
41+
|}
42+
|
43+
|export interface Colour {
44+
| rgb: string;
45+
|}
46+
""".trimMargin()
47+
.normalize()
48+
)
49+
}
50+
51+
@Test
52+
fun testExampleListObjects02() {
53+
captureOutput("ExampleListObjects02") {
54+
dev.adamko.kxstsgen.example.exampleListObjects02.main()
55+
}.normalizeJoin()
56+
.shouldBe(
57+
// language=TypeScript
58+
"""
59+
|export interface MyLists {
60+
| listOfMaps: { [key: string]: number }[];
61+
| listOfColourMaps: { [key: string]: Colour }[];
62+
|}
63+
|
64+
|export interface Colour {
65+
| rgb: string;
66+
|}
67+
""".trimMargin()
68+
.normalize()
69+
)
70+
}
2871
}

docs/code/test/MapsTests.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,40 @@ class MapsTests {
4949
fun testExampleMapPrimitive03() {
5050
captureOutput("ExampleMapPrimitive03") {
5151
dev.adamko.kxstsgen.example.exampleMapPrimitive03.main()
52+
}.normalizeJoin()
53+
.shouldBe(
54+
// language=TypeScript
55+
"""
56+
|export interface MapsWithLists {
57+
| mapOfLists: { [key: string]: string[] };
58+
|}
59+
""".trimMargin()
60+
.normalize()
61+
)
62+
}
63+
64+
@Test
65+
fun testExampleMapPrimitive04() {
66+
captureOutput("ExampleMapPrimitive04") {
67+
dev.adamko.kxstsgen.example.exampleMapPrimitive04.main()
68+
}.normalizeJoin()
69+
.shouldBe(
70+
// language=TypeScript
71+
"""
72+
|export interface MyDataClass {
73+
| mapOfLists: { [key: string]: Data };
74+
|}
75+
|
76+
|export type Data = string;
77+
""".trimMargin()
78+
.normalize()
79+
)
80+
}
81+
82+
@Test
83+
fun testExampleMapPrimitive05() {
84+
captureOutput("ExampleMapPrimitive05") {
85+
dev.adamko.kxstsgen.example.exampleMapPrimitive05.main()
5286
}.normalizeJoin()
5387
.shouldBe(
5488
// language=TypeScript

docs/lists.md

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
* [Introduction](#introduction)
88
* [Primitive lists](#primitive-lists)
9+
* [Lists of objects](#lists-of-objects)
10+
* [Lists of collections](#lists-of-collections)
911

1012
<!--- END -->
1113

@@ -19,12 +21,14 @@ import dev.adamko.kxstsgen.*
1921

2022
### Primitive lists
2123

24+
A collection will get converted to array.
25+
2226
```kotlin
2327
@Serializable
2428
data class MyLists(
2529
val strings: List<String>,
26-
val ints: List<Int>,
27-
val longs: List<Long>,
30+
val ints: Set<Int>,
31+
val longs: Collection<Long>,
2832
)
2933

3034
fun main() {
@@ -44,3 +48,75 @@ export interface MyLists {
4448
```
4549

4650
<!--- TEST -->
51+
52+
### Lists of objects
53+
54+
```kotlin
55+
@Serializable
56+
data class Colour(
57+
val rgb: String
58+
)
59+
60+
@Serializable
61+
data class MyLists(
62+
val colours: List<Colour>,
63+
val colourGroups: Set<List<Colour>>,
64+
val colourGroupGroups: LinkedHashSet<List<List<Colour>>>,
65+
)
66+
67+
fun main() {
68+
val tsGenerator = KxsTsGenerator()
69+
println(tsGenerator.generate(MyLists.serializer()))
70+
}
71+
```
72+
73+
> You can get the full code [here](./code/example/example-list-objects-01.kt).
74+
75+
```typescript
76+
export interface MyLists {
77+
colours: Colour[];
78+
colourGroups: Colour[][];
79+
colourGroupGroups: Colour[][][];
80+
}
81+
82+
export interface Colour {
83+
rgb: string;
84+
}
85+
```
86+
87+
<!--- TEST -->
88+
89+
### Lists of collections
90+
91+
```kotlin
92+
@Serializable
93+
data class Colour(
94+
val rgb: String
95+
)
96+
97+
@Serializable
98+
data class MyLists(
99+
val listOfMaps: List<Map<String, Int>>,
100+
val listOfColourMaps: List<Map<String, Colour>>,
101+
)
102+
103+
fun main() {
104+
val tsGenerator = KxsTsGenerator()
105+
println(tsGenerator.generate(MyLists.serializer()))
106+
}
107+
```
108+
109+
> You can get the full code [here](./code/example/example-list-objects-02.kt).
110+
111+
```typescript
112+
export interface MyLists {
113+
listOfMaps: { [key: string]: number }[];
114+
listOfColourMaps: { [key: string]: Colour }[];
115+
}
116+
117+
export interface Colour {
118+
rgb: string;
119+
}
120+
```
121+
122+
<!--- TEST -->

0 commit comments

Comments
 (0)