Skip to content

Commit 7fa81d6

Browse files
committed
Adapter singleton and instance adapter discoverability for RealmInstant
1 parent 16abd75 commit 7fa81d6

File tree

18 files changed

+748
-73
lines changed

18 files changed

+748
-73
lines changed

packages/library-base/src/commonMain/kotlin/io/realm/kotlin/Configuration.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import io.realm.kotlin.log.LogLevel
2525
import io.realm.kotlin.log.RealmLog
2626
import io.realm.kotlin.log.RealmLogger
2727
import io.realm.kotlin.types.BaseRealmObject
28+
import io.realm.kotlin.types.RealmTypeAdapter
2829
import kotlinx.coroutines.CoroutineDispatcher
2930
import kotlin.reflect.KClass
3031

@@ -238,6 +239,7 @@ public interface Configuration {
238239
protected var initialDataCallback: InitialDataCallback? = null
239240
protected var inMemory: Boolean = false
240241
protected var initialRealmFileConfiguration: InitialRealmFileConfiguration? = null
242+
protected var typeAdapters: List<RealmTypeAdapter<*, *>> = listOf()
241243

242244
/**
243245
* Sets the filename of the realm file.

packages/library-base/src/commonMain/kotlin/io/realm/kotlin/RealmConfiguration.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import io.realm.kotlin.log.RealmLog
2424
import io.realm.kotlin.log.RealmLogger
2525
import io.realm.kotlin.migration.AutomaticSchemaMigration
2626
import io.realm.kotlin.migration.RealmMigration
27+
import io.realm.kotlin.types.RealmTypeAdapter
2728
import io.realm.kotlin.types.TypedRealmObject
2829
import kotlin.reflect.KClass
2930

@@ -89,6 +90,19 @@ public interface RealmConfiguration : Configuration {
8990
public fun directory(directoryPath: String): Builder =
9091
apply { this.directory = directoryPath }
9192

93+
// TODO misplaced, move around
94+
public class TypeAdapterBuilder {
95+
internal val adapters: MutableList<RealmTypeAdapter<*, *>> = mutableListOf()
96+
public fun add(adapter: RealmTypeAdapter<*,*>) {
97+
adapters.add(adapter)
98+
}
99+
}
100+
101+
public fun typeAdapters(block: TypeAdapterBuilder.()->Unit): Builder =
102+
apply {
103+
this.typeAdapters = TypeAdapterBuilder().apply(block).adapters
104+
}
105+
92106
/**
93107
* Setting this will change the behavior of how migration exceptions are handled. Instead of
94108
* throwing an exception the on-disc Realm will be cleared and recreated with the new Realm
@@ -192,7 +206,8 @@ public interface RealmConfiguration : Configuration {
192206
initialDataCallback,
193207
inMemory,
194208
initialRealmFileConfiguration,
195-
realmLogger
209+
realmLogger,
210+
typeAdapters,
196211
)
197212
}
198213
}

packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/ConfigurationImpl.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import io.realm.kotlin.internal.util.CoroutineDispatcherFactory
4545
import io.realm.kotlin.migration.AutomaticSchemaMigration
4646
import io.realm.kotlin.migration.RealmMigration
4747
import io.realm.kotlin.types.BaseRealmObject
48+
import io.realm.kotlin.types.RealmTypeAdapter
4849
import kotlin.reflect.KClass
4950

5051
// TODO Public due to being accessed from `library-sync`
@@ -67,7 +68,8 @@ public open class ConfigurationImpl(
6768
override val isFlexibleSyncConfiguration: Boolean,
6869
inMemory: Boolean,
6970
initialRealmFileConfiguration: InitialRealmFileConfiguration?,
70-
logger: ContextLogger
71+
logger: ContextLogger,
72+
override val adapters: List<RealmTypeAdapter<*, *>>,
7173
) : InternalConfiguration {
7274

7375
override val path: String

packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/Converters.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import io.realm.kotlin.types.ObjectId
3636
import io.realm.kotlin.types.RealmAny
3737
import io.realm.kotlin.types.RealmInstant
3838
import io.realm.kotlin.types.RealmObject
39+
import io.realm.kotlin.types.RealmTypeAdapter
3940
import io.realm.kotlin.types.RealmUUID
4041
import io.realm.kotlin.types.geo.GeoBox
4142
import io.realm.kotlin.types.geo.GeoCircle
@@ -259,6 +260,32 @@ internal object IntConverter : CoreIntConverter, CompositeConverter<Int, Long>()
259260
public inline fun intToLong(value: Int?): Long? = value?.toLong()
260261
public inline fun longToInt(value: Long?): Int? = value?.toInt()
261262

263+
public inline fun toRealm(
264+
obj: RealmObjectReference<out BaseRealmObject>,
265+
converterClass: KClass<RealmTypeAdapter<Any, Any>>,
266+
userValue: Any,
267+
): Any {
268+
val adapter = obj.owner.owner
269+
.configuration
270+
.adapters
271+
.first { it::class == converterClass } as RealmTypeAdapter<Any, Any>
272+
273+
return adapter.toRealm(userValue)
274+
}
275+
276+
public inline fun fromRealm(
277+
obj: RealmObjectReference<out BaseRealmObject>,
278+
converterClass: KClass<RealmTypeAdapter<*, *>>,
279+
realmValue: Any,
280+
): Any {
281+
val adapter = obj.owner.owner
282+
.configuration
283+
.adapters
284+
.first { it::class == converterClass } as RealmTypeAdapter<Any, Any>
285+
286+
return adapter.fromRealm(realmValue)
287+
}
288+
262289
internal object RealmInstantConverter : PassThroughPublicConverter<RealmInstant>() {
263290
override inline fun fromRealmValue(realmValue: RealmValue): RealmInstant? =
264291
if (realmValue.isNull()) null else realmValueToRealmInstant(realmValue)

packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/InternalConfiguration.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import io.realm.kotlin.internal.interop.RealmConfigurationPointer
2121
import io.realm.kotlin.internal.interop.SchemaMode
2222
import io.realm.kotlin.internal.util.CoroutineDispatcherFactory
2323
import io.realm.kotlin.types.BaseRealmObject
24+
import io.realm.kotlin.types.RealmTypeAdapter
2425
import kotlin.reflect.KClass
2526

2627
/**
@@ -36,6 +37,7 @@ public interface InternalConfiguration : Configuration {
3637
public val writeDispatcherFactory: CoroutineDispatcherFactory
3738
public val schemaMode: SchemaMode
3839
public val logger: ContextLogger
40+
public val adapters: List<RealmTypeAdapter<*, *>>
3941

4042
// Temporary work-around for https://github.com/realm/realm-kotlin/issues/724
4143
public val isFlexibleSyncConfiguration: Boolean

packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/RealmConfigurationImpl.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import io.realm.kotlin.internal.interop.SchemaMode
2525
import io.realm.kotlin.internal.util.CoroutineDispatcherFactory
2626
import io.realm.kotlin.migration.RealmMigration
2727
import io.realm.kotlin.types.BaseRealmObject
28+
import io.realm.kotlin.types.RealmTypeAdapter
2829
import kotlin.reflect.KClass
2930

3031
public const val REALM_FILE_EXTENSION: String = ".realm"
@@ -47,7 +48,8 @@ internal class RealmConfigurationImpl(
4748
initialDataCallback: InitialDataCallback?,
4849
inMemory: Boolean,
4950
override val initialRealmFileConfiguration: InitialRealmFileConfiguration?,
50-
logger: ContextLogger
51+
logger: ContextLogger,
52+
adapters: List<RealmTypeAdapter<*, *>>
5153
) : ConfigurationImpl(
5254
directory,
5355
name,
@@ -69,6 +71,7 @@ internal class RealmConfigurationImpl(
6971
false,
7072
inMemory,
7173
initialRealmFileConfiguration,
72-
logger
74+
logger,
75+
adapters,
7376
),
7477
RealmConfiguration
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.realm.kotlin.types
2+
3+
/**
4+
* TODO
5+
*
6+
* @param R realm type.
7+
* @param U user type.
8+
*/
9+
public interface RealmTypeAdapter<R, U> { // where P is a supported realm type
10+
11+
public fun fromRealm(realmValue: R): U
12+
13+
public fun toRealm(value: U): R
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2022 Realm Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.realm.kotlin.types.annotations
18+
19+
import io.realm.kotlin.types.RealmTypeAdapter
20+
import kotlin.reflect.KClass
21+
22+
@Retention(AnnotationRetention.SOURCE)
23+
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
24+
@MustBeDocumented
25+
/**
26+
* TODO
27+
*/
28+
public annotation class TypeAdapter(val adapter: KClass<out RealmTypeAdapter<*, *>>)

packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/sync/SyncConfiguration.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ public interface SyncConfiguration : Configuration {
572572
partitionValue == null,
573573
inMemory,
574574
initialRealmFileConfiguration,
575-
realmLogger
575+
realmLogger,
576+
typeAdapters,
576577
)
577578

578579
return SyncConfigurationImpl(

0 commit comments

Comments
 (0)