Skip to content

allow for custom literals, for better overriding #23 #31

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 1 commit into from
Apr 13, 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
16 changes: 4 additions & 12 deletions docs/code/example/example-customising-output-02.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@ 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,
val price: Double,
val count: Int,
)

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

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

10 changes: 3 additions & 7 deletions docs/code/example/example-customising-output-03.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ 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,
val score: Int? = 0,
)

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

tsGenerator.descriptorOverrides += Int.serializer().descriptor to TsLiteral.Custom("customInt")

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


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

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
@JvmInline
value class Phase(val value: Int)

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

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

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)
)

tsGenerator.descriptorOverrides += Int.serializer().descriptor to TsLiteral.Custom("customInt")

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


34 changes: 31 additions & 3 deletions docs/code/test/CustomisingOutputTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ class CustomisingOutputTest : FunSpec({
dev.adamko.kxstsgen.example.exampleCustomisingOutput02.main()
}.normalizeJoin()

test("expect actual matches TypeScript") {
actual.shouldBe(
"""
|export interface Item {
| price: customDouble;
| count: number;
|}
""".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(
"""
Expand All @@ -50,6 +73,7 @@ class CustomisingOutputTest : FunSpec({
|
|export interface Item {
| count?: UInt | null;
| score?: customInt | null;
|}
|
|export type UInt = uint;
Expand All @@ -64,9 +88,9 @@ class CustomisingOutputTest : FunSpec({
// }
}

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

test("expect actual matches TypeScript") {
Expand All @@ -75,14 +99,18 @@ class CustomisingOutputTest : FunSpec({
|export interface ItemHolder {
| item: Item;
| tick: Tick | null;
| phase: Phase | null;
|}
|
|export interface Item {
| count?: UInt | null;
| score?: customInt | null;
|}
|
|export type Tick = UInt;
|
|export type Phase = customInt;
|
|export type UInt = uint;
""".trimMargin()
.normalize()
Expand Down
55 changes: 53 additions & 2 deletions docs/customising-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,41 @@ export type Double = double; // assume that 'double' will be provided by another

<!--- TEST TS_COMPILE_OFF -->

Instead of changing the output to be a type alias, a custom 'literal' type can be set instead.

```kotlin
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 TsLiteral.Custom("customDouble")

println(tsGenerator.generate(Item.serializer()))
}
```

> You can get the full code [here](./code/example/example-customising-output-02.kt).

This produces no type alias, and `Double` is overridden to be `customDouble`.

```typescript
export interface Item {
price: customDouble;
count: number;
}
```

<!--- TEST TS_COMPILE_OFF -->

### Override nullable elements

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

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

tsGenerator.descriptorOverrides += Int.serializer().descriptor to TsLiteral.Custom("customInt")

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

```

> You can get the full code [here](./code/example/example-customising-output-02.kt).
> You can get the full code [here](./code/example/example-customising-output-03.kt).

```typescript
export interface ItemHolder {
Expand All @@ -107,6 +145,7 @@ export interface ItemHolder {

export interface Item {
count?: UInt | null;
score?: customInt | null;
}

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

@Serializable
@JvmInline
value class Phase(val value: Int)

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

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

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

tsGenerator.descriptorOverrides += Int.serializer().descriptor to TsLiteral.Custom("customInt")

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


```

> You can get the full code [here](./code/example/example-customising-output-03.kt).
> You can get the full code [here](./code/example/example-customising-output-04.kt).

```typescript
export interface ItemHolder {
item: Item;
tick: Tick | null;
phase: Phase | null;
}

export interface Item {
count?: UInt | null;
score?: customInt | null;
}

export type Tick = UInt;

export type Phase = customInt;

export type UInt = uint;
```

Expand Down
2 changes: 1 addition & 1 deletion modules/kxs-ts-gen-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ kotlin {
jvm {
compilations.all {
kotlinOptions {
jvmTarget = "11"
jvmTarget = "1.8"
}
}
withJava()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ abstract class KxsTsSourceCodeGenerator(
}

is TsLiteral.TsMap -> generateMapTypeReference(typeRef.element)

is TsLiteral.Custom -> typeRef.element.value
}

is TsTypeRef.Declaration -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ sealed interface TsLiteral : TsElement {
}
}

@JvmInline
value class Custom(val value: String) : TsLiteral
}


Expand Down