-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce @EncodeDefault annotation with two modes: ALWAYS and NEVER (#…
- Loading branch information
1 parent
67cfed3
commit 656ba0c
Showing
9 changed files
with
175 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufNullAndDefaultTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.test.isJsLegacy | ||
import kotlin.test.* | ||
|
||
class ProtobufNullAndDefaultTest { | ||
@Serializable | ||
class ProtoWithNullDefault(val s: String? = null) | ||
|
||
@Serializable | ||
class ProtoWithNullDefaultAlways(@EncodeDefault val s: String? = null) | ||
|
||
@Serializable | ||
class ProtoWithNullDefaultNever(@EncodeDefault(EncodeDefault.Mode.NEVER) val s: String? = null) | ||
|
||
@Test | ||
fun testProtobufDropDefaults() { | ||
val proto = ProtoBuf { encodeDefaults = false } | ||
assertEquals(0, proto.encodeToByteArray(ProtoWithNullDefault()).size) | ||
if (isJsLegacy()) return // because of @EncodeDefault | ||
assertFailsWith<SerializationException> { proto.encodeToByteArray(ProtoWithNullDefaultAlways()) } | ||
assertEquals(0, proto.encodeToByteArray(ProtoWithNullDefaultNever()).size) | ||
} | ||
|
||
@Test | ||
fun testProtobufEncodeDefaults() { | ||
val proto = ProtoBuf { encodeDefaults = true } | ||
assertFailsWith<SerializationException> { proto.encodeToByteArray(ProtoWithNullDefault()) } | ||
if (isJsLegacy()) return // because of @EncodeDefault | ||
assertFailsWith<SerializationException> { proto.encodeToByteArray(ProtoWithNullDefaultAlways()) } | ||
assertEquals(0, proto.encodeToByteArray(ProtoWithNullDefaultNever()).size) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
formats/protobuf/commonTest/src/kotlinx/serialization/test/CurrentPlatform.common.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.test | ||
|
||
enum class Platform { | ||
JVM, JS_LEGACY, JS_IR, NATIVE | ||
} | ||
|
||
public expect val currentPlatform: Platform | ||
|
||
public fun isJs(): Boolean = currentPlatform == Platform.JS_LEGACY || currentPlatform == Platform.JS_IR | ||
public fun isJsLegacy(): Boolean = currentPlatform == Platform.JS_LEGACY | ||
public fun isJvm(): Boolean = currentPlatform == Platform.JVM | ||
public fun isNative(): Boolean = currentPlatform == Platform.NATIVE |
12 changes: 12 additions & 0 deletions
12
formats/protobuf/jsTest/src/kotlinx/serialization/test/CurrentPlatform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.test | ||
|
||
public actual val currentPlatform: Platform = if (isLegacyBackend()) Platform.JS_LEGACY else Platform.JS_IR | ||
|
||
// from https://github.com/JetBrains/kotlin/blob/569187a7516e2e5ab217158a3170d4beb0c5cb5a/js/js.translator/testData/_commonFiles/testUtils.kt#L3 | ||
private fun isLegacyBackend(): Boolean = | ||
// Using eval to prevent DCE from thinking that following code depends on Kotlin module. | ||
eval("(typeof Kotlin != \"undefined\" && typeof Kotlin.kotlin != \"undefined\")").unsafeCast<Boolean>() |
7 changes: 7 additions & 0 deletions
7
formats/protobuf/jvmTest/src/kotlinx/serialization/test/CurrentPlatform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.test | ||
|
||
actual val currentPlatform: Platform = Platform.JVM |
12 changes: 12 additions & 0 deletions
12
formats/protobuf/nativeTest/src/kotlinx/serialization/test/CurrentPlatform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.test | ||
|
||
|
||
import kotlin.native.concurrent.SharedImmutable | ||
|
||
|
||
@SharedImmutable | ||
public actual val currentPlatform: Platform = Platform.NATIVE |