diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/Configuration.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/Configuration.kt index f32bf47a36..4dcb76aa34 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/Configuration.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/Configuration.kt @@ -50,6 +50,17 @@ public fun interface CompactOnLaunchCallback { public fun shouldCompact(totalBytes: Long, usedBytes: Long): Boolean } + +/** + * TODO + */ +public class TypeAdapterBuilder { + internal val typeAdapters: MutableList> = mutableListOf() + public fun add(adapter: RealmTypeAdapter<*,*>) { + typeAdapters.add(adapter) + } +} + /** * This interface is used to write data to a Realm file when the file is first created. * It will be used in a way similar to using [Realm.writeBlocking]. @@ -197,6 +208,11 @@ public interface Configuration { */ public val initialRealmFileConfiguration: InitialRealmFileConfiguration? + /** + * TODO + */ + public val typeAdapters: List> + /** * Base class for configuration builders that holds properties available to both * [RealmConfiguration] and [SyncConfiguration]. @@ -239,7 +255,7 @@ public interface Configuration { protected var initialDataCallback: InitialDataCallback? = null protected var inMemory: Boolean = false protected var initialRealmFileConfiguration: InitialRealmFileConfiguration? = null - protected var typeAdapters: Map, RealmTypeAdapter<*, *>> = mapOf() + protected var typeAdapters: List> = listOf() /** * Sets the filename of the realm file. diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/RealmConfiguration.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/RealmConfiguration.kt index 0e5b876290..ec16057a01 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/RealmConfiguration.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/RealmConfiguration.kt @@ -90,17 +90,12 @@ public interface RealmConfiguration : Configuration { public fun directory(directoryPath: String): Builder = apply { this.directory = directoryPath } - // TODO misplaced, move around - public class TypeAdapterBuilder { - internal val adapters: MutableMap, RealmTypeAdapter<*, *>> = mutableMapOf() - public fun add(adapter: RealmTypeAdapter<*,*>) { - adapters[adapter::class] = adapter - } - } - + /** + * TODO + */ public fun typeAdapters(block: TypeAdapterBuilder.()->Unit): Builder = apply { - this.typeAdapters = TypeAdapterBuilder().apply(block).adapters + this.typeAdapters = TypeAdapterBuilder().apply(block).typeAdapters } /** diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/ConfigurationImpl.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/ConfigurationImpl.kt index c3396ac50d..e0b5f6b152 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/ConfigurationImpl.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/ConfigurationImpl.kt @@ -69,7 +69,7 @@ public open class ConfigurationImpl( inMemory: Boolean, initialRealmFileConfiguration: InitialRealmFileConfiguration?, logger: ContextLogger, - override val adapters: Map, RealmTypeAdapter<*, *>>, + override val typeAdapters: List>, ) : InternalConfiguration { override val path: String @@ -104,6 +104,7 @@ public open class ConfigurationImpl( override val initialDataCallback: InitialDataCallback? override val inMemory: Boolean override val initialRealmFileConfiguration: InitialRealmFileConfiguration? + override val typeAdapterMap: Map, RealmTypeAdapter<*, *>> override fun createNativeConfiguration(): RealmConfigurationPointer { val nativeConfig: RealmConfigurationPointer = RealmInterop.realm_config_new() @@ -150,6 +151,7 @@ public open class ConfigurationImpl( this.initialDataCallback = initialDataCallback this.inMemory = inMemory this.initialRealmFileConfiguration = initialRealmFileConfiguration + this.typeAdapterMap = typeAdapters.associateBy { it::class } // We need to freeze `compactOnLaunchCallback` reference on initial thread for Kotlin Native val compactCallback = compactOnLaunchCallback?.let { callback -> diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/Converters.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/Converters.kt index 1c562bdc88..514211ab46 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/Converters.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/Converters.kt @@ -267,7 +267,7 @@ public inline fun toRealm( ): Any? { val adapter = obj.owner.owner .configuration - .adapters.let { adapters -> + .typeAdapterMap.let { adapters -> require(adapters.contains(adapterClass)) {"User provided adaptes don't contains adapter ${adapterClass.simpleName}"} adapters[adapterClass] as RealmTypeAdapter } @@ -282,7 +282,7 @@ public inline fun fromRealm( ): Any? { val adapter = obj.owner.owner .configuration - .adapters.let { adapters -> + .typeAdapterMap.let { adapters -> require(adapters.contains(adapterClass)) {"User provided adaptes don't contains adapter ${adapterClass.simpleName}"} adapters[adapterClass] as RealmTypeAdapter } diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/InternalConfiguration.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/InternalConfiguration.kt index 33e7a2465b..db5b75156e 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/InternalConfiguration.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/InternalConfiguration.kt @@ -37,7 +37,7 @@ public interface InternalConfiguration : Configuration { public val writeDispatcherFactory: CoroutineDispatcherFactory public val schemaMode: SchemaMode public val logger: ContextLogger - public val adapters: Map, RealmTypeAdapter<*, *>> + public val typeAdapterMap: Map, RealmTypeAdapter<*, *>> // Temporary work-around for https://github.com/realm/realm-kotlin/issues/724 public val isFlexibleSyncConfiguration: Boolean diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/RealmConfigurationImpl.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/RealmConfigurationImpl.kt index d1b9e4ea7c..339b606e87 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/RealmConfigurationImpl.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/RealmConfigurationImpl.kt @@ -49,7 +49,7 @@ internal class RealmConfigurationImpl( inMemory: Boolean, override val initialRealmFileConfiguration: InitialRealmFileConfiguration?, logger: ContextLogger, - adapters: Map, RealmTypeAdapter<*, *>> + typeAdapters: List> ) : ConfigurationImpl( directory, name, @@ -72,6 +72,6 @@ internal class RealmConfigurationImpl( inMemory, initialRealmFileConfiguration, logger, - adapters, + typeAdapters, ), RealmConfiguration diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmConfigurationTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmConfigurationTests.kt index b84b0226d6..e57d229279 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmConfigurationTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/RealmConfigurationTests.kt @@ -34,6 +34,7 @@ import io.realm.kotlin.test.platform.PlatformUtils import io.realm.kotlin.test.platform.platformFileSystem import io.realm.kotlin.test.util.TestLogger import io.realm.kotlin.test.util.use +import io.realm.kotlin.types.RealmTypeAdapter import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.newSingleThreadContext import okio.Path.Companion.toPath @@ -41,6 +42,7 @@ import kotlin.random.Random import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test +import kotlin.test.assertContains import kotlin.test.assertContentEquals import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -493,6 +495,37 @@ class RealmConfigurationTests { RealmLog.reset() } + @Test + fun customTypeAdapters_defaultEmpty() { + val typeAdapter = object: RealmTypeAdapter { + override fun fromRealm(realmValue: String): String = TODO("Not yet implemented") + + override fun toRealm(value: String): String = TODO("Not yet implemented") + } + + val config = RealmConfiguration.Builder(setOf()) + .build() + + assertTrue(config.typeAdapters.isEmpty()) + } + + @Test + fun defineCustomTypeAdapters() { + val typeAdapter = object: RealmTypeAdapter { + override fun fromRealm(realmValue: String): String = TODO("Not yet implemented") + + override fun toRealm(value: String): String = TODO("Not yet implemented") + } + + val config = RealmConfiguration.Builder(setOf()) + .typeAdapters { + add(typeAdapter) + } + .build() + + assertContains(config.typeAdapters, typeAdapter) + } + private fun assertFailsWithEncryptionKey(builder: RealmConfiguration.Builder, keyLength: Int) { val key = Random.nextBytes(keyLength) assertFailsWith(