Skip to content

Refine implementation #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/code/example/example-enum-class-01.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
@file:Suppress("PackageDirectoryMismatch", "unused")
package dev.adamko.kxstsgen.example.exampleEnumClass01

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

Expand Down
2 changes: 0 additions & 2 deletions docs/code/example/example-enum-class-02.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
@file:Suppress("PackageDirectoryMismatch", "unused")
package dev.adamko.kxstsgen.example.exampleEnumClass02

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

Expand Down
46 changes: 46 additions & 0 deletions docs/code/example/example-tuple-01.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file was automatically generated from tuples.md by Knit tool. Do not edit.
@file:Suppress("PackageDirectoryMismatch", "unused")
package dev.adamko.kxstsgen.example.exampleTuple01

import dev.adamko.kxstsgen.*
import dev.adamko.kxstsgen.core.experiments.TupleSerializer
import kotlinx.serialization.*

@Serializable(with = SimpleTypes.SimpleTypesSerializer::class)
data class SimpleTypes(
val aString: String,
var anInt: Int,
val aDouble: Double?,
val bool: Boolean,
private val privateMember: String,
) {
// Create `SimpleTypesSerializer` inside `SimpleTypes`, so it
// has access to the private property `privateMember`.
object SimpleTypesSerializer : TupleSerializer<SimpleTypes>(
"SimpleTypes",
{
// Provide all tuple elements, in order, using the 'elements' helper method.
element(SimpleTypes::aString)
element(SimpleTypes::anInt)
element(SimpleTypes::aDouble)
element(SimpleTypes::bool)
element(SimpleTypes::privateMember)
}
) {
override fun tupleConstructor(elements: List<*>): SimpleTypes {
// When deserializing, the elements will be available as a list, in the order defined
return SimpleTypes(
elements[0] as String,
elements[1] as Int,
elements[2] as Double,
elements[3] as Boolean,
elements[4] as String,
)
}
}
}

fun main() {
val tsGenerator = KxsTsGenerator()
println(tsGenerator.generate(SimpleTypes.serializer()))
}
40 changes: 40 additions & 0 deletions docs/code/example/example-tuple-02.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This file was automatically generated from tuples.md by Knit tool. Do not edit.
@file:Suppress("PackageDirectoryMismatch", "unused")
package dev.adamko.kxstsgen.example.exampleTuple02

import dev.adamko.kxstsgen.*
import dev.adamko.kxstsgen.core.experiments.TupleSerializer
import kotlinx.serialization.*

@Serializable(with = OptionalFields.Serializer::class)
data class OptionalFields(
val requiredString: String,
val optionalString: String = "",
val nullableString: String?,
val nullableOptionalString: String? = "",
) {
object Serializer : TupleSerializer<OptionalFields>(
"OptionalFields",
{
element(OptionalFields::requiredString)
element(OptionalFields::optionalString)
element(OptionalFields::nullableString)
element(OptionalFields::nullableOptionalString)
}
) {
override fun tupleConstructor(elements: List<*>): OptionalFields {
val iter = elements.iterator()
return OptionalFields(
iter.next() as String,
iter.next() as String,
iter.next() as String,
iter.next() as String,
)
}
}
}

fun main() {
val tsGenerator = KxsTsGenerator()
println(tsGenerator.generate(OptionalFields.serializer()))
}
36 changes: 36 additions & 0 deletions docs/code/example/example-tuple-03.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file was automatically generated from tuples.md by Knit tool. Do not edit.
@file:Suppress("PackageDirectoryMismatch", "unused")
package dev.adamko.kxstsgen.example.exampleTuple03

import dev.adamko.kxstsgen.*
import dev.adamko.kxstsgen.core.experiments.TupleSerializer
import kotlinx.serialization.*

@Serializable(with = Coordinates.Serializer::class)
data class Coordinates(
val x: Int,
val y: Int,
val z: Int,
) {
object Serializer : TupleSerializer<Coordinates>(
"Coordinates",
{
element(Coordinates::x)
element(Coordinates::y)
element(Coordinates::z)
}
) {
override fun tupleConstructor(elements: List<*>): Coordinates {
return Coordinates(
elements[0] as Int,
elements[1] as Int,
elements[2] as Int,
)
}
}
}

fun main() {
val tsGenerator = KxsTsGenerator()
println(tsGenerator.generate(Coordinates.serializer()))
}
21 changes: 21 additions & 0 deletions docs/code/example/example-tuple-04.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file was automatically generated from tuples.md by Knit tool. Do not edit.
@file:Suppress("PackageDirectoryMismatch", "unused")
package dev.adamko.kxstsgen.example.exampleTuple04

import dev.adamko.kxstsgen.*
import dev.adamko.kxstsgen.core.experiments.TupleSerializer
import kotlinx.serialization.*

import dev.adamko.kxstsgen.example.exampleTuple03.Coordinates

@Serializable
class GameLocations(
val homeLocation: Coordinates,
val allLocations: List<Coordinates>,
val namedLocations: Map<String, Coordinates>,
)

fun main() {
val tsGenerator = KxsTsGenerator()
println(tsGenerator.generate(GameLocations.serializer()))
}
72 changes: 72 additions & 0 deletions docs/code/test/TuplesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file was automatically generated from tuples.md by Knit tool. Do not edit.
@file:Suppress("JSUnusedLocalSymbols")
package dev.adamko.kxstsgen.example.test

import io.kotest.matchers.*
import kotlinx.knit.test.*
import org.junit.jupiter.api.Test
import dev.adamko.kxstsgen.util.*

class TuplesTest {
@Test
fun testExampleTuple01() {
captureOutput("ExampleTuple01") {
dev.adamko.kxstsgen.example.exampleTuple01.main()
}.normalizeJoin()
.shouldBe(
// language=TypeScript
"""
|export type SimpleTypes = [string, number, number | null, boolean, string];
""".trimMargin()
.normalize()
)
}

@Test
fun testExampleTuple02() {
captureOutput("ExampleTuple02") {
dev.adamko.kxstsgen.example.exampleTuple02.main()
}.normalizeJoin()
.shouldBe(
// language=TypeScript
"""
|export type OptionalFields = [string, string, string | null, string | null];
""".trimMargin()
.normalize()
)
}

@Test
fun testExampleTuple03() {
captureOutput("ExampleTuple03") {
dev.adamko.kxstsgen.example.exampleTuple03.main()
}.normalizeJoin()
.shouldBe(
// language=TypeScript
"""
|export type Coordinates = [number, number, number];
""".trimMargin()
.normalize()
)
}

@Test
fun testExampleTuple04() {
captureOutput("ExampleTuple04") {
dev.adamko.kxstsgen.example.exampleTuple04.main()
}.normalizeJoin()
.shouldBe(
// language=TypeScript
"""
|export interface GameLocations {
| homeLocation: Coordinates;
| allLocations: Coordinates[];
| namedLocations: { [key: string]: Coordinates };
|}
|
|export type Coordinates = [number, number, number];
""".trimMargin()
.normalize()
)
}
}
5 changes: 0 additions & 5 deletions docs/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import dev.adamko.kxstsgen.*

### Simple enum

<!--- INCLUDE .*\.kt
import kotlinx.serialization.*
import dev.adamko.kxstsgen.*
-->

```kotlin
@Serializable
enum class SomeType {
Expand Down
Loading