Skip to content

Commit 3497060

Browse files
authored
add demo for making properties required with @Required (#50)
1 parent 5d81bee commit 3497060

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file was automatically generated from default-values.md by Knit tool. Do not edit.
2+
@file:Suppress("PackageDirectoryMismatch", "unused")
3+
package dev.adamko.kxstsgen.example.exampleDefaultValuesPrimitiveFields02
4+
5+
import kotlinx.serialization.*
6+
import dev.adamko.kxstsgen.*
7+
8+
@Serializable
9+
data class ContactDetails(
10+
@Required
11+
val name: String,
12+
@Required
13+
val email: String?,
14+
@Required
15+
val active: Boolean = true,
16+
@Required
17+
val phoneNumber: String? = null,
18+
)
19+
20+
fun main() {
21+
val tsGenerator = KxsTsGenerator()
22+
println(tsGenerator.generate(ContactDetails.serializer()))
23+
}

docs/code/test/DefaultValuesTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,31 @@ class DefaultValuesTest : FunSpec({
8484
actual.shouldTypeScriptCompile(caseName)
8585
}
8686
}
87+
88+
context("ExampleDefaultValuesPrimitiveFields02") {
89+
val caseName = testCase.name.testName
90+
91+
val actual = captureOutput(caseName) {
92+
dev.adamko.kxstsgen.example.exampleDefaultValuesPrimitiveFields02.main()
93+
}.normalizeJoin()
94+
95+
test("expect actual matches TypeScript") {
96+
actual.shouldBe(
97+
// language=TypeScript
98+
"""
99+
|export interface ContactDetails {
100+
| name: string;
101+
| email: string | null;
102+
| active: boolean;
103+
| phoneNumber: string | null;
104+
|}
105+
""".trimMargin()
106+
.normalize()
107+
)
108+
}
109+
110+
test("expect actual compiles").config(tags = tsCompile) {
111+
actual.shouldTypeScriptCompile(caseName)
112+
}
113+
}
87114
})

docs/customising-output.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
* [Introduction](#introduction)
99
* [Overriding output](#overriding-output)
10-
* [Override nullable elements](#override-nullable-elements)
10+
* [Override nullable properties](#override-nullable-properties)
1111
* [Override both nullable and non-nullable descriptors](#override-both-nullable-and-non-nullable-descriptors)
1212

1313
<!--- END -->
@@ -101,7 +101,7 @@ export interface Item {
101101

102102
<!--- TEST TS_COMPILE_OFF -->
103103

104-
### Override nullable elements
104+
### Override nullable properties
105105

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

docs/default-values.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [Default values](#default-values)
99
* [Nullable values](#nullable-values)
1010
* [Default and nullable](#default-and-nullable)
11+
* [Override optional properties](#override-optional-properties)
1112

1213
<!--- END -->
1314

@@ -106,3 +107,46 @@ export interface ContactDetails {
106107
```
107108

108109
<!--- TEST -->
110+
111+
### Override optional properties
112+
113+
Properties with default values can be set as required using the Kotlinx Serialization annotation,
114+
[`@kotlinx.serialization.Required`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-required/)
115+
.
116+
117+
For demonstration purposes, let's see what happens when `@Required` is added to all properties.
118+
119+
```kotlin
120+
@Serializable
121+
data class ContactDetails(
122+
@Required
123+
val name: String,
124+
@Required
125+
val email: String?,
126+
@Required
127+
val active: Boolean = true,
128+
@Required
129+
val phoneNumber: String? = null,
130+
)
131+
132+
fun main() {
133+
val tsGenerator = KxsTsGenerator()
134+
println(tsGenerator.generate(ContactDetails.serializer()))
135+
}
136+
```
137+
138+
> You can get the full code [here](./code/example/example-default-values-primitive-fields-02.kt).
139+
140+
`active` and `phoneNumber` are now required properties. Note that `@Required` had no effect
141+
on `name` or `email`; because they do not have default values, they were already required.
142+
143+
```typescript
144+
export interface ContactDetails {
145+
name: string;
146+
email: string | null;
147+
active: boolean;
148+
phoneNumber: string | null;
149+
}
150+
```
151+
152+
<!--- TEST -->

0 commit comments

Comments
 (0)