From 692dcda24a84cdb4fdaa81cacd4f2812db7f7840 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Wed, 13 Apr 2022 23:43:28 +0200 Subject: [PATCH] allow for custom literals, for better overriding #23 --- .../example/example-customising-output-02.kt | 16 ++---- .../example/example-customising-output-03.kt | 10 +--- .../example/example-customising-output-04.kt | 47 ++++++++++++++++ docs/code/test/CustomisingOutputTest.kt | 34 +++++++++++- docs/customising-output.md | 55 ++++++++++++++++++- modules/kxs-ts-gen-core/build.gradle.kts | 2 +- .../kxstsgen/core/KxsTsSourceCodeGenerator.kt | 2 + .../dev/adamko/kxstsgen/core/tsElements.kt | 2 + 8 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 docs/code/example/example-customising-output-04.kt diff --git a/docs/code/example/example-customising-output-02.kt b/docs/code/example/example-customising-output-02.kt index 28b63047..f5359aa0 100644 --- a/docs/code/example/example-customising-output-02.kt +++ b/docs/code/example/example-customising-output-02.kt @@ -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())) } - diff --git a/docs/code/example/example-customising-output-03.kt b/docs/code/example/example-customising-output-03.kt index b87d2d16..c99d80f4 100644 --- a/docs/code/example/example-customising-output-03.kt +++ b/docs/code/example/example-customising-output-03.kt @@ -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() { @@ -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())) } - diff --git a/docs/code/example/example-customising-output-04.kt b/docs/code/example/example-customising-output-04.kt new file mode 100644 index 00000000..e2e1551f --- /dev/null +++ b/docs/code/example/example-customising-output-04.kt @@ -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())) +} + + diff --git a/docs/code/test/CustomisingOutputTest.kt b/docs/code/test/CustomisingOutputTest.kt index bc792157..9e4eca8e 100644 --- a/docs/code/test/CustomisingOutputTest.kt +++ b/docs/code/test/CustomisingOutputTest.kt @@ -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( """ @@ -50,6 +73,7 @@ class CustomisingOutputTest : FunSpec({ | |export interface Item { | count?: UInt | null; + | score?: customInt | null; |} | |export type UInt = uint; @@ -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") { @@ -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() diff --git a/docs/customising-output.md b/docs/customising-output.md index 63771015..83f40657 100644 --- a/docs/customising-output.md +++ b/docs/customising-output.md @@ -66,6 +66,41 @@ export type Double = double; // assume that 'double' will be provided by another +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; +} +``` + + + ### Override nullable elements Even though UInt is nullable, it should be overridden by the UInt defined in `descriptorOverrides`. @@ -82,6 +117,7 @@ data class ItemHolder( @Serializable data class Item( val count: UInt? = 0u, + val score: Int? = 0, ) fun main() { @@ -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 { @@ -107,6 +145,7 @@ export interface ItemHolder { export interface Item { count?: UInt | null; + score?: customInt | null; } export type UInt = uint; @@ -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() { @@ -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; ``` diff --git a/modules/kxs-ts-gen-core/build.gradle.kts b/modules/kxs-ts-gen-core/build.gradle.kts index 7a2c18f9..c1fd24e0 100644 --- a/modules/kxs-ts-gen-core/build.gradle.kts +++ b/modules/kxs-ts-gen-core/build.gradle.kts @@ -26,7 +26,7 @@ kotlin { jvm { compilations.all { kotlinOptions { - jvmTarget = "11" + jvmTarget = "1.8" } } withJava() diff --git a/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/KxsTsSourceCodeGenerator.kt b/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/KxsTsSourceCodeGenerator.kt index 041bcd82..cf021f4f 100644 --- a/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/KxsTsSourceCodeGenerator.kt +++ b/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/KxsTsSourceCodeGenerator.kt @@ -200,6 +200,8 @@ abstract class KxsTsSourceCodeGenerator( } is TsLiteral.TsMap -> generateMapTypeReference(typeRef.element) + + is TsLiteral.Custom -> typeRef.element.value } is TsTypeRef.Declaration -> { diff --git a/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/tsElements.kt b/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/tsElements.kt index 45011488..5f68bed5 100644 --- a/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/tsElements.kt +++ b/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/tsElements.kt @@ -120,6 +120,8 @@ sealed interface TsLiteral : TsElement { } } + @JvmInline + value class Custom(val value: String) : TsLiteral }