Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamicObjects can now access fields by public name. #1571

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
Copy link
Contributor

@rorbech rorbech Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be appropriate with a comment here explaining that this is only due to the nature of asDynamicRealm that the public name is available here.

}

// --------------------------------------------------
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