Skip to content

Commit

Permalink
Clean up configuration + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clementetb committed Dec 4, 2023
1 parent 8c3f941 commit f64bdf3
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ public fun interface CompactOnLaunchCallback {
public fun shouldCompact(totalBytes: Long, usedBytes: Long): Boolean
}


/**
* TODO
*/
public class TypeAdapterBuilder {
internal val typeAdapters: MutableList<RealmTypeAdapter<*, *>> = 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].
Expand Down Expand Up @@ -197,6 +208,11 @@ public interface Configuration {
*/
public val initialRealmFileConfiguration: InitialRealmFileConfiguration?

/**
* TODO
*/
public val typeAdapters: List<RealmTypeAdapter<*, *>>

/**
* Base class for configuration builders that holds properties available to both
* [RealmConfiguration] and [SyncConfiguration].
Expand Down Expand Up @@ -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<KClass<*>, RealmTypeAdapter<*, *>> = mapOf()
protected var typeAdapters: List<RealmTypeAdapter<*, *>> = listOf()

/**
* Sets the filename of the realm file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<KClass<*>, 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
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public open class ConfigurationImpl(
inMemory: Boolean,
initialRealmFileConfiguration: InitialRealmFileConfiguration?,
logger: ContextLogger,
override val adapters: Map<KClass<*>, RealmTypeAdapter<*, *>>,
override val typeAdapters: List<RealmTypeAdapter<*, *>>,
) : InternalConfiguration {

override val path: String
Expand Down Expand Up @@ -104,6 +104,7 @@ public open class ConfigurationImpl(
override val initialDataCallback: InitialDataCallback?
override val inMemory: Boolean
override val initialRealmFileConfiguration: InitialRealmFileConfiguration?
override val typeAdapterMap: Map<KClass<*>, RealmTypeAdapter<*, *>>

override fun createNativeConfiguration(): RealmConfigurationPointer {
val nativeConfig: RealmConfigurationPointer = RealmInterop.realm_config_new()
Expand Down Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any?, Any?>
}
Expand All @@ -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<Any?, Any?>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface InternalConfiguration : Configuration {
public val writeDispatcherFactory: CoroutineDispatcherFactory
public val schemaMode: SchemaMode
public val logger: ContextLogger
public val adapters: Map<KClass<*>, RealmTypeAdapter<*, *>>
public val typeAdapterMap: Map<KClass<*>, RealmTypeAdapter<*, *>>

// Temporary work-around for https://github.com/realm/realm-kotlin/issues/724
public val isFlexibleSyncConfiguration: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal class RealmConfigurationImpl(
inMemory: Boolean,
override val initialRealmFileConfiguration: InitialRealmFileConfiguration?,
logger: ContextLogger,
adapters: Map<KClass<*>, RealmTypeAdapter<*, *>>
typeAdapters: List<RealmTypeAdapter<*, *>>
) : ConfigurationImpl(
directory,
name,
Expand All @@ -72,6 +72,6 @@ internal class RealmConfigurationImpl(
inMemory,
initialRealmFileConfiguration,
logger,
adapters,
typeAdapters,
),
RealmConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ 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
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
Expand Down Expand Up @@ -493,6 +495,37 @@ class RealmConfigurationTests {
RealmLog.reset()
}

@Test
fun customTypeAdapters_defaultEmpty() {
val typeAdapter = object: RealmTypeAdapter<String, String> {
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<String, String> {
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(
Expand Down

0 comments on commit f64bdf3

Please sign in to comment.