From 30c3bfda67a2bc74d7ce56dd1c3315142fd2305a Mon Sep 17 00:00:00 2001 From: clementetb Date: Wed, 15 Nov 2023 10:48:49 +0100 Subject: [PATCH] DynamicObjects can now access fields by public name. (#1571) --- .../kotlin/io/realm/kotlin/entities/Sample.kt | 4 ++++ .../realm/kotlin/entities/migration/Sample.kt | 4 ++++ .../kotlin/test/common/PersistedNameTests.kt | 6 +++--- .../common/migration/RealmMigrationTests.kt | 21 +++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/Sample.kt b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/Sample.kt index 476b969931..d4617e5250 100644 --- a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/Sample.kt +++ b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/Sample.kt @@ -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 @@ -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 { diff --git a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/migration/Sample.kt b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/migration/Sample.kt index 63228127cc..9237a889ca 100644 --- a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/migration/Sample.kt +++ b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/entities/migration/Sample.kt @@ -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 = "" } diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/PersistedNameTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/PersistedNameTests.kt index 603047d1ee..e5af30b72d 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/PersistedNameTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/PersistedNameTests.kt @@ -232,9 +232,9 @@ class PersistedNameTests { assertNotNull(dynamicSample) assertEquals("Realm", dynamicSample.getValue("persistedNameStringField")) - assertFailsWithMessage("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")) } // -------------------------------------------------- diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/migration/RealmMigrationTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/migration/RealmMigrationTests.kt index 090402e387..867f282de2 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/migration/RealmMigrationTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/migration/RealmMigrationTests.kt @@ -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(