Skip to content

Commit fa028ea

Browse files
author
Adrian Tosca
committed
add java serialization support for Id and RawId
1 parent 4c18883 commit fa028ea

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# typeid-kotlin
22
![Build Status](https://github.com/aleris/typeid-kotlin/actions/workflows/build-on-push.yml/badge.svg)
3-
![Current Version](https://img.shields.io/badge/Version-0.0.8-blue)
3+
![Current Version](https://img.shields.io/badge/Version-0.0.9-blue)
44

55

66
## A Kotlin implementation of [TypeID](https://github.com/jetpack-io/typeid).
@@ -25,14 +25,14 @@ To use with Maven:
2525
<dependency>
2626
<groupId>earth.adi</groupId>
2727
<artifactId>typeid-kotlin</artifactId>
28-
<version>0.0.8</version>
28+
<version>0.0.9</version>
2929
</dependency>
3030
```
3131

3232
To use via Gradle:
3333

3434
```kotlin
35-
implementation("earth.adi:typeid-kotlin:0.0.8")
35+
implementation("earth.adi:typeid-kotlin:0.0.9")
3636
```
3737

3838

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ plugins {
1414

1515
group = "earth.adi"
1616

17-
version = "0.0.8"
17+
version = "0.0.9"
1818

1919
repositories { mavenCentral() }
2020

@@ -159,6 +159,7 @@ tasks.publish { dependsOn(tasks.dokkaHtml) }
159159

160160
jreleaser {
161161
project {
162+
name.set(mavenArtifactId)
162163
description.set(mavenArtifactDescription)
163164
authors.set(arrayListOf("aleris"))
164165
license.set("Apache-2.0")

src/main/kotlin/earth/adi/typeid/RawId.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package earth.adi.typeid
22

33
import earth.adi.typeid.codec.Codec
4+
import java.io.Serializable
45
import java.util.UUID
56

67
/**
@@ -10,7 +11,7 @@ import java.util.UUID
1011
* @property prefix the prefix of the identifier
1112
* @property uuid the uuid of the identifier
1213
*/
13-
data class RawId(val prefix: String, val uuid: UUID) {
14+
data class RawId(val prefix: String, val uuid: UUID) : Serializable {
1415
init {
1516
Codec.requireValidPrefix(prefix)
1617
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package earth.adi.typeid
22

3+
import java.io.Serializable
4+
35
/** Typed prefix for a type id. */
4-
data class TypedPrefix<TEntity>(val prefix: String)
6+
data class TypedPrefix<TEntity>(val prefix: String) : Serializable

src/test/kotlin/earth/adi/typeid/IdTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package earth.adi.typeid
22

3+
import java.io.ByteArrayOutputStream
4+
import java.io.ObjectInputStream
5+
import java.io.ObjectOutputStream
36
import java.util.*
47
import org.assertj.core.api.Assertions.assertThat
58
import org.assertj.core.api.Assertions.assertThatThrownBy
@@ -30,4 +33,17 @@ class IdTest {
3033
val id = Id<String>(TypedPrefix("user"), uuid)
3134
assertThat(id.uuid).isEqualTo(uuid)
3235
}
36+
37+
@Test
38+
fun `test serialization deserialization`() {
39+
val uuid = UUID.fromString("00000000-0000-0000-0000-000000000000")
40+
val id = Id<String>(TypedPrefix("user"), uuid)
41+
ByteArrayOutputStream().use { outputStream ->
42+
ObjectOutputStream(outputStream).writeObject(id)
43+
ObjectInputStream(outputStream.toByteArray().inputStream()).use {
44+
@Suppress("UNCHECKED_CAST") val deserializedId = it.readObject() as Id<String>
45+
assertThat(deserializedId).isEqualTo(id)
46+
}
47+
}
48+
}
3349
}

src/test/kotlin/earth/adi/typeid/RawIdTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package earth.adi.typeid
22

3+
import java.io.ByteArrayOutputStream
4+
import java.io.ObjectInputStream
5+
import java.io.ObjectOutputStream
36
import java.util.*
47
import org.assertj.core.api.Assertions.assertThat
58
import org.junit.jupiter.api.Assertions.*
@@ -17,4 +20,17 @@ class RawIdTest {
1720
val uuid = UUID.randomUUID()
1821
assertThat(RawId("prefix", uuid).uuid).isEqualTo(uuid)
1922
}
23+
24+
@Test
25+
fun `test serialization deserialization`() {
26+
val uuid = UUID.fromString("00000000-0000-0000-0000-000000000000")
27+
val id = RawId("user", uuid)
28+
ByteArrayOutputStream().use { outputStream ->
29+
ObjectOutputStream(outputStream).writeObject(id)
30+
ObjectInputStream(outputStream.toByteArray().inputStream()).use {
31+
val deserializedId = it.readObject() as RawId
32+
assertThat(deserializedId).isEqualTo(id)
33+
}
34+
}
35+
}
2036
}

0 commit comments

Comments
 (0)