Skip to content

Commit

Permalink
DynamicObjects can now access fields by public name. (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
clementetb authored Nov 15, 2023
1 parent be6f709 commit 30c3bfd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.RealmSet
import io.realm.kotlin.types.RealmUUID
import io.realm.kotlin.types.annotations.PersistedName
import org.mongodb.kbson.BsonObjectId
import org.mongodb.kbson.Decimal128

Expand Down Expand Up @@ -178,6 +179,9 @@ class Sample : RealmObject {
val listBacklinks by backlinks(Sample::objectListField)
val setBacklinks by backlinks(Sample::objectSetField)

@PersistedName("persistedStringField")
var publicStringField = "Realm"

// For verification that references inside class is also using our modified accessors and are
// not optimized to use the backing field directly.
fun stringFieldGetter(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package io.realm.kotlin.entities.migration

import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PersistedName

@Suppress("MagicNumber")
class Sample : RealmObject {
var name: String = "Migration"
var stringField: String = "Realm"
var intField: Int = 42

@PersistedName("persistedStringField")
var publicStringField = ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ class PersistedNameTests {

assertNotNull(dynamicSample)
assertEquals("Realm", dynamicSample.getValue("persistedNameStringField"))
assertFailsWithMessage<IllegalArgumentException>("Schema for type 'AlternativePersistedNameSample' doesn't contain a property named 'publicNameStringField'") {
dynamicSample.getValue("publicNameStringField")
}
// We can access property via the public name because the dynamic Realm is build upon a typed
// Realm via the extension function `asDynamicRealm`.
assertEquals("Realm", dynamicSample.getValue("publicNameStringField"))
}

// --------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ class RealmMigrationTests {
PlatformUtils.deleteTempDir(tmpDir)
}

@Test
fun migrationContext_publicNamesNotAvailable() {
migration(
initialSchema = setOf(
io.realm.kotlin.entities.schema.SchemaVariations::class,
io.realm.kotlin.entities.Sample::class
),
migratedSchema = setOf(io.realm.kotlin.entities.migration.Sample::class),
migration = { context ->
val oldRealm = context.oldRealm
val newRealm = context.newRealm

assertNotNull(oldRealm.schema()["Sample"]?.get("persistedStringField"))
assertNull(oldRealm.schema()["Sample"]?.get("publicStringField"))

assertNotNull(newRealm.schema()["Sample"]?.get("persistedStringField"))
assertNull(newRealm.schema()["Sample"]?.get("publicStringField"))
}
)
}

@Test
fun migrationContext_schemaVerification() {
migration(
Expand Down

0 comments on commit 30c3bfd

Please sign in to comment.