-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use sealed interfaces for forward-compatible enums
Resolves: #467
- Loading branch information
1 parent
3ab5f75
commit 6cd3842
Showing
6 changed files
with
33,412 additions
and
22,681 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
23 changes: 23 additions & 0 deletions
23
src/commonMain/kotlin/org/hildan/chrome/devtools/protocol/FCEnumSerializer.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,23 @@ | ||
package org.hildan.chrome.devtools.protocol | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlin.reflect.* | ||
|
||
abstract class FCEnumSerializer<FC : Any>(fcClass: KClass<FC>) : KSerializer<FC> { | ||
|
||
override val descriptor = PrimitiveSerialDescriptor( | ||
serialName = fcClass.simpleName ?: error("Cannot create serializer for anonymous class"), | ||
kind = PrimitiveKind.STRING, | ||
) | ||
|
||
override fun deserialize(decoder: Decoder): FC = fromCode(decoder.decodeString()) | ||
|
||
override fun serialize(encoder: Encoder, value: FC) { | ||
encoder.encodeString(codeOf(value)) | ||
} | ||
|
||
abstract fun fromCode(code: String): FC | ||
abstract fun codeOf(value: FC): String | ||
} |
49 changes: 49 additions & 0 deletions
49
src/commonTest/kotlin/org/hildan/chrome/devtools/protocol/FCEnumSerializerTest.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,49 @@ | ||
package org.hildan.chrome.devtools.protocol | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.* | ||
import org.hildan.chrome.devtools.domains.accessibility.* | ||
import org.hildan.chrome.devtools.domains.bluetoothemulation.* | ||
import kotlin.test.* | ||
|
||
class FCEnumSerializerTest { | ||
|
||
@Test | ||
fun deserializesKnownValues() { | ||
assertEquals(AXPropertyName.url, Json.decodeFromString<AXPropertyName>("\"url\"")) | ||
assertEquals(AXPropertyName.level, Json.decodeFromString<AXPropertyName>("\"level\"")) | ||
assertEquals(AXPropertyName.hiddenRoot, Json.decodeFromString<AXPropertyName>("\"hiddenRoot\"")) | ||
} | ||
|
||
@Test | ||
fun deserializesKnownValues_withDashes() { | ||
assertEquals(CentralState.poweredOn, Json.decodeFromString<CentralState>("\"powered-on\"")) | ||
assertEquals(CentralState.poweredOff, Json.decodeFromString<CentralState>("\"powered-off\"")) | ||
} | ||
|
||
@Test | ||
fun deserializesUnknownValues() { | ||
assertEquals(AXPropertyName.NotDefinedInProtocol("notRendered"), Json.decodeFromString<AXPropertyName>("\"notRendered\"")) | ||
assertEquals(AXPropertyName.NotDefinedInProtocol("uninteresting"), Json.decodeFromString<AXPropertyName>("\"uninteresting\"")) | ||
} | ||
|
||
@Test | ||
fun serializesKnownValues() { | ||
assertEquals("\"url\"", Json.encodeToString(AXPropertyName.url)) | ||
assertEquals("\"level\"", Json.encodeToString(AXPropertyName.level)) | ||
assertEquals("\"hiddenRoot\"", Json.encodeToString(AXPropertyName.hiddenRoot)) | ||
} | ||
|
||
@Test | ||
fun serializesKnownValues_withDashes() { | ||
assertEquals("\"powered-on\"", Json.encodeToString(CentralState.poweredOn)) | ||
assertEquals("\"powered-off\"", Json.encodeToString(CentralState.poweredOff)) | ||
} | ||
|
||
@Test | ||
fun serializesUnknownValues() { | ||
assertEquals("\"notRendered\"", Json.encodeToString<AXPropertyName>(AXPropertyName.NotDefinedInProtocol("notRendered"))) | ||
assertEquals("\"uninteresting\"", Json.encodeToString(AXPropertyName.NotDefinedInProtocol("uninteresting"))) | ||
assertEquals("\"totallyInexistentStuff\"", Json.encodeToString(AXPropertyName.NotDefinedInProtocol("totallyInexistentStuff"))) | ||
} | ||
} |
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