Skip to content

add demo for making properties required with @Required #50

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 2 commits into from
Nov 12, 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
23 changes: 23 additions & 0 deletions docs/code/example/example-default-values-primitive-fields-02.kt
Original file line number Diff line number Diff line change
@@ -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()))
}
27 changes: 27 additions & 0 deletions docs/code/test/DefaultValuesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
})
4 changes: 2 additions & 2 deletions docs/customising-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<!--- END -->
Expand Down Expand Up @@ -101,7 +101,7 @@ export interface Item {

<!--- TEST TS_COMPILE_OFF -->

### Override nullable elements
### Override nullable properties

Even though UInt is nullable, it should be overridden by the UInt defined in `descriptorOverrides`.

Expand Down
44 changes: 44 additions & 0 deletions docs/default-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [Default values](#default-values)
* [Nullable values](#nullable-values)
* [Default and nullable](#default-and-nullable)
* [Override optional properties](#override-optional-properties)

<!--- END -->

Expand Down Expand Up @@ -106,3 +107,46 @@ export interface ContactDetails {
```

<!--- TEST -->

### 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;
}
```

<!--- TEST -->