File tree 4 files changed +96
-2
lines changed
4 files changed +96
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -84,4 +84,31 @@ class DefaultValuesTest : FunSpec({
84
84
actual.shouldTypeScriptCompile(caseName)
85
85
}
86
86
}
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
+ }
87
114
})
Original file line number Diff line number Diff line change 7
7
8
8
* [ Introduction] ( #introduction )
9
9
* [ Overriding output] ( #overriding-output )
10
- * [ Override nullable elements ] ( #override-nullable-elements )
10
+ * [ Override nullable properties ] ( #override-nullable-properties )
11
11
* [ Override both nullable and non-nullable descriptors] ( #override-both-nullable-and-non-nullable-descriptors )
12
12
13
13
<!-- - END -->
@@ -101,7 +101,7 @@ export interface Item {
101
101
102
102
<!-- - TEST TS_COMPILE_OFF -->
103
103
104
- ### Override nullable elements
104
+ ### Override nullable properties
105
105
106
106
Even though UInt is nullable, it should be overridden by the UInt defined in ` descriptorOverrides ` .
107
107
Original file line number Diff line number Diff line change 8
8
* [ Default values] ( #default-values )
9
9
* [ Nullable values] ( #nullable-values )
10
10
* [ Default and nullable] ( #default-and-nullable )
11
+ * [ Override optional properties] ( #override-optional-properties )
11
12
12
13
<!-- - END -->
13
14
@@ -106,3 +107,46 @@ export interface ContactDetails {
106
107
```
107
108
108
109
<!-- - 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 -->
You can’t perform that action at this time.
0 commit comments