From fa028ea2759605b6a840f859e68fd7621ffed724 Mon Sep 17 00:00:00 2001 From: Adrian Tosca Date: Mon, 20 May 2024 09:43:27 +0300 Subject: [PATCH] add java serialization support for Id and RawId --- README.md | 6 +++--- build.gradle.kts | 3 ++- src/main/kotlin/earth/adi/typeid/RawId.kt | 3 ++- src/main/kotlin/earth/adi/typeid/TypedPrefix.kt | 4 +++- src/test/kotlin/earth/adi/typeid/IdTest.kt | 16 ++++++++++++++++ src/test/kotlin/earth/adi/typeid/RawIdTest.kt | 16 ++++++++++++++++ 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 77af979..f147200 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # typeid-kotlin ![Build Status](https://github.com/aleris/typeid-kotlin/actions/workflows/build-on-push.yml/badge.svg) -![Current Version](https://img.shields.io/badge/Version-0.0.8-blue) +![Current Version](https://img.shields.io/badge/Version-0.0.9-blue) ## A Kotlin implementation of [TypeID](https://github.com/jetpack-io/typeid). @@ -25,14 +25,14 @@ To use with Maven: earth.adi typeid-kotlin - 0.0.8 + 0.0.9 ``` To use via Gradle: ```kotlin -implementation("earth.adi:typeid-kotlin:0.0.8") +implementation("earth.adi:typeid-kotlin:0.0.9") ``` diff --git a/build.gradle.kts b/build.gradle.kts index c7fc644..650d53d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ plugins { group = "earth.adi" -version = "0.0.8" +version = "0.0.9" repositories { mavenCentral() } @@ -159,6 +159,7 @@ tasks.publish { dependsOn(tasks.dokkaHtml) } jreleaser { project { + name.set(mavenArtifactId) description.set(mavenArtifactDescription) authors.set(arrayListOf("aleris")) license.set("Apache-2.0") diff --git a/src/main/kotlin/earth/adi/typeid/RawId.kt b/src/main/kotlin/earth/adi/typeid/RawId.kt index 5d79965..ca5cf4e 100644 --- a/src/main/kotlin/earth/adi/typeid/RawId.kt +++ b/src/main/kotlin/earth/adi/typeid/RawId.kt @@ -1,6 +1,7 @@ package earth.adi.typeid import earth.adi.typeid.codec.Codec +import java.io.Serializable import java.util.UUID /** @@ -10,7 +11,7 @@ import java.util.UUID * @property prefix the prefix of the identifier * @property uuid the uuid of the identifier */ -data class RawId(val prefix: String, val uuid: UUID) { +data class RawId(val prefix: String, val uuid: UUID) : Serializable { init { Codec.requireValidPrefix(prefix) } diff --git a/src/main/kotlin/earth/adi/typeid/TypedPrefix.kt b/src/main/kotlin/earth/adi/typeid/TypedPrefix.kt index af0b3f9..e38204c 100644 --- a/src/main/kotlin/earth/adi/typeid/TypedPrefix.kt +++ b/src/main/kotlin/earth/adi/typeid/TypedPrefix.kt @@ -1,4 +1,6 @@ package earth.adi.typeid +import java.io.Serializable + /** Typed prefix for a type id. */ -data class TypedPrefix(val prefix: String) +data class TypedPrefix(val prefix: String) : Serializable diff --git a/src/test/kotlin/earth/adi/typeid/IdTest.kt b/src/test/kotlin/earth/adi/typeid/IdTest.kt index 216d803..06766c7 100644 --- a/src/test/kotlin/earth/adi/typeid/IdTest.kt +++ b/src/test/kotlin/earth/adi/typeid/IdTest.kt @@ -1,5 +1,8 @@ package earth.adi.typeid +import java.io.ByteArrayOutputStream +import java.io.ObjectInputStream +import java.io.ObjectOutputStream import java.util.* import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy @@ -30,4 +33,17 @@ class IdTest { val id = Id(TypedPrefix("user"), uuid) assertThat(id.uuid).isEqualTo(uuid) } + + @Test + fun `test serialization deserialization`() { + val uuid = UUID.fromString("00000000-0000-0000-0000-000000000000") + val id = Id(TypedPrefix("user"), uuid) + ByteArrayOutputStream().use { outputStream -> + ObjectOutputStream(outputStream).writeObject(id) + ObjectInputStream(outputStream.toByteArray().inputStream()).use { + @Suppress("UNCHECKED_CAST") val deserializedId = it.readObject() as Id + assertThat(deserializedId).isEqualTo(id) + } + } + } } diff --git a/src/test/kotlin/earth/adi/typeid/RawIdTest.kt b/src/test/kotlin/earth/adi/typeid/RawIdTest.kt index 886c9f3..c8d4af2 100644 --- a/src/test/kotlin/earth/adi/typeid/RawIdTest.kt +++ b/src/test/kotlin/earth/adi/typeid/RawIdTest.kt @@ -1,5 +1,8 @@ package earth.adi.typeid +import java.io.ByteArrayOutputStream +import java.io.ObjectInputStream +import java.io.ObjectOutputStream import java.util.* import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Assertions.* @@ -17,4 +20,17 @@ class RawIdTest { val uuid = UUID.randomUUID() assertThat(RawId("prefix", uuid).uuid).isEqualTo(uuid) } + + @Test + fun `test serialization deserialization`() { + val uuid = UUID.fromString("00000000-0000-0000-0000-000000000000") + val id = RawId("user", uuid) + ByteArrayOutputStream().use { outputStream -> + ObjectOutputStream(outputStream).writeObject(id) + ObjectInputStream(outputStream.toByteArray().inputStream()).use { + val deserializedId = it.readObject() as RawId + assertThat(deserializedId).isEqualTo(id) + } + } + } }