From 9ee8272ccdc86ff4b67ccec4b1402d92be7b5292 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 12 Nov 2022 13:23:05 +0100 Subject: [PATCH 1/2] add demo for @Required properties --- ...mple-default-values-primitive-fields-02.kt | 23 ++++++++++ docs/code/test/DefaultValuesTest.kt | 27 ++++++++++++ docs/customising-output.md | 4 +- docs/default-values.md | 44 +++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 docs/code/example/example-default-values-primitive-fields-02.kt diff --git a/docs/code/example/example-default-values-primitive-fields-02.kt b/docs/code/example/example-default-values-primitive-fields-02.kt new file mode 100644 index 00000000..31e63376 --- /dev/null +++ b/docs/code/example/example-default-values-primitive-fields-02.kt @@ -0,0 +1,23 @@ +// This file was automatically generated from default-values.md by Knit tool. Do not edit. +@file:Suppress("PackageDirectoryMismatch", "unused") +package dev.adamko.kxstsgen.example.exampleDefaultValuesPrimitiveFields02 + +import kotlinx.serialization.* +import dev.adamko.kxstsgen.* + +@Serializable +data class ContactDetails( + @Required + val name: String, + @Required + val email: String?, + @Required + val active: Boolean = true, + @Required + val phoneNumber: String? = null, +) + +fun main() { + val tsGenerator = KxsTsGenerator() + println(tsGenerator.generate(ContactDetails.serializer())) +} diff --git a/docs/code/test/DefaultValuesTest.kt b/docs/code/test/DefaultValuesTest.kt index d4c30508..6e28c6a6 100644 --- a/docs/code/test/DefaultValuesTest.kt +++ b/docs/code/test/DefaultValuesTest.kt @@ -84,4 +84,31 @@ class DefaultValuesTest : FunSpec({ actual.shouldTypeScriptCompile(caseName) } } + + context("ExampleDefaultValuesPrimitiveFields02") { + val caseName = testCase.name.testName + + val actual = captureOutput(caseName) { + dev.adamko.kxstsgen.example.exampleDefaultValuesPrimitiveFields02.main() + }.normalizeJoin() + + test("expect actual matches TypeScript") { + actual.shouldBe( + // language=TypeScript + """ + |export interface ContactDetails { + | name: string; + | email: string | null; + | active: boolean; + | phoneNumber: string | null; + |} + """.trimMargin() + .normalize() + ) + } + + test("expect actual compiles").config(tags = tsCompile) { + actual.shouldTypeScriptCompile(caseName) + } + } }) diff --git a/docs/customising-output.md b/docs/customising-output.md index 5c126945..264b7350 100644 --- a/docs/customising-output.md +++ b/docs/customising-output.md @@ -7,7 +7,7 @@ * [Introduction](#introduction) * [Overriding output](#overriding-output) - * [Override nullable elements](#override-nullable-elements) + * [Override nullable properties](#override-nullable-properties) * [Override both nullable and non-nullable descriptors](#override-both-nullable-and-non-nullable-descriptors) @@ -101,7 +101,7 @@ export interface Item { -### Override nullable elements +### Override nullable properties Even though UInt is nullable, it should be overridden by the UInt defined in `descriptorOverrides`. diff --git a/docs/default-values.md b/docs/default-values.md index 8cc78c84..f1f19327 100644 --- a/docs/default-values.md +++ b/docs/default-values.md @@ -8,6 +8,7 @@ * [Default values](#default-values) * [Nullable values](#nullable-values) * [Default and nullable](#default-and-nullable) + * [Override optional properties](#override-optional-properties) @@ -106,3 +107,46 @@ export interface ContactDetails { ``` + +### Override optional properties + +Properties with default values can be set as required using the Kotlinx Serialization annotation, +[`@kotlinx.serialization.Required`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-required/) +. + +For demonstration purposes, let's see what happens when `@Required` is added to all properties. + +```kotlin +@Serializable +data class ContactDetails( + @Required + val name: String, + @Required + val email: String?, + @Required + val active: Boolean = true, + @Required + val phoneNumber: String? = null, +) + +fun main() { + val tsGenerator = KxsTsGenerator() + println(tsGenerator.generate(ContactDetails.serializer())) +} +``` + +> You can get the full code [here](./code/example/example-default-values-primitive-fields-02.kt). + +`active` and `phoneNumber` are now required properties. Note that `@Required` had no effect +on `name` or `email`. Because they do not have default values, they were already required. + +```typescript +export interface ContactDetails { + name: string; + email: string | null; + active: boolean; + phoneNumber: string | null; +} +``` + + From 8d6d4fcd05bff41183cf8bb9ac94055f3a41e237 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 12 Nov 2022 13:24:12 +0100 Subject: [PATCH 2/2] grammar tweak --- docs/default-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/default-values.md b/docs/default-values.md index f1f19327..ea1cd289 100644 --- a/docs/default-values.md +++ b/docs/default-values.md @@ -138,7 +138,7 @@ fun main() { > You can get the full code [here](./code/example/example-default-values-primitive-fields-02.kt). `active` and `phoneNumber` are now required properties. Note that `@Required` had no effect -on `name` or `email`. Because they do not have default values, they were already required. +on `name` or `email`; because they do not have default values, they were already required. ```typescript export interface ContactDetails {