diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/DynamicMutableRealmObject.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/DynamicMutableRealmObject.kt index 53af038533..ee7908e50e 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/DynamicMutableRealmObject.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/DynamicMutableRealmObject.kt @@ -20,10 +20,7 @@ import io.realm.kotlin.MutableRealm import io.realm.kotlin.Realm import io.realm.kotlin.internal.dynamic.DynamicUnmanagedRealmObject import io.realm.kotlin.schema.RealmStorageType -import io.realm.kotlin.types.RealmDictionary -import io.realm.kotlin.types.RealmList import io.realm.kotlin.types.RealmObject -import io.realm.kotlin.types.RealmSet /** * A **dynamic mutable realm object** gives access and possibility to update the data of the realm @@ -33,16 +30,6 @@ import io.realm.kotlin.types.RealmSet */ public interface DynamicMutableRealmObject : DynamicRealmObject { - override fun getObject(propertyName: String): DynamicMutableRealmObject? - - override fun getObjectList(propertyName: String): RealmList - - override fun getObjectSet(propertyName: String): RealmSet - - override fun getObjectDictionary( - propertyName: String - ): RealmDictionary - /** * Sets the value for the given field. * diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/DynamicRealmObject.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/DynamicRealmObject.kt index 7da966790f..7e38c2c171 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/DynamicRealmObject.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/DynamicRealmObject.kt @@ -18,14 +18,12 @@ package io.realm.kotlin.dynamic import io.realm.kotlin.Realm import io.realm.kotlin.query.RealmResults -import io.realm.kotlin.schema.RealmStorageType import io.realm.kotlin.types.BaseRealmObject import io.realm.kotlin.types.EmbeddedRealmObject -import io.realm.kotlin.types.RealmDictionary -import io.realm.kotlin.types.RealmList import io.realm.kotlin.types.RealmObject -import io.realm.kotlin.types.RealmSet import kotlin.reflect.KClass +import kotlin.reflect.KType +import kotlin.reflect.typeOf /** * A **dynamic realm object** gives access to the data of the realm objects through a generic string @@ -41,350 +39,7 @@ public interface DynamicRealmObject : BaseRealmObject { */ public val type: String - /** - * Returns the value of a specific non-nullable value property. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different value - * types: - * ``` - * // Retrieve a nullable int from a 'nullableIntField' property - * dynamicRealmObject.getNullableValue("nullableIntField", Int::class) - * - * // Retrieve a non-nullable int from an 'intField' property - * dynamicRealmObject.getValue("intField", Int::class) - * - * // Retrieve an object from an `objectField' property - * dynamicRealmObject.getObject("objectField", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the property to retrieve the value for. - * @param clazz the Kotlin class of the value. Must match the [RealmStorageType.kClass] of the - * property in the realm. - * @param T the type of the value. - * @return the property value. - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if `clazz` doesn't match the property's [RealmStorageType.kClass] or if trying to - * retrieve collection properties. - */ - public fun getValue(propertyName: String, clazz: KClass): T - - /** - * Returns the value of a specific nullable value property. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different value - * types: - * ``` - * // Retrieve a nullable int from a 'nullableIntField' property - * dynamicRealmObject.getNullableValue("nullableIntField", Int::class) - * - * // Retrieve a non-nullable int from an 'intField' property - * dynamicRealmObject.getValue("intField", Int::class) - * - * // Retrieve an object from an `objectField' property - * dynamicRealmObject.getObject("objectField", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the property to retrieve the value for. - * @param clazz the Kotlin class of the value. - * @param T the type of the value. - * @return the [RealmList] value. - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if `clazz` doesn't match the property's [RealmStorageType.kClass] or if trying to - * retrieve collection properties. - */ - public fun getNullableValue(propertyName: String, clazz: KClass): T? - - /** - * Returns the value of an object property. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different value - * types: - * ``` - * // Retrieve a nullable int from a 'nullableIntField' property - * dynamicRealmObject.getNullableValue("nullableIntField", Int::class) - * - * // Retrieve a non-nullable int from an 'intField' property - * dynamicRealmObject.getValue("intField", Int::class) - * - * // Retrieve an object from an `objectField' property - * dynamicRealmObject.getObject("objectField", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the property to retrieve the value for. - * @return the [RealmList] value. - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if `clazz` doesn't match the property's [RealmStorageType.kClass] or if trying to - * retrieve collection properties. - */ - public fun getObject(propertyName: String): DynamicRealmObject? - - /** - * Returns the list of non-nullable value elements referenced by the property name as a - * [RealmList]. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different list - * types: - * ``` - * // Retrieve a list of nullable ints from a 'nullableIntList' property - * dynamicRealmObject.getNullableValueList("nullableIntList", Int::class) - * - * // Retrieve a list of non-nullable ints from a 'intList' property - * dynamicRealmObject.getValueList("intList", Int::class) - * - * // Retrieve a list of objects from an `objectList' property - * // Object lists are ALWAYS non-nullable - * dynamicRealmObject.getObjectList("objectList", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the list property to retrieve the list for. - * @param clazz the Kotlin class of the list element type. - * @param T the type of the list element type. - * @return the referenced [RealmList] - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if trying to retrieve values for non-list properties or if `clazz` doesn't match the - * property's [RealmStorageType.kClass]. - */ - public fun getValueList(propertyName: String, clazz: KClass): RealmList - - /** - * Returns the list of nullable elements referenced by the property name as a [RealmList]. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different list - * types: - * ``` - * // Retrieve a list of nullable ints from a 'nullableIntList' property - * dynamicRealmObject.getNullableValueList("nullableIntList", Int::class) - * - * // Retrieve a list of non-nullable ints from a 'intList' property - * dynamicRealmObject.getValueList("intList", Int::class) - * - * // Retrieve a list of objects from an `objectList' property - * // Object lists are ALWAYS non-nullable - * dynamicRealmObject.getObjectList("objectList", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the list property to retrieve the list for. - * @param clazz the Kotlin class of the list element type. - * @param T the type of the list element type. - * @return the referenced [RealmList] - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if trying to retrieve values for non-list properties or if `clazz` doesn't match the - * property's [RealmStorageType.kClass]. - */ - public fun getNullableValueList(propertyName: String, clazz: KClass): RealmList - - /** - * Returns the list of objects referenced by the property name as a [RealmList]. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different list - * types: - * ``` - * // Retrieve a list of nullable ints from a 'nullableIntList' property - * dynamicRealmObject.getNullableValueList("nullableIntList", Int::class) - * - * // Retrieve a list of non-nullable ints from a 'intList' property - * dynamicRealmObject.getValueList("intList", Int::class) - * - * // Retrieve a list of objects from an `objectList' property - * // Object lists are ALWAYS non-nullable - * dynamicRealmObject.getObjectList("objectList", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the list property to retrieve the list for. - * @return the referenced [RealmList] - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if trying to retrieve values for non-list properties or if `clazz` doesn't match the - * property's [RealmStorageType.kClass]. - */ - public fun getObjectList(propertyName: String): RealmList - - /** - * Returns the set of non-nullable value elements referenced by the property name as a - * [RealmSet]. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different set types: - * ``` - * // Retrieve a set of nullable ints from a 'nullableIntSet' property - * dynamicRealmObject.getNullableValueSet("nullableIntSet", Int::class) - * - * // Retrieve a set of non-nullable ints from a 'intSet' property - * dynamicRealmObject.getValueSet("intSet", Int::class) - * - * // Retrieve a set of objects from an `objectSet' property - * // Object sets are ALWAYS non-nullable - * dynamicRealmObject.getObjectSet("objectSet", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the set property to retrieve the set for. - * @param clazz the Kotlin class of the set element type. - * @param T the type of the set element type. - * @return the referenced [RealmSet] - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if trying to retrieve values for non-set properties or if `clazz` doesn't match the - * property's [RealmStorageType.kClass]. - */ - public fun getValueSet(propertyName: String, clazz: KClass): RealmSet - - /** - * Returns the set of nullable elements referenced by the property name as a [RealmSet]. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different set types: - * ``` - * // Retrieve a set of nullable ints from a 'nullableIntSet' property - * dynamicRealmObject.getNullableValueSet("nullableIntSet", Int::class) - * - * // Retrieve a set of non-nullable ints from a 'intSet' property - * dynamicRealmObject.getValueSet("intSet", Int::class) - * - * // Retrieve a set of objects from an `objectSet' property - * // Object sets are ALWAYS non-nullable - * dynamicRealmObject.getObjectSet("objectSet", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the set property to retrieve the set for. - * @param clazz the Kotlin class of the set element type. - * @param T the type of the set element type. - * @return the referenced [RealmSet] - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if trying to retrieve values for non-set properties or if `clazz` doesn't match the - * property's [RealmStorageType.kClass]. - */ - public fun getNullableValueSet(propertyName: String, clazz: KClass): RealmSet - - /** - * Returns the set of objects referenced by the property name as a [RealmSet]. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different set types: - * ``` - * // Retrieve a set of nullable ints from a 'nullableIntSet' property - * dynamicRealmObject.getNullableValueSet("nullableIntSet", Int::class) - * - * // Retrieve a set of non-nullable ints from a 'intSet' property - * dynamicRealmObject.getValueSet("intSet", Int::class) - * - * // Retrieve a set of objects from an `objectSet' property - * // Object sets are ALWAYS non-nullable - * dynamicRealmObject.getObjectSet("objectSet", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the set property to retrieve the set for. - * @return the referenced [RealmSet] - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if trying to retrieve values for non-set properties or if `clazz` doesn't match the - * property's [RealmStorageType.kClass]. - */ - public fun getObjectSet(propertyName: String): RealmSet - - /** - * Returns the dictionary of non-nullable value elements referenced by the property name as a - * [RealmDictionary]. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different dictionary - * types: - * ``` - * // Retrieve a dictionary of nullable ints from a 'nullableIntDictionary' property - * dynamicRealmObject.getNullableValueDictionary("nullableIntDictionary", Int::class) - * - * // Retrieve a dictionary of non-nullable ints from a 'intDictionary' property - * dynamicRealmObject.getValueDictionary("intDictionary", Int::class) - * - * // Retrieve a dictionary of objects from an `objectDictionary' property - * // Object dictionaries are ALWAYS nullable - * dynamicRealmObject.getObjectDictionary("objectDictionary", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the dictionary property to retrieve the dictionary for. - * @param clazz the Kotlin class of the dictionary element type. - * @param T the type of the dictionary element type. - * @return the referenced [RealmDictionary] - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if trying to retrieve values for non-dictionary properties or if `clazz` doesn't match - * the property's [RealmStorageType.kClass]. - */ - public fun getValueDictionary( - propertyName: String, - clazz: KClass - ): RealmDictionary - - /** - * Returns the dictionary of nullable elements referenced by the property name as a - * [RealmDictionary]. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different dictionary - * types: - * ``` - * // Retrieve a dictionary of nullable ints from a 'nullableIntDictionary' property - * dynamicRealmObject.getNullableValueDictionary("nullableIntDictionary", Int::class) - * - * // Retrieve a dictionary of non-nullable ints from a 'intDictionary' property - * dynamicRealmObject.getValueDictionary("intDictionary", Int::class) - * - * // Retrieve a dictionary of objects from an `objectDictionary' property - * // Object dictionaries are ALWAYS nullable - * dynamicRealmObject.getObjectDictionary("objectDictionary", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the dictionary property to retrieve the dictionary for. - * @param clazz the Kotlin class of the dictionary element type. - * @param T the type of the dictionary element type. - * @return the referenced [RealmDictionary] - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if trying to retrieve values for non-dictionary properties or if `clazz` doesn't match - * the property's [RealmStorageType.kClass]. - */ - public fun getNullableValueDictionary( - propertyName: String, - clazz: KClass - ): RealmDictionary - - /** - * Returns the dictionary of objects referenced by the property name as a [RealmDictionary]. - * - * The `class` argument must be the [KClass] of the [RealmStorageType] for the property. - * - * The following snippet outlines the different functions available for the different dictionary - * types: - * ``` - * // Retrieve a dictionary of nullable ints from a 'nullableIntDictionary' property - * dynamicRealmObject.getNullableValueDictionary("nullableIntDictionary", Int::class) - * - * // Retrieve a dictionary of non-nullable ints from a 'intDictionary' property - * dynamicRealmObject.getValueDictionary("intDictionary", Int::class) - * - * // Retrieve a dictionary of objects from an `objectDictionary' property - * // Object dictionaries are ALWAYS nullable - * dynamicRealmObject.getObjectDictionary("objectDictionary", DynamicRealmObject::class) - * ``` - * - * @param propertyName the name of the dictionary property to retrieve the dictionary for. - * @return the referenced [RealmDictionary] - * @throws IllegalArgumentException if the class doesn't contain a field with the specific - * name, if trying to retrieve values for non-dictionary properties or if `clazz` doesn't match - * the property's [RealmStorageType.kClass]. - */ - public fun getObjectDictionary(propertyName: String): RealmDictionary + public fun get(propertyName: String, type: KType): T /** * Returns a backlinks collection referenced by the property name as a [RealmResults]. @@ -397,66 +52,10 @@ public interface DynamicRealmObject : BaseRealmObject { public fun getBacklinks(propertyName: String): RealmResults } -/** - * Returns the value of a specific value property. - * - * Reified convenience wrapper of [DynamicRealmObject.getValue]. - */ -public inline fun DynamicRealmObject.getValue(fieldName: String): T = - this.getValue(fieldName, T::class) - -/** - * Returns the value of a specific nullable value property. - * - * Reified convenience wrapper of [DynamicRealmObject.getNullableValue]. - */ -public inline fun DynamicRealmObject.getNullableValue(fieldName: String): T? = - this.getNullableValue(fieldName, T::class) - -/** - * Returns the list referenced by the property name as a [RealmList]. - * - * Reified convenience wrapper of [DynamicRealmObject.getValueList]. - */ -public inline fun DynamicRealmObject.getValueList(fieldName: String): RealmList = - this.getValueList(fieldName, T::class) - -/** - * Returns the list of nullable elements referenced by the property name as a [RealmList]. - * - * Reified convenience wrapper of [DynamicRealmObject.getNullableValueList]. - */ -public inline fun DynamicRealmObject.getNullableValueList(fieldName: String): RealmList = - this.getNullableValueList(fieldName, T::class) - -/** - * Returns the set referenced by the property name as a [RealmSet]. - * - * Reified convenience wrapper of [DynamicRealmObject.getValueSet]. - */ -public inline fun DynamicRealmObject.getValueSet(fieldName: String): RealmSet = - this.getValueSet(fieldName, T::class) - -/** - * Returns the set of nullable elements referenced by the property name as a [RealmSet]. - * - * Reified convenience wrapper of [DynamicRealmObject.getNullableValueSet]. - */ -public inline fun DynamicRealmObject.getNullableValueSet(fieldName: String): RealmSet = - this.getNullableValueSet(fieldName, T::class) - -/** - * Returns the set referenced by the property name as a [RealmDictionary]. - * - * Reified convenience wrapper of [DynamicRealmObject.getValueSet]. - */ -public inline fun DynamicRealmObject.getValueDictionary(fieldName: String): RealmDictionary = - this.getValueDictionary(fieldName, T::class) +public inline fun DynamicRealmObject.get(propertyName: String): T { + return get(propertyName, typeOf()) +} -/** - * Returns the set of nullable elements referenced by the property name as a [RealmDictionary]. - * - * Reified convenience wrapper of [DynamicRealmObject.getNullableValueSet]. - */ -public inline fun DynamicRealmObject.getNullableValueDictionary(fieldName: String): RealmDictionary = - this.getNullableValueDictionary(fieldName, T::class) +public inline fun DynamicRealmObject.get(propertyName: String, clazz: KClass): T { + return get(propertyName, typeOf()) +} diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/RealmObjectHelper.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/RealmObjectHelper.kt index f21e85de1d..4305cb5bf2 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/RealmObjectHelper.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/RealmObjectHelper.kt @@ -70,6 +70,7 @@ import org.mongodb.kbson.Decimal128 import kotlin.reflect.KClass import kotlin.reflect.KMutableProperty1 import kotlin.reflect.KProperty1 +import kotlin.reflect.KType /** * This object holds helper methods for the compiler plugin generated methods, providing the @@ -888,16 +889,137 @@ internal object RealmObjectHelper { } } + internal fun dynamicGetFromKType( + obj: RealmObjectReference, + propertyName: String, + type: KType, + issueDynamicMutableObject: Boolean = false + ): R { + obj.checkValid() + val collectionType = when { + type.classifier == RealmList::class -> CollectionType.RLM_COLLECTION_TYPE_LIST + type.classifier == RealmSet::class -> CollectionType.RLM_COLLECTION_TYPE_SET + type.classifier == RealmDictionary::class -> CollectionType.RLM_COLLECTION_TYPE_DICTIONARY + else -> CollectionType.RLM_COLLECTION_TYPE_NONE + } + val elementType: KType = if (collectionType != CollectionType.RLM_COLLECTION_TYPE_NONE) { + type.arguments[0].type!! + } else type + + val propertyMetadata = checkPropertyType( + obj, + propertyName, + collectionType, + elementType.classifier as KClass<*>, +// type.classifier as KClass, + elementType.isMarkedNullable + ) + val operatorType = when { + propertyMetadata.type == PropertyType.RLM_PROPERTY_TYPE_MIXED -> + CollectionOperatorType.REALM_ANY + + propertyMetadata.type != PropertyType.RLM_PROPERTY_TYPE_OBJECT -> + CollectionOperatorType.PRIMITIVE + + !obj.owner.schemaMetadata[propertyMetadata.linkTarget]!!.isEmbeddedRealmObject -> + CollectionOperatorType.REALM_OBJECT + + else -> CollectionOperatorType.EMBEDDED_OBJECT + } + // FIXME get to return collections in mixed + return when (collectionType) { + CollectionType.RLM_COLLECTION_TYPE_NONE -> { + return getterScope { + val transport = realm_get_value(obj.objectPointer, propertyMetadata.key) + + // Consider moving this dynamic conversion to Converters.kt + val value = when (type.classifier) { + DynamicRealmObject::class, + DynamicMutableRealmObject::class -> realmValueToRealmObject( + transport, + type.classifier as KClass, + obj.mediator, + obj.owner + ) + RealmAny::class -> realmValueToRealmAny( + realmValue = transport, + parent = obj, + mediator = obj.mediator, + owner = obj.owner, + issueDynamicObject = true, + issueDynamicMutableObject = issueDynamicMutableObject, + getListFunction = { + RealmInterop.realm_get_list( + obj.objectPointer, + propertyMetadata.key + ) + }, + getDictionaryFunction = { + RealmInterop.realm_get_dictionary( + obj.objectPointer, + propertyMetadata.key + ) + } + ) + + else -> with(primitiveTypeConverters.getValue(type.classifier as KClass)) { + realmValueToPublic(transport) + } + } + value?.let { + @Suppress("UNCHECKED_CAST") + if ((type.classifier as KClass).isInstance(value)) { + value as R + } else { + throw ClassCastException("Retrieving value of type '${(type.classifier as KClass).simpleName}' but was of type '${value::class.simpleName}'") + } + } as R + } + } + CollectionType.RLM_COLLECTION_TYPE_LIST -> { + getListByKey( + obj, + propertyMetadata, + elementType.classifier as KClass, + operatorType, + true, + issueDynamicMutableObject + ) as R + } + CollectionType.RLM_COLLECTION_TYPE_SET -> { + getSetByKey( + obj, + propertyMetadata, + elementType.classifier as KClass, + operatorType, + true, + issueDynamicMutableObject + ) as R + } + CollectionType.RLM_COLLECTION_TYPE_DICTIONARY -> { + getDictionaryByKey( + obj, + propertyMetadata, + elementType.classifier as KClass, + operatorType, + true, + issueDynamicMutableObject + ) as R + } + else -> sdkError("Unknown collection type $collectionType") + } + } + /** * Get values for non-collection properties by name. * * This will verify that the requested type (`clazz`) and nullability matches the property * properties in the schema. */ - internal fun dynamicGet( + internal fun dynamicGet( obj: RealmObjectReference, propertyName: String, - clazz: KClass, + clazz: KClass, nullable: Boolean, issueDynamicMutableObject: Boolean = false ): R? { diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicMutableRealmObjectImpl.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicMutableRealmObjectImpl.kt index ad8d6088eb..88af5c786f 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicMutableRealmObjectImpl.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicMutableRealmObjectImpl.kt @@ -18,125 +18,19 @@ package io.realm.kotlin.internal.dynamic import io.realm.kotlin.dynamic.DynamicMutableRealmObject import io.realm.kotlin.internal.RealmObjectHelper -import io.realm.kotlin.types.RealmDictionary -import io.realm.kotlin.types.RealmList -import io.realm.kotlin.types.RealmSet -import kotlin.reflect.KClass +import kotlin.reflect.KType internal class DynamicMutableRealmObjectImpl : DynamicMutableRealmObject, DynamicRealmObjectImpl() { - override fun getValue(propertyName: String, clazz: KClass): T { - // dynamicGetSingle checks nullability of property, so null pointer check raises appropriate NPE - return RealmObjectHelper.dynamicGet( - this.`io_realm_kotlin_objectReference`!!, - propertyName, - clazz, - nullable = false, - issueDynamicMutableObject = true - )!! - } - - override fun getNullableValue(propertyName: String, clazz: KClass): T? { - return RealmObjectHelper.dynamicGet( - `io_realm_kotlin_objectReference`!!, - propertyName, - clazz, - nullable = true, - issueDynamicMutableObject = true + override fun get(propertyName: String, type: KType): T { + return RealmObjectHelper.dynamicGetFromKType( + obj = this.`io_realm_kotlin_objectReference`!!, + propertyName = propertyName, + type = type, + issueDynamicMutableObject = true, ) } - override fun getObject(propertyName: String): DynamicMutableRealmObject? { - return getNullableValue(propertyName, DynamicMutableRealmObject::class) - } - - override fun getValueList(propertyName: String, clazz: KClass): RealmList { - return RealmObjectHelper.dynamicGetList( - `io_realm_kotlin_objectReference`!!, - propertyName, - clazz, - nullable = false, - issueDynamicMutableObject = true - ).let { - @Suppress("unchecked_cast") - it as RealmList - } - } - - override fun getNullableValueList(propertyName: String, clazz: KClass): RealmList { - return RealmObjectHelper.dynamicGetList( - `io_realm_kotlin_objectReference`!!, - propertyName, - clazz, - nullable = true, - issueDynamicMutableObject = true - ) - } - - override fun getObjectList(propertyName: String): RealmList { - return getValueList(propertyName, DynamicMutableRealmObject::class) - } - - override fun getValueSet(propertyName: String, clazz: KClass): RealmSet { - return RealmObjectHelper.dynamicGetSet( - `io_realm_kotlin_objectReference`!!, - propertyName, - clazz, - nullable = false, - issueDynamicMutableObject = true - ).let { - @Suppress("unchecked_cast") - it as RealmSet - } - } - - override fun getNullableValueSet(propertyName: String, clazz: KClass): RealmSet { - return RealmObjectHelper.dynamicGetSet( - `io_realm_kotlin_objectReference`!!, - propertyName, - clazz, - nullable = true, - issueDynamicMutableObject = true - ) - } - - override fun getObjectSet(propertyName: String): RealmSet { - return getValueSet(propertyName, DynamicMutableRealmObject::class) - } - - override fun getValueDictionary( - propertyName: String, - clazz: KClass, - ): RealmDictionary { - return RealmObjectHelper.dynamicGetDictionary( - `io_realm_kotlin_objectReference`!!, - propertyName, - clazz, - nullable = false, - issueDynamicMutableObject = true - ).let { - @Suppress("unchecked_cast") - it as RealmDictionary - } - } - - override fun getNullableValueDictionary( - propertyName: String, - clazz: KClass - ): RealmDictionary { - return RealmObjectHelper.dynamicGetDictionary( - `io_realm_kotlin_objectReference`!!, - propertyName, - clazz, - nullable = true, - issueDynamicMutableObject = true - ) - } - - override fun getObjectDictionary(propertyName: String): RealmDictionary { - return getNullableValueDictionary(propertyName, DynamicMutableRealmObject::class) - } - override fun set(propertyName: String, value: T): DynamicMutableRealmObject { // `io_realm_kotlin_objectReference` is not null, as DynamicMutableRealmObject are always managed val reference = this.io_realm_kotlin_objectReference!! diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicRealmObjectImpl.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicRealmObjectImpl.kt index 5179fb4298..3314464610 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicRealmObjectImpl.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicRealmObjectImpl.kt @@ -22,10 +22,7 @@ import io.realm.kotlin.internal.RealmObjectInternal import io.realm.kotlin.internal.RealmObjectReference import io.realm.kotlin.query.RealmResults import io.realm.kotlin.types.BaseRealmObject -import io.realm.kotlin.types.RealmDictionary -import io.realm.kotlin.types.RealmList -import io.realm.kotlin.types.RealmSet -import kotlin.reflect.KClass +import kotlin.reflect.KType public open class DynamicRealmObjectImpl : DynamicRealmObject, RealmObjectInternal { override val type: String @@ -34,126 +31,17 @@ public open class DynamicRealmObjectImpl : DynamicRealmObject, RealmObjectIntern // This should never be null after initialization of a dynamic object, but we currently cannot // represent that in the type system as we one some code paths construct the Kotlin object // before having the realm object reference - override var `io_realm_kotlin_objectReference`: RealmObjectReference? = - null + override var `io_realm_kotlin_objectReference`: RealmObjectReference? = null - override fun getValue(propertyName: String, clazz: KClass): T { - // dynamicGetSingle checks nullability of property, so null pointer check raises appropriate NPE - return RealmObjectHelper.dynamicGet( + override fun get(propertyName: String, type: KType): T { + return RealmObjectHelper.dynamicGetFromKType( obj = this.`io_realm_kotlin_objectReference`!!, propertyName = propertyName, - clazz = clazz, - nullable = false - )!! - } - - override fun getNullableValue(propertyName: String, clazz: KClass): T? { - return RealmObjectHelper.dynamicGet( - obj = `io_realm_kotlin_objectReference`!!, - propertyName = propertyName, - clazz = clazz, - nullable = true - ) - } - - override fun getObject(propertyName: String): DynamicRealmObject? { - return getNullableValue(propertyName, DynamicRealmObject::class) - } - - override fun getValueList(propertyName: String, clazz: KClass): RealmList { - return RealmObjectHelper.dynamicGetList( - obj = `io_realm_kotlin_objectReference`!!, - propertyName = propertyName, - clazz = clazz, - nullable = false - ) - .let { - @Suppress("unchecked_cast") - it as RealmList - } - } - - override fun getNullableValueList( - propertyName: String, - clazz: KClass, - ): RealmList { - return RealmObjectHelper.dynamicGetList( - obj = `io_realm_kotlin_objectReference`!!, - propertyName = propertyName, - clazz = clazz, - nullable = true - ) - } - - override fun getObjectList(propertyName: String): RealmList { - return getValueList(propertyName, DynamicRealmObject::class) - } - - override fun getValueSet(propertyName: String, clazz: KClass): RealmSet { - return RealmObjectHelper.dynamicGetSet( - obj = `io_realm_kotlin_objectReference`!!, - propertyName = propertyName, - clazz = clazz, - nullable = false - ) - .let { - @Suppress("unchecked_cast") - it as RealmSet - } - } - - override fun getNullableValueSet( - propertyName: String, - clazz: KClass, - ): RealmSet { - return RealmObjectHelper.dynamicGetSet( - obj = `io_realm_kotlin_objectReference`!!, - propertyName = propertyName, - clazz = clazz, - nullable = true - ) - } - - override fun getObjectSet(propertyName: String): RealmSet { - return getValueSet(propertyName, DynamicRealmObject::class) - } - - override fun getValueDictionary( - propertyName: String, - clazz: KClass, - ): RealmDictionary { - return RealmObjectHelper.dynamicGetDictionary( - obj = `io_realm_kotlin_objectReference`!!, - propertyName = propertyName, - clazz = clazz, - nullable = false + type = type ) - .let { - @Suppress("unchecked_cast") - it as RealmDictionary - } - } - - override fun getNullableValueDictionary( - propertyName: String, - clazz: KClass, - ): RealmDictionary { - return RealmObjectHelper.dynamicGetDictionary( - obj = `io_realm_kotlin_objectReference`!!, - propertyName = propertyName, - clazz = clazz, - nullable = true - ) - } - - override fun getObjectDictionary(propertyName: String): RealmDictionary { - return getValueDictionary(propertyName, DynamicRealmObject::class) } override fun getBacklinks(propertyName: String): RealmResults { - return RealmObjectHelper.dynamicGetBacklinks( - `io_realm_kotlin_objectReference`!!, - propertyName - ) + return RealmObjectHelper.dynamicGetBacklinks(`io_realm_kotlin_objectReference`!!, propertyName) } } diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicUnmanagedRealmObject.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicUnmanagedRealmObject.kt index 99f7947433..f3fe1368fc 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicUnmanagedRealmObject.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/dynamic/DynamicUnmanagedRealmObject.kt @@ -14,7 +14,7 @@ import io.realm.kotlin.types.BaseRealmObject import io.realm.kotlin.types.RealmDictionary import io.realm.kotlin.types.RealmList import io.realm.kotlin.types.RealmSet -import kotlin.reflect.KClass +import kotlin.reflect.KType internal class DynamicUnmanagedRealmObject( override val type: String, @@ -29,60 +29,18 @@ internal class DynamicUnmanagedRealmObject( val properties: MutableMap = properties.toMutableMap() - override fun getValue(propertyName: String, clazz: KClass): T = - properties[propertyName] as T - - override fun getNullableValue(propertyName: String, clazz: KClass): T? = - properties[propertyName] as T? - - override fun getObject(propertyName: String): DynamicMutableRealmObject? = - properties[propertyName] as DynamicMutableRealmObject? - - override fun getValueList(propertyName: String, clazz: KClass): RealmList = - properties.getOrPut(propertyName) { realmListOf() } as RealmList - - override fun getNullableValueList( - propertyName: String, - clazz: KClass - ): RealmList = properties.getOrPut(propertyName) { realmListOf() } as RealmList - - override fun getObjectList(propertyName: String): RealmList = - properties.getOrPut(propertyName) { realmListOf() } - as RealmList - - override fun getValueSet(propertyName: String, clazz: KClass): RealmSet = - properties.getOrPut(propertyName) { realmSetOf() } as RealmSet - - override fun getNullableValueSet( - propertyName: String, - clazz: KClass - ): RealmSet = properties.getOrPut(propertyName) { realmSetOf() } as RealmSet - - override fun getValueDictionary( - propertyName: String, - clazz: KClass - ): RealmDictionary = - properties.getOrPut(propertyName) { realmDictionaryOf() } as RealmDictionary - - override fun getNullableValueDictionary( - propertyName: String, - clazz: KClass - ): RealmDictionary = - properties.getOrPut(propertyName) { realmDictionaryOf() } as RealmDictionary + override fun get(propertyName: String, type: KType): T = + when (type.classifier) { + // FIXME Generic parameter for collection constructors needs to be of the element type + RealmList::class -> properties.getOrPut(propertyName) { realmListOf() } as T + RealmSet::class -> properties.getOrPut(propertyName) { realmSetOf() } as T + RealmDictionary::class -> properties.getOrPut(propertyName) { realmDictionaryOf() } as T + else -> properties[propertyName] as T + } override fun getBacklinks(propertyName: String): RealmResults = throw IllegalStateException("Unmanaged dynamic realm objects do not support backlinks.") - override fun getObjectSet(propertyName: String): RealmSet = - properties.getOrPut(propertyName) { realmSetOf() } - as RealmSet - - override fun getObjectDictionary( - propertyName: String - ): RealmDictionary = - properties.getOrPut(propertyName) { realmDictionaryOf() } - as RealmDictionary - override fun set(propertyName: String, value: T): DynamicMutableRealmObject { properties[propertyName] = value as Any return this 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 e8c9fbe96d..65bb42329b 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 @@ -20,7 +20,7 @@ package io.realm.kotlin.test.common import io.realm.kotlin.Realm import io.realm.kotlin.RealmConfiguration -import io.realm.kotlin.dynamic.getValue +import io.realm.kotlin.dynamic.get import io.realm.kotlin.ext.backlinks import io.realm.kotlin.ext.query import io.realm.kotlin.ext.realmListOf @@ -217,10 +217,10 @@ class PersistedNameTests { .single() assertNotNull(dynamicSample) - assertEquals("Realm", dynamicSample.getValue("persistedNameStringField")) + assertEquals("Realm", dynamicSample.get("persistedNameStringField")) // 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")) + assertEquals("Realm", dynamicSample.get("publicNameStringField")) } // -------------------------------------------------- diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicMutableRealmObjectTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicMutableRealmObjectTests.kt index f07fd73009..159cbd5fb6 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicMutableRealmObjectTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicMutableRealmObjectTests.kt @@ -22,14 +22,7 @@ import io.realm.kotlin.RealmConfiguration import io.realm.kotlin.dynamic.DynamicMutableRealm import io.realm.kotlin.dynamic.DynamicMutableRealmObject import io.realm.kotlin.dynamic.DynamicRealmObject -import io.realm.kotlin.dynamic.getNullableValue -import io.realm.kotlin.dynamic.getNullableValueDictionary -import io.realm.kotlin.dynamic.getNullableValueList -import io.realm.kotlin.dynamic.getNullableValueSet -import io.realm.kotlin.dynamic.getValue -import io.realm.kotlin.dynamic.getValueDictionary -import io.realm.kotlin.dynamic.getValueList -import io.realm.kotlin.dynamic.getValueSet +import io.realm.kotlin.dynamic.get import io.realm.kotlin.entities.Sample import io.realm.kotlin.entities.embedded.embeddedSchema import io.realm.kotlin.entities.embedded.embeddedSchemaWithPrimaryKey @@ -124,7 +117,7 @@ class DynamicMutableRealmObjectTests { ) ) assertTrue(parent.isManaged()) - val child: DynamicMutableRealmObject? = parent.getObject("nullableObject") + val child: DynamicMutableRealmObject? = parent.get("nullableObject") assertNotNull(child) assertTrue(child.isManaged()) child.set("stringField", "UPDATED_CHILD") @@ -145,11 +138,11 @@ class DynamicMutableRealmObjectTests { ) ) parent.run { - assertEquals("PARENT", getValue("stringField")) - val child: DynamicMutableRealmObject? = parent.getObject("nullableObject") + assertEquals("PARENT", get("stringField")) + val child: DynamicMutableRealmObject? = parent.get("nullableObject") assertNotNull(child) assertTrue(child.isManaged()) - assertEquals("CHILD", child.getValue("stringField")) + assertEquals("CHILD", child.get("stringField")) } } @@ -173,82 +166,82 @@ class DynamicMutableRealmObjectTests { when (type.storageType) { RealmStorageType.BOOL -> { dynamicSample.set(name, true) - assertEquals(true, dynamicSample.getNullableValue(name)) + assertEquals(true, dynamicSample.get(name)) dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) } RealmStorageType.INT -> { dynamicSample.set(name, 42L) - assertEquals(42L, dynamicSample.getNullableValue(name)) + assertEquals(42L, dynamicSample.get(name)) dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) } RealmStorageType.STRING -> { dynamicSample.set(name, "STRING") - assertEquals("STRING", dynamicSample.getNullableValue(name)) + assertEquals("STRING", dynamicSample.get(name)) dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) } RealmStorageType.OBJECT -> { dynamicSample.set(name, Sample()) - val nullableObject = dynamicSample.getObject(name) + val nullableObject = dynamicSample.get(name) assertNotNull(nullableObject) - assertEquals("Realm", nullableObject.getValue("stringField")) + assertEquals("Realm", nullableObject.get("stringField")) dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getObject(name)) + assertEquals(null, dynamicSample.get(name)) } RealmStorageType.FLOAT -> { dynamicSample.set(name, 4.2f) - assertEquals(4.2f, dynamicSample.getNullableValue(name)) + assertEquals(4.2f, dynamicSample.get(name)) dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) } RealmStorageType.DOUBLE -> { dynamicSample.set(name, 4.2) - assertEquals(4.2, dynamicSample.getNullableValue(name)) + assertEquals(4.2, dynamicSample.get(name)) dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) } RealmStorageType.DECIMAL128 -> { val value = Decimal128("1.84467440731231618E-615") dynamicSample.set(name, value) - assertEquals(value, dynamicSample.getNullableValue(name)) + assertEquals(value, dynamicSample.get(name)) dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) } RealmStorageType.TIMESTAMP -> { val value = RealmInstant.from(100, 100) dynamicSample.set(name, value) - assertEquals(value, dynamicSample.getNullableValue(name)) + assertEquals(value, dynamicSample.get(name)) dynamicSample.set(name, null) assertEquals( null, - dynamicSample.getNullableValue(name) + dynamicSample.get(name) ) } RealmStorageType.OBJECT_ID -> { val value = BsonObjectId() dynamicSample.set(name, value) - assertEquals(value, dynamicSample.getNullableValue(name)) + assertEquals(value, dynamicSample.get(name)) dynamicSample.set(name, null) assertEquals( null, - dynamicSample.getNullableValue(name) + dynamicSample.get(name) ) } RealmStorageType.UUID -> { val value = RealmUUID.random() dynamicSample.set(name, value) - assertEquals(value, dynamicSample.getNullableValue(name)) + assertEquals(value, dynamicSample.get(name)) dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) } RealmStorageType.BINARY -> { val value = byteArrayOf(42) dynamicSample.set(name, value) - assertContentEquals(value, dynamicSample.getNullableValue(name)) + assertContentEquals(value, dynamicSample.get(name)) dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) } RealmStorageType.ANY -> { // Check writing a regular object using the Dynamic API throws @@ -262,12 +255,12 @@ class DynamicMutableRealmObjectTests { // Test we can set null ... dynamicSample.set(name, null) - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) // ... and primitives... val value = RealmAny.create(42) dynamicSample.set(name, value) - assertEquals(value, dynamicSample.getNullableValue(name)) + assertEquals(value, dynamicSample.get(name)) // ... and dynamic mutable unmanaged objects ... DynamicMutableRealmObject.create( @@ -278,10 +271,10 @@ class DynamicMutableRealmObjectTests { RealmAny.create(dynamicMutableUnmanagedObject) dynamicSample.set(name, dynamicRealmAny) val expectedValue = - dynamicMutableUnmanagedObject.getValue("stringField") - val actualValue = dynamicSample.getNullableValue(name) + dynamicMutableUnmanagedObject.get("stringField") + val actualValue = dynamicSample.get(name) ?.asRealmObject() - ?.getValue("stringField") + ?.get("stringField") assertEquals(expectedValue, actualValue) } @@ -296,19 +289,19 @@ class DynamicMutableRealmObjectTests { RealmAny.create(dynamicMutableManagedObject) dynamicSample.set(name, dynamicRealmAny) val expectedValue = - dynamicMutableManagedObject.getValue("stringField") + dynamicMutableManagedObject.get("stringField") val managedDynamicMutableObject = - dynamicSample.getNullableValue(name) + dynamicSample.get(name) ?.asRealmObject() val actualValue = - managedDynamicMutableObject?.getValue("stringField") + managedDynamicMutableObject?.get("stringField") assertEquals(expectedValue, actualValue) // Check we did indeed get a dynamic mutable object managedDynamicMutableObject?.set("stringField", "NEW") assertEquals( "NEW", - managedDynamicMutableObject?.getValue("stringField") + managedDynamicMutableObject?.get("stringField") ) } // Collections in RealmAny are tested in @@ -322,48 +315,48 @@ class DynamicMutableRealmObjectTests { when (type.storageType) { RealmStorageType.BOOL -> { dynamicSample.set(name, true) - assertEquals(true, dynamicSample.getValue(name)) + assertEquals(true, dynamicSample.get(name)) } RealmStorageType.INT -> { dynamicSample.set(name, 42L) - assertEquals(42L, dynamicSample.getValue(name)) + assertEquals(42L, dynamicSample.get(name)) } RealmStorageType.STRING -> { dynamicSample.set(name, "STRING") - assertEquals("STRING", dynamicSample.getValue(name)) + assertEquals("STRING", dynamicSample.get(name)) } RealmStorageType.FLOAT -> { dynamicSample.set(name, 4.2f) - assertEquals(4.2f, dynamicSample.getValue(name)) + assertEquals(4.2f, dynamicSample.get(name)) } RealmStorageType.DOUBLE -> { dynamicSample.set(name, 4.2) - assertEquals(4.2, dynamicSample.getValue(name)) + assertEquals(4.2, dynamicSample.get(name)) } RealmStorageType.DECIMAL128 -> { val value = Decimal128("1.84467440731231618E-615") dynamicSample.set(name, value) - assertEquals(value, dynamicSample.getValue(name)) + assertEquals(value, dynamicSample.get(name)) } RealmStorageType.TIMESTAMP -> { val value = RealmInstant.from(100, 100) dynamicSample.set(name, value) - assertEquals(value, dynamicSample.getValue(name)) + assertEquals(value, dynamicSample.get(name)) } RealmStorageType.OBJECT_ID -> { val value = BsonObjectId() dynamicSample.set(name, value) - assertEquals(value, dynamicSample.getValue(name)) + assertEquals(value, dynamicSample.get(name)) } RealmStorageType.UUID -> { val value = RealmUUID.random() dynamicSample.set(name, value) - assertEquals(value, dynamicSample.getValue(name)) + assertEquals(value, dynamicSample.get(name)) } RealmStorageType.BINARY -> { val value = byteArrayOf(42) dynamicSample.set(name, value) - assertContentEquals(value, dynamicSample.getValue(name)) + assertContentEquals(value, dynamicSample.get(name)) } else -> error("Model contains untested properties: $property") } @@ -382,12 +375,12 @@ class DynamicMutableRealmObjectTests { set(Sample::nullableObject.name, dynamicSample) } "listBacklinks" -> { - getValueList(Sample::objectListField.name).add( + get>(Sample::objectListField.name).add( dynamicSample ) } "setBacklinks" -> { - getValueSet(Sample::objectSetField.name).add( + get>(Sample::objectSetField.name).add( dynamicSample ) } @@ -397,30 +390,14 @@ class DynamicMutableRealmObjectTests { ) assertTrue(linkingObjects.isNotEmpty()) assertEquals( - target.getValue(Sample::stringField.name), - linkingObjects.first().getValue(Sample::stringField.name) + target.get(Sample::stringField.name), + linkingObjects.first().get(Sample::stringField.name) ) } else if (type.isNullable) { - fun assertionsForNullable( - listFromSample: RealmList, - property: RealmProperty, - value: T, - clazz: KClass<*> - ) { - listFromSample.add(value) - listFromSample.add(null) - val listOfNullable = dynamicSample.getNullableValueList( - property.name, - clazz - ) - assertEquals(2, listOfNullable.size) - assertEquals(value, listOfNullable[0]) - assertNull(listOfNullable[1]) - } - when (type.storageType) { RealmStorageType.BOOL -> assertionsForNullable( - dynamicSample.getNullableValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, true, Boolean::class @@ -435,50 +412,58 @@ class DynamicMutableRealmObjectTests { else -> error("Unexpected integral field ${property.name}") } assertionsForNullable( - dynamicSample.getNullableValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, value, Long::class ) } RealmStorageType.STRING -> assertionsForNullable( - dynamicSample.getNullableValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, "NEW_ELEMENT", String::class ) RealmStorageType.FLOAT -> assertionsForNullable( - dynamicSample.getNullableValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234f, Float::class ) RealmStorageType.DOUBLE -> assertionsForNullable( - dynamicSample.getNullableValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234, Double::class ) RealmStorageType.DECIMAL128 -> assertionsForNullable( - dynamicSample.getNullableValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, Decimal128("1.84467440731231618E-615"), BsonDecimal128::class ) RealmStorageType.TIMESTAMP -> assertionsForNullable( - dynamicSample.getNullableValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmInstant.from(100, 100), RealmInstant::class ) RealmStorageType.OBJECT_ID -> assertionsForNullable( - dynamicSample.getNullableValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, BsonObjectId(), BsonObjectId::class ) RealmStorageType.UUID -> assertionsForNullable( - dynamicSample.getNullableValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmUUID.random(), RealmUUID::class @@ -486,13 +471,13 @@ class DynamicMutableRealmObjectTests { RealmStorageType.BINARY -> { // TODO use assertionsForNullable when we add support for structural equality for RealmList val value = byteArrayOf(42) - dynamicSample.getNullableValueList(property.name) + dynamicSample.get>(property.name) .add(value) - dynamicSample.getNullableValueList(property.name) + dynamicSample.get>(property.name) .add(null) - val listOfNullable = dynamicSample.getNullableValueList( + // FIXME DByteArray::class + val listOfNullable = dynamicSample.get>( property.name, - ByteArray::class ) assertContentEquals(value, listOfNullable[0]) assertEquals(null, listOfNullable[1]) @@ -509,7 +494,7 @@ class DynamicMutableRealmObjectTests { // Test we can set null ... dynamicSample.set(name, realmListOf(null)) - dynamicSample.getNullableValueList(name) + dynamicSample.get>(name) .also { list -> assertEquals(1, list.size) assertEquals(null, list[0]) @@ -518,7 +503,7 @@ class DynamicMutableRealmObjectTests { // ... and primitives... val value = RealmAny.create(42) dynamicSample.set(name, realmListOf(value)) - dynamicSample.getNullableValueList(name) + dynamicSample.get>(name) .also { list -> assertEquals(1, list.size) assertEquals(value, list[0]) @@ -533,13 +518,13 @@ class DynamicMutableRealmObjectTests { RealmAny.create(dynamicMutableUnmanagedObject) dynamicSample.set(name, realmListOf(dynamicRealmAny)) val expectedValue = - dynamicMutableUnmanagedObject.getValue("stringField") + dynamicMutableUnmanagedObject.get("stringField") val actualList = - dynamicSample.getNullableValueList(name) + dynamicSample.get>(name) assertEquals(1, actualList.size) val actualValue = actualList[0] ?.asRealmObject() - ?.getValue("stringField") + ?.get("stringField") assertEquals(expectedValue, actualValue) } @@ -554,46 +539,31 @@ class DynamicMutableRealmObjectTests { RealmAny.create(dynamicMutableManagedObject) dynamicSample.set(name, realmListOf(dynamicRealmAny)) val expectedValue = - dynamicMutableManagedObject.getValue("stringField") + dynamicMutableManagedObject.get("stringField") val actualList = - dynamicSample.getNullableValueList(name) + dynamicSample.get>(name) assertEquals(1, actualList.size) val managedDynamicMutableObject = actualList[0] ?.asRealmObject() val actualValue = - managedDynamicMutableObject?.getValue("stringField") + managedDynamicMutableObject?.get("stringField") assertEquals(expectedValue, actualValue) // Check we did indeed get a dynamic mutable object managedDynamicMutableObject?.set("stringField", "NEW") assertEquals( "NEW", - managedDynamicMutableObject?.getValue("stringField") + managedDynamicMutableObject?.get("stringField") ) } } else -> error("Model contains untested properties: $property") } } else { - fun assertionsForValue( - listFromSample: RealmList, - property: RealmProperty, - value: T, - clazz: KClass<*> - ) { - listFromSample.add(value) - val valueList = dynamicSample.getValueList( - property.name, - clazz - ) - assertEquals(1, valueList.size) - @Suppress("UNCHECKED_CAST") - assertEquals(value, valueList[0] as T) - } - when (type.storageType) { RealmStorageType.BOOL -> assertionsForValue( - dynamicSample.getValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, true, Boolean::class @@ -608,50 +578,58 @@ class DynamicMutableRealmObjectTests { else -> error("Unexpected integral field ${property.name}") } assertionsForValue( - dynamicSample.getValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, value, Long::class ) } RealmStorageType.STRING -> assertionsForValue( - dynamicSample.getValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, "NEW_ELEMENT", String::class ) RealmStorageType.FLOAT -> assertionsForValue( - dynamicSample.getValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234f, Float::class ) RealmStorageType.DOUBLE -> assertionsForValue( - dynamicSample.getValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234, Double::class ) RealmStorageType.DECIMAL128 -> assertionsForValue( - dynamicSample.getValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, Decimal128("1.84467440731231618E-615"), BsonDecimal128::class ) RealmStorageType.TIMESTAMP -> assertionsForValue( - dynamicSample.getValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmInstant.from(100, 100), RealmInstant::class ) RealmStorageType.OBJECT_ID -> assertionsForValue( - dynamicSample.getValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, BsonObjectId(), BsonObjectId::class ) RealmStorageType.UUID -> assertionsForValue( - dynamicSample.getValueList(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmUUID.random(), RealmUUID::class @@ -659,24 +637,24 @@ class DynamicMutableRealmObjectTests { RealmStorageType.BINARY -> { // TODO use assertionsForValue when we add support for structural equality for RealmList val value = byteArrayOf(42) - dynamicSample.getValueList(property.name).add(value) + dynamicSample.get>(property.name).add(value) assertContentEquals( value, - dynamicSample.getValueList(property.name, ByteArray::class)[0] + dynamicSample.get>(property.name)[0] ) } RealmStorageType.OBJECT -> { val value = dynamicMutableRealm.copyToRealm( DynamicMutableRealmObject.create("Sample") ).set("stringField", "NEW_OBJECT") - dynamicSample.getValueList(property.name) + dynamicSample.get>(property.name) .add(value) assertEquals( "NEW_OBJECT", - dynamicSample.getValueList( + dynamicSample.get>( property.name, - DynamicRealmObject::class - )[0].getValue("stringField") +// DynamicRealmObject::class + )[0].get("stringField") ) } else -> error("Model contains untested properties: $property") @@ -685,26 +663,10 @@ class DynamicMutableRealmObjectTests { } is SetPropertyType -> { if (type.isNullable) { - fun assertionsForNullable( - setFromSample: RealmSet, - property: RealmProperty, - value: T, - clazz: KClass<*> - ) { - setFromSample.add(value) - setFromSample.add(null) - val setOfNullable = dynamicSample.getNullableValueSet( - property.name, - clazz - ) - assertEquals(2, setOfNullable.size) - assertTrue(setOfNullable.contains(value as Any?)) - assertTrue(setOfNullable.contains(null)) - } - when (type.storageType) { RealmStorageType.BOOL -> assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, true, Boolean::class @@ -719,56 +681,65 @@ class DynamicMutableRealmObjectTests { else -> error("Unexpected integral field ${property.name}") } assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, value, Long::class ) } - RealmStorageType.STRING -> assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + RealmStorageType.STRING -> assertionsForNullable( + dynamicSample, + dynamicSample.get>(property.name), property, "NEW_ELEMENT", String::class ) RealmStorageType.FLOAT -> assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234f, Float::class ) RealmStorageType.DOUBLE -> assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234, Double::class ) RealmStorageType.TIMESTAMP -> assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmInstant.from(100, 100), RealmInstant::class ) RealmStorageType.OBJECT_ID -> assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, BsonObjectId(), BsonObjectId::class ) RealmStorageType.UUID -> assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmUUID.random(), RealmUUID::class ) RealmStorageType.BINARY -> assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, byteArrayOf(42), ByteArray::class ) RealmStorageType.DECIMAL128 -> assertionsForNullable( - dynamicSample.getNullableValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, Decimal128("1.84467440731231618E-615"), Decimal128::class @@ -785,7 +756,7 @@ class DynamicMutableRealmObjectTests { // Test we can set null ... dynamicSample.set(name, realmSetOf(null)) - dynamicSample.getNullableValueSet(name) + dynamicSample.get>(name) .also { set -> assertEquals(1, set.size) assertEquals(null, set.iterator().next()) @@ -794,7 +765,7 @@ class DynamicMutableRealmObjectTests { // ... and primitives... val value = RealmAny.create(42) dynamicSample.set(name, realmSetOf(value)) - dynamicSample.getNullableValueSet(name) + dynamicSample.get>(name) .also { set -> assertEquals(1, set.size) assertEquals(value, set.iterator().next()) @@ -809,13 +780,13 @@ class DynamicMutableRealmObjectTests { RealmAny.create(dynamicMutableUnmanagedObject) dynamicSample.set(name, realmSetOf(dynamicRealmAny)) val expectedValue = - dynamicMutableUnmanagedObject.getValue("stringField") + dynamicMutableUnmanagedObject.get("stringField") val actualSet = - dynamicSample.getNullableValueSet(name) + dynamicSample.get>(name) assertEquals(1, actualSet.size) val actualValue = actualSet.iterator().next() ?.asRealmObject() - ?.getValue("stringField") + ?.get("stringField") assertEquals(expectedValue, actualValue) } @@ -830,42 +801,32 @@ class DynamicMutableRealmObjectTests { RealmAny.create(dynamicMutableManagedObject) dynamicSample.set(name, realmSetOf(dynamicRealmAny)) val expectedValue = - dynamicMutableManagedObject.getValue("stringField") + dynamicMutableManagedObject.get("stringField") val actualSet = - dynamicSample.getNullableValueSet(name) + dynamicSample.get>(name) assertEquals(1, actualSet.size) val managedDynamicMutableObject = actualSet.iterator().next() ?.asRealmObject() val actualValue = - managedDynamicMutableObject?.getValue("stringField") + managedDynamicMutableObject?.get("stringField") assertEquals(expectedValue, actualValue) // Check we did indeed get a dynamic mutable object managedDynamicMutableObject?.set("stringField", "NEW") assertEquals( "NEW", - managedDynamicMutableObject?.getValue("stringField") + managedDynamicMutableObject?.get("stringField") ) } } else -> error("Model contains untested properties: $property") } } else { - fun assertionsForValue( - setFromSample: RealmSet, - property: RealmProperty, - value: T, - clazz: KClass<*> - ) { - setFromSample.add(value) - val setOfValue = dynamicSample.getValueSet(property.name, clazz) - assertEquals(1, setOfValue.size) - assertTrue(setOfValue.contains(value as Any)) - } when (type.storageType) { RealmStorageType.BOOL -> assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, true, Boolean::class @@ -880,56 +841,65 @@ class DynamicMutableRealmObjectTests { else -> error("Unexpected integral field ${property.name}") } assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, value, Long::class ) } RealmStorageType.STRING -> assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, "NEW_ELEMENT", String::class ) RealmStorageType.FLOAT -> assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234f, Float::class ) RealmStorageType.DOUBLE -> assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234, Double::class ) RealmStorageType.TIMESTAMP -> assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmInstant.from(100, 100), RealmInstant::class ) RealmStorageType.OBJECT_ID -> assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, BsonObjectId(), BsonObjectId::class ) RealmStorageType.UUID -> assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmUUID.random(), RealmUUID::class ) RealmStorageType.BINARY -> assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, byteArrayOf(42), ByteArray::class ) RealmStorageType.DECIMAL128 -> assertionsForValue( - dynamicSample.getValueSet(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, Decimal128("1.84467440731231618E-615"), BsonDecimal128::class @@ -938,14 +908,14 @@ class DynamicMutableRealmObjectTests { val value = dynamicMutableRealm.copyToRealm( DynamicMutableRealmObject.create("Sample") ).set("stringField", "NEW_OBJECT") - dynamicSample.getValueSet(property.name) + dynamicSample.get>(property.name) .add(value) // Loop through the set to find the element as indices aren't available var found = false - dynamicSample.getValueSet(property.name, DynamicRealmObject::class) + dynamicSample.get>(property.name) .forEach { - if (it.getValue("stringField") == "NEW_OBJECT") { + if (it.get("stringField") == "NEW_OBJECT") { found = true return@forEach } @@ -958,37 +928,10 @@ class DynamicMutableRealmObjectTests { } is MapPropertyType -> { if (type.isNullable) { - fun assertionsForNullable( - dictionaryFromSample: RealmDictionary, - property: RealmProperty, - value: T, - clazz: KClass<*> - ) { - dictionaryFromSample["A"] = value - dictionaryFromSample["B"] = null - val dictionaryOfNullable = dynamicSample.getNullableValueDictionary( - property.name, - clazz - ) - assertEquals(2, dictionaryOfNullable.size) - assertTrue(dictionaryOfNullable.containsKey("A")) - assertTrue(dictionaryOfNullable.containsKey("B")) - assertFalse(dictionaryOfNullable.containsKey("C")) - assertTrue(dictionaryOfNullable.containsValue(value as Any?)) - assertTrue(dictionaryOfNullable.containsValue(null)) - dictionaryOfNullable.entries.also { entries -> - assertTrue( - entries.contains(realmDictionaryEntryOf("A" to value as Any?)) - ) - assertTrue( - entries.contains(realmDictionaryEntryOf("B" to null)) - ) - } - } - when (type.storageType) { RealmStorageType.BOOL -> assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, true, Boolean::class @@ -1003,20 +946,23 @@ class DynamicMutableRealmObjectTests { else -> error("Unexpected integral field ${property.name}") } assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, value, Long::class ) } RealmStorageType.STRING -> assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, "NEW_ELEMENT", String::class ) RealmStorageType.BINARY -> assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, byteArrayOf(42), ByteArray::class @@ -1025,19 +971,18 @@ class DynamicMutableRealmObjectTests { val value = dynamicMutableRealm.copyToRealm( DynamicMutableRealmObject.create("Sample") ).set("stringField", "NEW_OBJECT") - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name )["A"] = value - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name )["B"] = null val nullableObjDictionary = - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name, - DynamicRealmObject::class ) assertEquals(2, nullableObjDictionary.size) assertTrue(nullableObjDictionary.containsKey("A")) @@ -1047,42 +992,48 @@ class DynamicMutableRealmObjectTests { assertNotNull(obj) assertEquals( "NEW_OBJECT", - obj.getValue("stringField") + obj.get("stringField") ) } } RealmStorageType.FLOAT -> assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234f, Float::class ) RealmStorageType.DOUBLE -> assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234, Double::class ) RealmStorageType.DECIMAL128 -> assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, Decimal128("1.84467440731231618E-615"), Decimal128::class ) RealmStorageType.TIMESTAMP -> assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmInstant.from(100, 100), RealmInstant::class ) RealmStorageType.OBJECT_ID -> assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, BsonObjectId(), BsonObjectId::class ) RealmStorageType.UUID -> assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmUUID.random(), RealmUUID::class @@ -1102,7 +1053,7 @@ class DynamicMutableRealmObjectTests { // Test we can set null ... dynamicSample.set(name, realmDictionaryOf("A" to null)) - dynamicSample.getNullableValueDictionary(name) + dynamicSample.get>(name) .also { dictionary -> assertEquals(1, dictionary.size) assertNull(dictionary["A"]) @@ -1111,7 +1062,7 @@ class DynamicMutableRealmObjectTests { // ... and primitives... val value = RealmAny.create(42) dynamicSample.set(name, realmDictionaryOf("A" to value)) - dynamicSample.getNullableValueDictionary(name) + dynamicSample.get>(name) .also { dictionary -> assertEquals(1, dictionary.size) assertEquals(value, dictionary["A"]) @@ -1129,13 +1080,13 @@ class DynamicMutableRealmObjectTests { realmDictionaryOf("A" to dynamicRealmAny) ) val expectedValue = - dynamicMutableUnmanagedObject.getValue("stringField") + dynamicMutableUnmanagedObject.get("stringField") val actualDictionary = - dynamicSample.getNullableValueDictionary(name) + dynamicSample.get>(name) assertEquals(1, actualDictionary.size) val actualValue = actualDictionary["A"] ?.asRealmObject() - ?.getValue("stringField") + ?.get("stringField") assertEquals(expectedValue, actualValue) } @@ -1153,50 +1104,32 @@ class DynamicMutableRealmObjectTests { realmDictionaryOf("A" to dynamicRealmAny) ) val expectedValue = - dynamicMutableManagedObject.getValue("stringField") + dynamicMutableManagedObject.get("stringField") val actualDictionary = - dynamicSample.getNullableValueDictionary(name) + dynamicSample.get>(name) assertEquals(1, actualDictionary.size) val managedDynamicMutableObject = actualDictionary["A"] ?.asRealmObject() val actualValue = - managedDynamicMutableObject?.getValue("stringField") + managedDynamicMutableObject?.get("stringField") assertEquals(expectedValue, actualValue) // Check we did indeed get a dynamic mutable object managedDynamicMutableObject?.set("stringField", "NEW") assertEquals( "NEW", - managedDynamicMutableObject?.getValue("stringField") + managedDynamicMutableObject?.get("stringField") ) } } else -> error("Model contains untested properties: $property") } } else { - fun assertionsForValue( - dictionaryFromSample: RealmDictionary, - property: RealmProperty, - value: T, - clazz: KClass<*> - ) { - dictionaryFromSample["A"] = value - val dictionaryOfValue = dynamicSample.getValueDictionary( - property.name, - clazz - ) - assertEquals(1, dictionaryOfValue.size) - assertTrue(dictionaryOfValue.containsKey("A")) - assertFalse(dictionaryOfValue.containsKey("B")) - assertTrue(dictionaryOfValue.containsValue(value as Any)) - assertTrue( - dictionaryOfValue.entries - .contains(realmDictionaryEntryOf("A" to value as Any)) - ) - } + when (type.storageType) { RealmStorageType.BOOL -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, true, Boolean::class @@ -1211,20 +1144,23 @@ class DynamicMutableRealmObjectTests { else -> error("Unexpected integral field ${property.name}") } assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, value, Long::class ) } RealmStorageType.STRING -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, "NEW_ELEMENT", String::class ) RealmStorageType.BINARY -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, byteArrayOf(42), ByteArray::class @@ -1233,12 +1169,11 @@ class DynamicMutableRealmObjectTests { val value = dynamicMutableRealm.copyToRealm( DynamicMutableRealmObject.create("Sample") ).set("stringField", "NEW_OBJECT") - dynamicSample.getValueDictionary(property.name)["A"] = + dynamicSample.get>(property.name)["A"] = value - val objDictionary = dynamicSample.getValueDictionary( + val objDictionary = dynamicSample.get>( property.name, - DynamicRealmObject::class ) assertEquals(1, objDictionary.size) assertTrue(objDictionary.containsKey("A")) @@ -1246,41 +1181,47 @@ class DynamicMutableRealmObjectTests { val objFromDictionary = assertNotNull(objDictionary["A"]) assertEquals( "NEW_OBJECT", - objFromDictionary.getValue("stringField") + objFromDictionary.get("stringField") ) } RealmStorageType.FLOAT -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234F, Float::class ) RealmStorageType.DOUBLE -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, 1.234, Double::class ) RealmStorageType.DECIMAL128 -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, Decimal128("1.84467440731231618E-615"), Decimal128::class ) RealmStorageType.TIMESTAMP -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmInstant.from(100, 100), RealmInstant::class ) RealmStorageType.OBJECT_ID -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, BsonObjectId(), BsonObjectId::class ) RealmStorageType.UUID -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample, + dynamicSample.get>(property.name), property, RealmUUID.random(), RealmUUID::class @@ -1349,20 +1290,20 @@ class DynamicMutableRealmObjectTests { ) ) ).let { - val list = it.getNullableValue("nullableRealmAnyField")!!.asList() + val list = it.get("nullableRealmAnyField")!!.asList() // Verify that we get mutable instances out of the collections list[0]!!.asList().let { embeddedList -> val o = embeddedList.first()!! .asRealmObject() assertIs(o) - assertEquals("INNER_LIST", o.getValue("stringField")) + assertEquals("INNER_LIST", o.get("stringField")) embeddedList.add(RealmAny.Companion.create(dynamicSampleInner)) } list[1]!!.asDictionary().let { embeddedDictionary -> val o = embeddedDictionary["key"]!! .asRealmObject() assertIs(o) - assertEquals("INNER_DICT", o.getValue("stringField")) + assertEquals("INNER_DICT", o.get("stringField")) embeddedDictionary.put("UPDATE", RealmAny.Companion.create(dynamicSampleInner)) } } @@ -1406,20 +1347,20 @@ class DynamicMutableRealmObjectTests { ) ) ).let { - val dict = it.getNullableValue("nullableRealmAnyField")!!.asDictionary() + val dict = it.get("nullableRealmAnyField")!!.asDictionary() // Verify that we get mutable instances out of the collections dict["list"]!!.asList().let { embeddedList -> val o = embeddedList.first()!! .asRealmObject() assertIs(o) - assertEquals("INNER_LIST", o.getValue("stringField")) + assertEquals("INNER_LIST", o.get("stringField")) embeddedList.add(RealmAny.Companion.create(dynamicSampleInner)) } dict["dict"]!!.asDictionary().let { embeddedDictionary -> val o = embeddedDictionary["key"]!! .asRealmObject() assertIs(o) - assertEquals("INNER_DICT", o.getValue("stringField")) + assertEquals("INNER_DICT", o.get("stringField")) embeddedDictionary.put("UPDATE", RealmAny.Companion.create(dynamicSampleInner)) } } @@ -1434,7 +1375,7 @@ class DynamicMutableRealmObjectTests { .find() .single() .run { - assertEquals("child1", getObject("child")!!.getNullableValue("id")) + assertEquals("child1", get("child")!!.get("id")) } } @@ -1444,14 +1385,14 @@ class DynamicMutableRealmObjectTests { dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("EmbeddedParent")) parent.set("child", DynamicMutableRealmObject.create("EmbeddedChild", "id" to "child1")) dynamicMutableRealm.query("EmbeddedParent").find().single().run { - assertEquals("child1", getObject("child")!!.getNullableValue("id")) + assertEquals("child1", get("child")!!.get("id")) parent.set("child", DynamicMutableRealmObject.create("EmbeddedChild", "id" to "child2")) } dynamicMutableRealm.query("EmbeddedChild") .find() .single() .run { - assertEquals("child2", getNullableValue("id")) + assertEquals("child2", get("id")) } } @@ -1491,7 +1432,7 @@ class DynamicMutableRealmObjectTests { ) ) o.set("primaryKey", "UPDATED_PRIMARY_KEY") - assertEquals("UPDATED_PRIMARY_KEY", o.getValue("primaryKey")) + assertEquals("UPDATED_PRIMARY_KEY", o.get("primaryKey")) } @Test @@ -1514,7 +1455,7 @@ class DynamicMutableRealmObjectTests { .find() .single() .run { - assertEquals("INIT", getNullableValue("name")) + assertEquals("INIT", get("name")) } dynamicMutableRealm.run { @@ -1537,7 +1478,7 @@ class DynamicMutableRealmObjectTests { .find() .single() .run { - assertEquals("UPDATED", getNullableValue("name")) + assertEquals("UPDATED", get("name")) } } @@ -1547,9 +1488,9 @@ class DynamicMutableRealmObjectTests { @Test fun list_add_embeddedRealmObject() { - val parent = + val parent: DynamicMutableRealmObject = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("EmbeddedParent")) - parent.getObjectList("childrenList").add( + parent.get>("childrenList").add( DynamicMutableRealmObject.create( "EmbeddedChild", "subTree" to DynamicMutableRealmObject.create("EmbeddedParent", "id" to "subParent") @@ -1560,7 +1501,7 @@ class DynamicMutableRealmObjectTests { .find() .single() .run { - assertEquals("subParent", getObject("subTree")!!.getNullableValue("id")) + assertEquals("subParent", get("subTree")!!.get("id")) } } @@ -1577,7 +1518,7 @@ class DynamicMutableRealmObjectTests { "objectListField" to realmListOf(child, child) ) val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("Sample")) - parent.getObjectList("objectListField") + parent.get>("objectListField") .add(intermediate) dynamicMutableRealm.query("Sample") @@ -1600,7 +1541,7 @@ class DynamicMutableRealmObjectTests { "objectListField" to realmListOf(child, child) ) val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("Sample")) - parent.getObjectList("objectListField") + parent.get>("objectListField") .add(0, intermediate) dynamicMutableRealm.query("Sample").find().run { @@ -1621,13 +1562,13 @@ class DynamicMutableRealmObjectTests { "EmbeddedChild", "subTree" to DynamicMutableRealmObject.create("EmbeddedParent", "id" to "subParent") ) - parent.getObjectList("childrenList") + parent.get>("childrenList") .addAll(listOf(child, child)) dynamicMutableRealm.query("EmbeddedChild").find().run { assertEquals(2, size) - assertEquals("subParent", get(0).getObject("subTree")!!.getNullableValue("id")) - assertEquals("subParent", get(1).getObject("subTree")!!.getNullableValue("id")) + assertEquals("subParent", get(0).get("subTree")!!.get("id")) + assertEquals("subParent", get(1).get("subTree")!!.get("id")) } } @@ -1644,7 +1585,7 @@ class DynamicMutableRealmObjectTests { "objectListField" to realmListOf(child, child) ) val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("Sample")) - parent.getObjectList("objectListField") + parent.get>("objectListField") .addAll(listOf(intermediate, intermediate)) dynamicMutableRealm.query("Sample").find().run { @@ -1665,7 +1606,7 @@ class DynamicMutableRealmObjectTests { "objectListField" to realmListOf(child, child) ) val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("Sample")) - parent.getObjectList("objectListField").addAll(0, listOf(intermediate, intermediate)) + parent.get>("objectListField").addAll(0, listOf(intermediate, intermediate)) dynamicMutableRealm.query("Sample").find().run { assertEquals(3, size) @@ -1689,7 +1630,7 @@ class DynamicMutableRealmObjectTests { "objectListField" to realmListOf(child2, child2) ) val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("Sample")) - parent.getObjectList("objectListField").run { + parent.get>("objectListField").run { add(child1) set(0, intermediate) } @@ -1715,7 +1656,7 @@ class DynamicMutableRealmObjectTests { "objectSetField" to realmSetOf(child, child) ) val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("Sample")) - parent.getObjectSet("objectSetField") + parent.get>("objectSetField") .add(intermediate) dynamicMutableRealm.query("Sample") @@ -1738,7 +1679,7 @@ class DynamicMutableRealmObjectTests { "objectSetField" to realmSetOf(child, child) ) val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("Sample")) - parent.getObjectSet("objectSetField") + parent.get>("objectSetField") .addAll(setOf(intermediate, intermediate)) dynamicMutableRealm.query("Sample") @@ -1756,7 +1697,7 @@ class DynamicMutableRealmObjectTests { fun dictionary_put_embeddedRealmObject() { val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("EmbeddedParent")) - parent.getObjectDictionary("childrenDictionary")["A"] = + parent.get>("childrenDictionary")["A"] = DynamicMutableRealmObject.create( "EmbeddedChild", "subTree" to DynamicMutableRealmObject.create("EmbeddedParent", "id" to "subParent") @@ -1766,7 +1707,7 @@ class DynamicMutableRealmObjectTests { .find() .single() .run { - assertEquals("subParent", getObject("subTree")!!.getNullableValue("id")) + assertEquals("subParent", get("subTree")!!.get("id")) } } @@ -1782,13 +1723,13 @@ class DynamicMutableRealmObjectTests { "EmbeddedChild", "subTree" to DynamicMutableRealmObject.create("EmbeddedParent", "id" to "subParent") ) - parent.getObjectDictionary("childrenDictionary") + parent.get>("childrenDictionary") .putAll(listOf("A" to child, "B" to child)) dynamicMutableRealm.query("EmbeddedChild").find().run { assertEquals(2, size) - assertEquals("subParent", get(0).getObject("subTree")!!.getNullableValue("id")) - assertEquals("subParent", get(1).getObject("subTree")!!.getNullableValue("id")) + assertEquals("subParent", get(0).get("subTree")!!.get("id")) + assertEquals("subParent", get(1).get("subTree")!!.get("id")) } } @@ -1812,7 +1753,7 @@ class DynamicMutableRealmObjectTests { ) ) val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("Sample")) - parent.getObjectDictionary("nullableObjectDictionaryFieldNotNull").run { + parent.get>("nullableObjectDictionaryFieldNotNull").run { put("A", child1) put("B", intermediate) } @@ -1841,7 +1782,7 @@ class DynamicMutableRealmObjectTests { ) ) val parent = dynamicMutableRealm.copyToRealm(DynamicMutableRealmObject.create("Sample")) - parent.getObjectDictionary("nullableObjectDictionaryFieldNotNull").run { + parent.get>("nullableObjectDictionaryFieldNotNull").run { putAll(listOf("A" to child1, "B" to intermediate)) } dynamicMutableRealm.query("Sample").find().run { @@ -1867,3 +1808,114 @@ class DynamicMutableRealmObjectTests { } } } + +inline fun assertionsForValue( + dynamicRealmObject: DynamicRealmObject, + setFromSample: RealmSet, + property: RealmProperty, + value: T, + clazz: KClass +) { + setFromSample.add(value) + val setOfValue = dynamicRealmObject.get>(property.name) + assertEquals(1, setOfValue.size) + assertTrue(setOfValue.contains(value)) +} + +inline fun assertionsForNullable( + dynamicRealmObject: DynamicRealmObject, + listFromSample: RealmList, + property: RealmProperty, + value: T, + clazz: KClass, +) { + listFromSample.add(value) + listFromSample.add(null) + val listOfNullable: RealmList = dynamicRealmObject.get>( + property.name, + ) + assertEquals(2, listOfNullable.size) + assertEquals(value, listOfNullable[0]) + assertNull(listOfNullable[1]) +} + +inline fun assertionsForValue( + dynamicRealmObject: DynamicRealmObject, + listFromSample: RealmList, + property: RealmProperty, + value: T, + clazz: KClass +) { + listFromSample.add(value) + val valueList = dynamicRealmObject.get>( + property.name, + ) + assertEquals(1, valueList.size) + @Suppress("UNCHECKED_CAST") + assertEquals(value, valueList[0] as T) +} +inline fun assertionsForNullable( + dynamicRealmObject: DynamicRealmObject, + setFromSample: RealmSet, + property: RealmProperty, + value: T, + clazz: KClass +) { + setFromSample.add(value) + setFromSample.add(null) + val setOfNullable = dynamicRealmObject.get>( + property.name, +// clazz + ) + assertEquals(2, setOfNullable.size) + assertTrue(setOfNullable.contains(value as Any?)) + assertTrue(setOfNullable.contains(null)) +} +inline fun assertionsForValue( + dynamicRealmObject: DynamicRealmObject, + dictionaryFromSample: RealmDictionary, + property: RealmProperty, + value: T, + clazz: KClass +) { + dictionaryFromSample["A"] = value + val dictionaryOfValue = dynamicRealmObject.get>( + property.name, + ) + assertEquals(1, dictionaryOfValue.size) + assertTrue(dictionaryOfValue.containsKey("A")) + assertFalse(dictionaryOfValue.containsKey("B")) + assertTrue(dictionaryOfValue.containsValue(value as T)) + assertTrue( + dictionaryOfValue.entries + .contains(realmDictionaryEntryOf("A" to value as T)) + ) +} + +inline fun assertionsForNullable( + dynamicRealmObject: DynamicRealmObject, + dictionaryFromSample: RealmDictionary, + property: RealmProperty, + value: T, + clazz: KClass +) { + dictionaryFromSample["A"] = value + dictionaryFromSample["B"] = null + val dictionaryOfNullable = dynamicRealmObject.get>( + property.name, + ) + assertEquals(2, dictionaryOfNullable.size) + assertTrue(dictionaryOfNullable.containsKey("A")) + assertTrue(dictionaryOfNullable.containsKey("B")) + assertFalse(dictionaryOfNullable.containsKey("C")) + assertTrue(dictionaryOfNullable.containsValue(value as Any?)) + assertTrue(dictionaryOfNullable.containsValue(null)) + dictionaryOfNullable.entries.also { entries: MutableSet> -> + assertTrue( + entries.contains(realmDictionaryEntryOf("A" to value as T?)) + ) + assertTrue( + entries.contains(realmDictionaryEntryOf("B" to null)) + ) + } +} diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicMutableRealmTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicMutableRealmTests.kt index e37b94d9a9..15111f4aed 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicMutableRealmTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicMutableRealmTests.kt @@ -21,10 +21,8 @@ import io.realm.kotlin.RealmConfiguration import io.realm.kotlin.UpdatePolicy import io.realm.kotlin.dynamic.DynamicMutableRealm import io.realm.kotlin.dynamic.DynamicMutableRealmObject -import io.realm.kotlin.dynamic.getNullableValue -import io.realm.kotlin.dynamic.getValue -import io.realm.kotlin.dynamic.getValueList -import io.realm.kotlin.dynamic.getValueSet +import io.realm.kotlin.dynamic.DynamicRealmObject +import io.realm.kotlin.dynamic.get import io.realm.kotlin.entities.Sample import io.realm.kotlin.entities.SampleWithPrimaryKey import io.realm.kotlin.entities.embedded.embeddedSchema @@ -41,6 +39,8 @@ import io.realm.kotlin.query.RealmSingleQuery import io.realm.kotlin.test.StandaloneDynamicMutableRealm import io.realm.kotlin.test.common.utils.assertFailsWithMessage import io.realm.kotlin.test.platform.PlatformUtils +import io.realm.kotlin.types.RealmList +import io.realm.kotlin.types.RealmSet import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test @@ -110,7 +110,7 @@ class DynamicMutableRealmTests { ) ) assertTrue { dynamicMutableObject.isValid() } - assertEquals("PRIMARY_KEY", dynamicMutableObject.getValue("primaryKey")) + assertEquals("PRIMARY_KEY", dynamicMutableObject.get("primaryKey")) } // TODO Add variants for each type @@ -123,7 +123,7 @@ class DynamicMutableRealmTests { ) ) assertTrue { dynamicMutableObject.isValid() } - assertNull(dynamicMutableObject.getNullableValue("primaryKey")) + assertNull(dynamicMutableObject.get("primaryKey")) } @Test @@ -153,26 +153,26 @@ class DynamicMutableRealmTests { ) dynamicMutableRealm.query("EmbeddedParent", "id = 'PARENT'").find().single().let { parent -> - parent.getObject("child").let { child -> - assertEquals("CHILD", child!!.getNullableValue("id")) - child.getObject("innerChild").let { innerChild -> - assertEquals("INNER", innerChild!!.getNullableValue("id")) + parent.get("child").let { child -> + assertEquals("CHILD", child!!.get("id")) + child.get("innerChild").let { innerChild -> + assertEquals("INNER", innerChild!!.get("id")) } - child.getObject("subTree")!!.run { - assertEquals("SUBTREE_PARENT", getNullableValue("id")) + child.get("subTree")!!.run { + assertEquals("SUBTREE_PARENT", get("id")) } } - parent.getObjectList("childrenList").forEach { child -> - assertEquals("CHILD", child.getNullableValue("id")) - child.getObject("innerChild").let { innerChild -> - assertEquals("INNER", innerChild!!.getNullableValue("id")) + parent.get>("childrenList").forEach { child -> + assertEquals("CHILD", child.get("id")) + child.get("innerChild").let { innerChild -> + assertEquals("INNER", innerChild!!.get("id")) } } } dynamicMutableRealm.query("EmbeddedParent", "id = 'SUBTREE_PARENT'").find().single().run { - assertEquals("SUBTREE_PARENT", getNullableValue("id")) - getObject("child").let { child -> - assertEquals("SUBTREE_CHILD", child!!.getNullableValue("id")) + assertEquals("SUBTREE_PARENT", get("id")) + get("child").let { child -> + assertEquals("SUBTREE_CHILD", child!!.get("id")) } } dynamicMutableRealm.query("EmbeddedChild", "id = 'CHILD'").find().run { @@ -205,8 +205,8 @@ class DynamicMutableRealmTests { ) ) dynamicMutableRealm.query("Sample", "stringField = 'PARENT'").find().single().run { - getObject("nullableObject")!!.run { - assertEquals("CHILD", getValue("stringField")) + get("nullableObject")!!.run { + assertEquals("CHILD", get("stringField")) } } } @@ -222,8 +222,8 @@ class DynamicMutableRealmTests { ) ) dynamicMutableRealm.query("Sample", "stringField = 'PARENT'").find().single().run { - getObject("nullableObject")!!.run { - assertEquals("CHILD", getValue("stringField")) + get("nullableObject")!!.run { + assertEquals("CHILD", get("stringField")) } } } @@ -250,7 +250,7 @@ class DynamicMutableRealmTests { dynamicMutableRealm.query("SampleWithPrimaryKey").find().run { assertEquals(2, size) - forEach { assertEquals("UPDATED_VALUE", it.getValue("stringField")) } + forEach { assertEquals("UPDATED_VALUE", it.get("stringField")) } } } @@ -355,10 +355,10 @@ class DynamicMutableRealmTests { ) dynamicMutableRealm.copyToRealm(obj) dynamicMutableRealm.query("EmbeddedParent").find().single().run { - getObjectList("childrenList").run { + get>("childrenList").run { assertEquals(2, size) - assertEquals("child1", get(0).getNullableValue("id")) - assertEquals("child2", get(1).getNullableValue("id")) + assertEquals("child1", get(0).get("id")) + assertEquals("child2", get(1).get("id")) } } dynamicMutableRealm.query("EmbeddedChild").find().run { @@ -384,16 +384,16 @@ class DynamicMutableRealmTests { dynamicMutableRealm.copyToRealm(parent) dynamicMutableRealm.query("EmbeddedParentWithPrimaryKey").find().single().let { parent -> - parent.getObject("child").let { child -> - assertEquals("CHILD", child!!.getNullableValue("id")) - child.getObject("innerChild").let { innerChild -> - assertEquals("INNER", innerChild!!.getNullableValue("id")) + parent.get("child").let { child -> + assertEquals("CHILD", child!!.get("id")) + child.get("innerChild").let { innerChild -> + assertEquals("INNER", innerChild!!.get("id")) } } - parent.getObjectList("childrenList").forEach { child -> - assertEquals("CHILD", child.getNullableValue("id")) - child.getObject("innerChild").let { innerChild -> - assertEquals("INNER", innerChild!!.getNullableValue("id")) + parent.get>("childrenList").forEach { child -> + assertEquals("CHILD", child.get("id")) + child.get("innerChild").let { innerChild -> + assertEquals("INNER", innerChild!!.get("id")) } } } @@ -410,16 +410,16 @@ class DynamicMutableRealmTests { dynamicMutableRealm.copyToRealm(parent, updatePolicy = UpdatePolicy.ALL) dynamicMutableRealm.query("EmbeddedParentWithPrimaryKey").find().single().let { parent -> - parent.getObject("child").let { child -> - assertEquals("UPDATED", child!!.getNullableValue("id")) - child.getObject("innerChild").let { innerChild -> - assertEquals("UPDATED", innerChild!!.getNullableValue("id")) + parent.get("child").let { child -> + assertEquals("UPDATED", child!!.get("id")) + child.get("innerChild").let { innerChild -> + assertEquals("UPDATED", innerChild!!.get("id")) } } - parent.getObjectList("childrenList").forEach { child -> - assertEquals("UPDATED", child.getNullableValue("id")) - child.getObject("innerChild").let { innerChild -> - assertEquals("UPDATED", innerChild!!.getNullableValue("id")) + parent.get>("childrenList").forEach { child -> + assertEquals("UPDATED", child.get("id")) + child.get("innerChild").let { innerChild -> + assertEquals("UPDATED", innerChild!!.get("id")) } } } @@ -452,7 +452,7 @@ class DynamicMutableRealmTests { val o2 = dynamicMutableRealm.findLatest(o1) assertNotNull(o2) - assertEquals("NEW_VALUE", o2.getValue("stringField")) + assertEquals("NEW_VALUE", o2.get("stringField")) } @Test @@ -509,24 +509,24 @@ class DynamicMutableRealmTests { dynamicMutableRealm.run { val liveObject = copyToRealm(DynamicMutableRealmObject.create("Sample")).apply { set("stringField", "PARENT") - getObjectList("objectListField").run { + get>("objectListField").run { add(DynamicMutableRealmObject.create("Sample")) add(DynamicMutableRealmObject.create("Sample")) add(DynamicMutableRealmObject.create("Sample")) } - getValueList("stringListField").run { + get>("stringListField").run { add("ELEMENT1") add("ELEMENT2") } } assertEquals(4, query("Sample").count().find()) - liveObject.getObjectList("objectListField").run { + liveObject.get>("objectListField").run { assertEquals(3, size) delete(this) assertEquals(0, size) } - liveObject.getValueList("stringListField").run { + liveObject.get>("stringListField").run { assertEquals(2, size) delete(this) assertEquals(0, size) @@ -540,24 +540,24 @@ class DynamicMutableRealmTests { dynamicMutableRealm.run { val liveObject = copyToRealm(DynamicMutableRealmObject.create("Sample")).apply { set("stringField", "PARENT") - getObjectSet("objectSetField").run { + get>("objectSetField").run { add(DynamicMutableRealmObject.create("Sample")) add(DynamicMutableRealmObject.create("Sample")) add(DynamicMutableRealmObject.create("Sample")) } - getValueSet("stringSetField").run { + get>("stringSetField").run { add("ELEMENT1") add("ELEMENT2") } } assertEquals(4, query("Sample").count().find()) - liveObject.getObjectSet("objectSetField").run { + liveObject.get>("objectSetField").run { assertEquals(3, size) delete(this) assertEquals(0, size) } - liveObject.getValueSet("stringSetField").run { + liveObject.get>("stringSetField").run { assertEquals(2, size) delete(this) assertEquals(0, size) @@ -579,7 +579,7 @@ class DynamicMutableRealmTests { val samples: RealmResults = query("Sample").find() assertEquals(5, samples.size) for (sample in samples) { - assertEquals(0, sample.getValue("intField")) + assertEquals(0, sample.get("intField")) } } } @@ -600,7 +600,7 @@ class DynamicMutableRealmTests { val samples: RealmResults = query("Sample").find() assertEquals(3, samples.size) for (sample in samples) { - assertNotEquals(1, sample.getValue("intField")) + assertNotEquals(1, sample.get("intField")) } } } @@ -618,7 +618,7 @@ class DynamicMutableRealmTests { val samples: RealmResults = query("Sample").find() assertEquals(5, samples.size) for (sample in samples) { - assertEquals(0, sample.getValue("intField")) + assertEquals(0, sample.get("intField")) } } } diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicRealmObjectTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicRealmObjectTests.kt index c8956a94f1..3c1efbaa96 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicRealmObjectTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicRealmObjectTests.kt @@ -21,14 +21,7 @@ package io.realm.kotlin.test.common.dynamic import io.realm.kotlin.Realm import io.realm.kotlin.RealmConfiguration import io.realm.kotlin.dynamic.DynamicRealmObject -import io.realm.kotlin.dynamic.getNullableValue -import io.realm.kotlin.dynamic.getNullableValueDictionary -import io.realm.kotlin.dynamic.getNullableValueList -import io.realm.kotlin.dynamic.getNullableValueSet -import io.realm.kotlin.dynamic.getValue -import io.realm.kotlin.dynamic.getValueDictionary -import io.realm.kotlin.dynamic.getValueList -import io.realm.kotlin.dynamic.getValueSet +import io.realm.kotlin.dynamic.get import io.realm.kotlin.entities.Sample import io.realm.kotlin.ext.asFlow import io.realm.kotlin.ext.asRealmObject @@ -118,13 +111,13 @@ class DynamicRealmObjectTests { assertEquals("Sample", first.type) // get string - val actual = first.getValue("stringField", String::class) + val actual = first.get("stringField", String::class) assertEquals("Parent", actual) // get object - val dynamicChild: DynamicRealmObject? = first.getObject("nullableObject") + val dynamicChild: DynamicRealmObject? = first.get("nullableObject") assertNotNull(dynamicChild) - assertEquals("Child", dynamicChild.getValue("stringField")) + assertEquals("Child", dynamicChild.get("stringField")) } @Test @@ -158,129 +151,129 @@ class DynamicRealmObjectTests { if (type.isNullable) { when (type.storageType) { RealmStorageType.BOOL -> { - assertEquals(null, dynamicSample.getNullableValue(name)) + assertEquals(null, dynamicSample.get(name)) assertEquals( null, - dynamicSample.getNullableValue(name, type.storageType.kClass) + dynamicSample.get(name, Boolean::class) ) } RealmStorageType.INT -> { assertEquals( null, - dynamicSample.getNullableValue(property.name) + dynamicSample.get(property.name) ) assertEquals( null, - dynamicSample.getNullableValue( + dynamicSample.get( property.name, - type.storageType.kClass + Long::class ) ) } RealmStorageType.STRING -> { assertEquals( null, - dynamicSample.getNullableValue(property.name) + dynamicSample.get(property.name) ) assertEquals( null, - dynamicSample.getNullableValue( + dynamicSample.get( property.name, - type.storageType.kClass + String::class ) ) } RealmStorageType.OBJECT -> { - assertEquals(null, dynamicSample.getObject(property.name)) + assertEquals(null, dynamicSample.get(property.name)) } RealmStorageType.FLOAT -> { assertEquals( null, - dynamicSample.getNullableValue(property.name) + dynamicSample.get(property.name) ) assertEquals( null, - dynamicSample.getNullableValue( + dynamicSample.get( property.name, - type.storageType.kClass + Float::class ) ) } RealmStorageType.DOUBLE -> { assertEquals( null, - dynamicSample.getNullableValue(property.name) + dynamicSample.get(property.name) ) assertEquals( null, - dynamicSample.getNullableValue( + dynamicSample.get( property.name, - type.storageType.kClass + Double::class ) ) } RealmStorageType.DECIMAL128 -> { assertEquals( null, - dynamicSample.getNullableValue(property.name) + dynamicSample.get(property.name) ) assertEquals( null, - dynamicSample.getNullableValue( + dynamicSample.get( property.name, - type.storageType.kClass + Decimal128::class ) ) } RealmStorageType.TIMESTAMP -> { assertEquals( null, - dynamicSample.getNullableValue(property.name) + dynamicSample.get(property.name) ) assertEquals( null, - dynamicSample.getNullableValue( + dynamicSample.get( property.name, - type.storageType.kClass + RealmInstant::class ) ) } RealmStorageType.OBJECT_ID -> { assertEquals( null, - dynamicSample.getNullableValue(property.name) + dynamicSample.get(property.name) ) assertEquals( null, - dynamicSample.getNullableValue( + dynamicSample.get( property.name, - type.storageType.kClass + BsonObjectId::class ) ) } RealmStorageType.UUID -> { assertEquals( null, - dynamicSample.getNullableValue(property.name) + dynamicSample.get(property.name) ) assertEquals( null, - dynamicSample.getNullableValue( + dynamicSample.get( property.name, - type.storageType.kClass + RealmUUID::class ) ) } RealmStorageType.BINARY -> { assertContentEquals( null, - dynamicSample.getNullableValue(property.name) + dynamicSample.get(property.name) ) assertContentEquals( null, - dynamicSample.getNullableValue( + dynamicSample.get( property.name, - type.storageType.kClass + ByteArray::class ) as ByteArray? ) } @@ -298,15 +291,15 @@ class DynamicRealmObjectTests { RealmStorageType.BOOL -> { assertEquals( expectedSample.booleanField, - dynamicSample.getValue(name) + dynamicSample.get(name) ) assertEquals( expectedSample.booleanField, - dynamicSample.getValue(name) + dynamicSample.get(name) ) assertEquals( expectedSample.booleanField, - dynamicSample.getValue(property.name, type.storageType.kClass) + dynamicSample.get(property.name, Boolean::class) ) } RealmStorageType.INT -> { @@ -319,132 +312,125 @@ class DynamicRealmObjectTests { "mutableRealmIntField" -> expectedSample.mutableRealmIntField.get() else -> error("Unexpected integral field ${property.name}") } - assertEquals(expectedValue, dynamicSample.getValue(property.name)) + assertEquals(expectedValue, dynamicSample.get(property.name)) assertEquals( expectedValue, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedValue, - dynamicSample.getValue(property.name, type.storageType.kClass) + dynamicSample.get(property.name, Long::class) ) } RealmStorageType.STRING -> { assertEquals( expectedSample.stringField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.stringField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.stringField, - dynamicSample.getValue(property.name, type.storageType.kClass) + dynamicSample.get(property.name, String::class) ) } RealmStorageType.FLOAT -> { assertEquals( expectedSample.floatField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.floatField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.floatField, - dynamicSample.getValue(property.name, type.storageType.kClass) + dynamicSample.get(property.name, Float::class) ) } RealmStorageType.DOUBLE -> { assertEquals( expectedSample.doubleField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.doubleField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.doubleField, - dynamicSample.getValue(property.name, type.storageType.kClass) + dynamicSample.get(property.name, Double::class) ) } RealmStorageType.DECIMAL128 -> { assertEquals( expectedSample.decimal128Field, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.decimal128Field, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.decimal128Field, - dynamicSample.getValue(property.name, type.storageType.kClass) + dynamicSample.get(property.name, Decimal128::class) ) } RealmStorageType.TIMESTAMP -> { assertEquals( expectedSample.timestampField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.timestampField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.timestampField, - dynamicSample.getValue(property.name, type.storageType.kClass) + dynamicSample.get(property.name, RealmInstant::class) ) } RealmStorageType.OBJECT_ID -> { assertEquals( expectedSample.bsonObjectIdField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.bsonObjectIdField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.bsonObjectIdField, - dynamicSample.getValue( + dynamicSample.get( property.name, - type.storageType.kClass + BsonObjectId::class ) ) } RealmStorageType.UUID -> { assertEquals( expectedSample.uuidField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.uuidField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertEquals( expectedSample.uuidField, - dynamicSample.getValue(property.name, type.storageType.kClass) + dynamicSample.get(property.name, RealmUUID::class) ) } RealmStorageType.BINARY -> { assertContentEquals( expectedSample.binaryField, - dynamicSample.getValue(property.name) + dynamicSample.get(property.name) ) assertContentEquals( expectedSample.binaryField, - dynamicSample.getValue(property.name) - ) - assertContentEquals( - expectedSample.binaryField, - dynamicSample.getValue( - property.name, - type.storageType.kClass - ) as ByteArray + dynamicSample.get(property.name, ByteArray::class) ) } else -> error("Model contains untested properties: $property") @@ -465,7 +451,7 @@ class DynamicRealmObjectTests { val expectedValue = defaultSample.stringField assertEquals( expectedValue, - linkingObjects.first().getValue("stringField") + linkingObjects.first().get("stringField") ) } else -> error("Model contains untested properties: $property") @@ -478,126 +464,126 @@ class DynamicRealmObjectTests { when (type.storageType) { RealmStorageType.BOOL -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - Boolean::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// Boolean::class +// ) +// ) } RealmStorageType.INT -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - Long::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// Long::class +// ) +// ) } RealmStorageType.STRING -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - String::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// String::class +// ) +// ) } RealmStorageType.FLOAT -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - Float::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// Float::class +// ) +// ) } RealmStorageType.DOUBLE -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - Double::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// Double::class +// ) +// ) } RealmStorageType.DECIMAL128 -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - Decimal128::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// Decimal128::class +// ) +// ) } RealmStorageType.TIMESTAMP -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - RealmInstant::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// RealmInstant::class +// ) +// ) } RealmStorageType.OBJECT_ID -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - BsonObjectId::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// BsonObjectId::class +// ) +// ) } RealmStorageType.UUID -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - RealmUUID::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// RealmUUID::class +// ) +// ) } RealmStorageType.BINARY -> { assertionsForNullable( - dynamicSample.getNullableValueList(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueList( - property.name, - ByteArray::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getList( +// property.name, +// ByteArray::class +// ) +// ) } RealmStorageType.OBJECT -> { assertEquals( null, - dynamicSample.getNullableValueList(property.name)[0] - ) - assertEquals( - null, - dynamicSample.getNullableValueList( - property.name, - DynamicRealmObject::class - )[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// null, +// dynamicSample.get>( +// property.name, +// DynamicRealmObject::class +// )[0] +// ) } RealmStorageType.ANY -> { // The testing pattern doesn't work for RealmAny since we only land @@ -615,12 +601,12 @@ class DynamicRealmObjectTests { val expectedValue = defaultSample.booleanField assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertEquals( - expectedValue, - dynamicSample.getValueList(property.name, Boolean::class)[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// expectedValue, +// dynamicSample.getList(property.name, Boolean::class)[0] +// ) } RealmStorageType.INT -> { val expectedValue: Long? = when (property.name) { @@ -633,122 +619,122 @@ class DynamicRealmObjectTests { } assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertEquals( - expectedValue, - dynamicSample.getValueList(property.name, Long::class)[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// expectedValue, +// dynamicSample.getList(property.name, Long::class)[0] +// ) } RealmStorageType.STRING -> { val expectedValue = defaultSample.stringField assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertEquals( - expectedValue, - dynamicSample.getValueList(property.name, String::class)[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// expectedValue, +// dynamicSample.getList(property.name, String::class)[0] +// ) } RealmStorageType.FLOAT -> { val expectedValue = defaultSample.floatField assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertEquals( - expectedValue, - dynamicSample.getValueList(property.name, Float::class)[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// expectedValue, +// dynamicSample.getList(property.name, Float::class)[0] +// ) } RealmStorageType.DOUBLE -> { val expectedValue = defaultSample.doubleField assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertEquals( - expectedValue, - dynamicSample.getValueList(property.name, Double::class)[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// expectedValue, +// dynamicSample.getList(property.name, Double::class)[0] +// ) } RealmStorageType.DECIMAL128 -> { val expectedValue = defaultSample.decimal128Field assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertEquals( - expectedValue, - dynamicSample.getValueList(property.name, Decimal128::class)[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// expectedValue, +// dynamicSample.getList(property.name, Decimal128::class)[0] +// ) } RealmStorageType.TIMESTAMP -> { val expectedValue = defaultSample.timestampField assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertEquals( - expectedValue, - dynamicSample.getValueList( - property.name, - RealmInstant::class - )[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// expectedValue, +// dynamicSample.getList( +// property.name, +// RealmInstant::class +// )[0] +// ) } RealmStorageType.OBJECT_ID -> { val expectedValue = defaultSample.bsonObjectIdField assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertEquals( - expectedValue, - dynamicSample.getValueList( - property.name, - BsonObjectId::class - )[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// expectedValue, +// dynamicSample.getList( +// property.name, +// BsonObjectId::class +// )[0] +// ) } RealmStorageType.UUID -> { val expectedValue = defaultSample.uuidField assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertEquals( - expectedValue, - dynamicSample.getValueList(property.name, RealmUUID::class)[0] + dynamicSample.get>(property.name)[0] ) +// assertEquals( +// expectedValue, +// dynamicSample.getList(property.name, RealmUUID::class)[0] +// ) } RealmStorageType.BINARY -> { val expectedValue = defaultSample.binaryField assertContentEquals( expectedValue, - dynamicSample.getValueList(property.name)[0] - ) - assertContentEquals( - expectedValue, - dynamicSample.getValueList(property.name, ByteArray::class)[0] + dynamicSample.get>(property.name)[0] ) +// assertContentEquals( +// expectedValue, +// dynamicSample.getList(property.name, ByteArray::class)[0] +// ) } RealmStorageType.OBJECT -> { val expectedValue = defaultSample.stringField assertEquals( expectedValue, - dynamicSample.getValueList(property.name)[0].getValue( + dynamicSample.get>(property.name)[0].get( "stringField" ) ) - assertEquals( - expectedValue, - dynamicSample.getValueList( - property.name, - DynamicRealmObject::class - )[0].getValue("stringField") - ) +// assertEquals( +// expectedValue, +// dynamicSample.getList( +// property.name, +// DynamicRealmObject::class +// )[0].get("stringField") +// ) } else -> error("Model contains untested properties: $property") } @@ -763,114 +749,114 @@ class DynamicRealmObjectTests { when (type.storageType) { RealmStorageType.BOOL -> { assertionsForNullable( - dynamicSample.getNullableValueSet(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueSet(property.name, Boolean::class) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.getNullableValueSet(property.name, Boolean::class) +// ) } RealmStorageType.INT -> { assertionsForNullable( - dynamicSample.getNullableValueSet(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueSet(property.name, Long::class) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.get>(property.name, Long::class) +// ) } RealmStorageType.STRING -> { assertionsForNullable( - dynamicSample.getNullableValueSet(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueSet(property.name, String::class) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.get>(property.name, String::class) +// ) } RealmStorageType.FLOAT -> { assertionsForNullable( - dynamicSample.getNullableValueSet(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueSet(property.name, Float::class) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.get>(property.name, Float::class) +// ) } RealmStorageType.DOUBLE -> { assertionsForNullable( - dynamicSample.getNullableValueSet(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueSet(property.name, Double::class) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.get>(property.name, Double::class) +// ) } RealmStorageType.TIMESTAMP -> { assertionsForNullable( - dynamicSample.getNullableValueSet(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueSet( - property.name, - RealmInstant::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// RealmInstant::class +// ) +// ) } RealmStorageType.OBJECT_ID -> { assertionsForNullable( - dynamicSample.getNullableValueSet( + dynamicSample.get>( property.name ) ) - assertionsForNullable( - dynamicSample.getNullableValueSet( - property.name, - BsonObjectId::class - ) - ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// BsonObjectId::class +// ) +// ) } RealmStorageType.UUID -> { assertionsForNullable( - dynamicSample.getNullableValueSet(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueSet(property.name, RealmUUID::class) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.get>(property.name, RealmUUID::class) +// ) } RealmStorageType.BINARY -> { assertContentEquals( null, - dynamicSample.getNullableValueSet(property.name) + dynamicSample.get>(property.name) .first() ) - assertContentEquals( - null, - dynamicSample.getNullableValueSet( - property.name, - ByteArray::class - ).first() - ) +// assertContentEquals( +// null, +// dynamicSample.get>( +// property.name, +// ByteArray::class, +// ).first() +// ) } RealmStorageType.DECIMAL128 -> { assertNull( - dynamicSample.getNullableValueSet(property.name) + dynamicSample.get>(property.name) .first() ) - assertNull( - dynamicSample.getNullableValueSet( - property.name, - Decimal128::class - ).first() - ) +// assertNull( +// dynamicSample.get>( +// property.name, +// Decimal128::class +// ).first() +// ) } RealmStorageType.OBJECT -> { assertNull( - dynamicSample.getNullableValueSet( + dynamicSample.get>( property.name ).first() ) - assertNull( - dynamicSample.getNullableValueSet( - property.name, - DynamicRealmObject::class - ).first() - ) +// assertNull( +// dynamicSample.get>( +// property.name, +// DynamicRealmObject::class +// ).first() +// ) } RealmStorageType.ANY -> { // The testing pattern doesn't work for RealmAny since we only land @@ -897,13 +883,13 @@ class DynamicRealmObjectTests { when (type.storageType) { RealmStorageType.BOOL -> { assertionsForValue( - dynamicSample.getValueSet(property.name), - defaultSample.booleanField - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, Boolean::class), + dynamicSample.get>(property.name), defaultSample.booleanField ) +// assertionsForValue( +// dynamicSample.get>(property.name, Boolean::class), +// defaultSample.booleanField +// ) } RealmStorageType.INT -> { val expectedValue: Long = when (property.name) { @@ -919,108 +905,108 @@ class DynamicRealmObjectTests { else -> error("Unexpected integral field ${property.name}") } assertionsForValue( - dynamicSample.getValueSet(property.name), - expectedValue - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, Long::class), + dynamicSample.get>(property.name), expectedValue ) +// assertionsForValue( +// dynamicSample.get>(property.name, Long::class), +// expectedValue +// ) } RealmStorageType.STRING -> { assertionsForValue( - dynamicSample.getValueSet(property.name), - defaultSample.stringField - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, String::class), + dynamicSample.get>(property.name), defaultSample.stringField ) +// assertionsForValue( +// dynamicSample.get>(property.name, String::class), +// defaultSample.stringField +// ) } RealmStorageType.FLOAT -> { assertionsForValue( - dynamicSample.getValueSet(property.name), - defaultSample.floatField - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, Float::class), + dynamicSample.get>(property.name), defaultSample.floatField ) +// assertionsForValue( +// dynamicSample.get>(property.name, Float::class), +// defaultSample.floatField +// ) } RealmStorageType.DOUBLE -> { assertionsForValue( - dynamicSample.getValueSet(property.name), - defaultSample.doubleField - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, Double::class), + dynamicSample.get>(property.name), defaultSample.doubleField ) +// assertionsForValue( +// dynamicSample.get>(property.name, Double::class), +// defaultSample.doubleField +// ) } RealmStorageType.TIMESTAMP -> { assertionsForValue( - dynamicSample.getValueSet(property.name), - defaultSample.timestampField - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, RealmInstant::class), + dynamicSample.get>(property.name), defaultSample.timestampField ) +// assertionsForValue( +// dynamicSample.get>(property.name, RealmInstant::class), +// defaultSample.timestampField +// ) } RealmStorageType.OBJECT_ID -> { assertionsForValue( - dynamicSample.getValueSet(property.name), - defaultSample.bsonObjectIdField - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, BsonObjectId::class), + dynamicSample.get>(property.name), defaultSample.bsonObjectIdField ) +// assertionsForValue( +// dynamicSample.get>(property.name, BsonObjectId::class), +// defaultSample.bsonObjectIdField +// ) } RealmStorageType.UUID -> { assertionsForValue( - dynamicSample.getValueSet(property.name), - defaultSample.uuidField - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, RealmUUID::class), + dynamicSample.get>(property.name), defaultSample.uuidField ) +// assertionsForValue( +// dynamicSample.get>(property.name, RealmUUID::class), +// defaultSample.uuidField +// ) } RealmStorageType.BINARY -> { assertionsForValue( - dynamicSample.getValueSet(property.name), - defaultSample.binaryField - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, ByteArray::class), + dynamicSample.get>(property.name), defaultSample.binaryField ) +// assertionsForValue( +// dynamicSample.get>(property.name, ByteArray::class), +// defaultSample.binaryField +// ) } RealmStorageType.DECIMAL128 -> { assertionsForValue( - dynamicSample.getValueSet(property.name), - defaultSample.decimal128Field - ) - assertionsForValue( - dynamicSample.getValueSet(property.name, Decimal128::class), + dynamicSample.get>(property.name), defaultSample.decimal128Field ) +// assertionsForValue( +// dynamicSample.get>(property.name, Decimal128::class), +// defaultSample.decimal128Field +// ) } RealmStorageType.OBJECT -> { val expectedValue = defaultSample.stringField assertEquals( expectedValue, - dynamicSample.getValueSet(property.name) - .first().getValue("stringField") - ) - assertEquals( - expectedValue, - dynamicSample.getValueSet( - property.name, - DynamicRealmObject::class - ).first().getValue("stringField") + dynamicSample.get>(property.name) + .first().get("stringField") ) +// assertEquals( +// expectedValue, +// dynamicSample.get>( +// property.name, +// DynamicRealmObject::class +// ).first().get("stringField") +// ) } else -> error("Model contains untested properties: $property") } @@ -1035,150 +1021,150 @@ class DynamicRealmObjectTests { when (type.storageType) { RealmStorageType.BOOL -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - Boolean::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// Boolean::class +// ) +// ) } RealmStorageType.INT -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - Long::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// Long::class +// ) +// ) } RealmStorageType.STRING -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary(property.name) - ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - String::class - ) + dynamicSample.get>(property.name) ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// String::class +// ) +// ) } RealmStorageType.BINARY -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name ) ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - ByteArray::class - ) - ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// ByteArray::class +// ) +// ) } RealmStorageType.OBJECT -> when (property.name) { "nullableObjectDictionaryFieldNull" -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name ) ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - DynamicRealmObject::class - ) - ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// DynamicRealmObject::class +// ) +// ) } "nullableObjectDictionaryFieldNotNull" -> { - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name ).also { dictionaryFromSample -> val inner = assertNotNull(dictionaryFromSample["A"]) - assertEquals("INNER", inner.getValue("stringField")) + assertEquals("INNER", inner.get("stringField")) } } } RealmStorageType.FLOAT -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name ) ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - Float::class - ) - ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// Float::class +// ) +// ) } RealmStorageType.DOUBLE -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name ) ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - Double::class - ) - ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// Double::class +// ) +// ) } RealmStorageType.DECIMAL128 -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name ) ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - Decimal128::class - ) - ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// Decimal128::class +// ) +// ) } RealmStorageType.TIMESTAMP -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name ) ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - RealmInstant::class - ) - ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// RealmInstant::class +// ) +// ) } RealmStorageType.OBJECT_ID -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name ) ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - BsonObjectId::class - ) - ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// BsonObjectId::class +// ) +// ) } RealmStorageType.UUID -> { assertionsForNullable( - dynamicSample.getNullableValueDictionary( + dynamicSample.get>( property.name ) ) - assertionsForNullable( - dynamicSample.getNullableValueDictionary( - property.name, - RealmUUID::class - ) - ) +// assertionsForNullable( +// dynamicSample.get>( +// property.name, +// RealmUUID::class +// ) +// ) } RealmStorageType.ANY -> { // The testing pattern doesn't work for RealmAny since we only land @@ -1204,7 +1190,7 @@ class DynamicRealmObjectTests { when (type.storageType) { RealmStorageType.BOOL -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), defaultSample.booleanField ) RealmStorageType.INT -> { @@ -1222,16 +1208,16 @@ class DynamicRealmObjectTests { else -> error("Unexpected integral field ${property.name}") } assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), expectedValue ) } RealmStorageType.STRING -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), defaultSample.stringField ) RealmStorageType.BINARY -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), defaultSample.binaryField ) RealmStorageType.OBJECT -> { @@ -1239,27 +1225,27 @@ class DynamicRealmObjectTests { // nullable and that has been tested above } RealmStorageType.FLOAT -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), defaultSample.floatField ) RealmStorageType.DOUBLE -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), defaultSample.doubleField ) RealmStorageType.DECIMAL128 -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), defaultSample.decimal128Field ) RealmStorageType.TIMESTAMP -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), defaultSample.timestampField ) RealmStorageType.OBJECT_ID -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), defaultSample.bsonObjectIdField ) RealmStorageType.UUID -> assertionsForValue( - dynamicSample.getValueDictionary(property.name), + dynamicSample.get>(property.name), defaultSample.uuidField ) RealmStorageType.ANY -> { @@ -1298,8 +1284,8 @@ class DynamicRealmObjectTests { .find() .first() - val actualReified = dynamicSample.getNullableValue(Sample::nullableRealmAnyField.name) - val actual = dynamicSample.getNullableValue(Sample::nullableRealmAnyField.name, RealmAny::class) + val actualReified = dynamicSample.get(Sample::nullableRealmAnyField.name) +// val actual = dynamicSample.get(Sample::nullableRealmAnyField.name, RealmAny::class) // Test we throw if trying to retrieve the object using its actual class instead of // DynamicRealmObject @@ -1307,22 +1293,22 @@ class DynamicRealmObjectTests { assertFailsWith { actualReified?.asRealmObject() } - assertFailsWith { - actual?.asRealmObject(Sample::class) - } +// assertFailsWith { +// actual?.asRealmObject(Sample::class) +// } // Retrieve values now assertEquals( expected?.asRealmObject()?.stringField, - actualReified?.asRealmObject()?.getValue("stringField") - ) - assertEquals( - expected?.asRealmObject()?.stringField, - actual?.asRealmObject()?.getValue("stringField") + actualReified?.asRealmObject()?.get("stringField") ) +// assertEquals( +// expected?.asRealmObject()?.stringField, +// actual?.asRealmObject()?.get("stringField") +// ) } else { assertEquals(expected, actualReified) - assertEquals(expected, actual) +// assertEquals(expected, actual) } realm.writeBlocking { delete(query(Sample::class)) } } @@ -1361,9 +1347,9 @@ class DynamicRealmObjectTests { val dynamicInner = dynamicRealm.query("Sample", "stringField = $0", "OUTER") .find() .first() - val actual = dynamicInner.getNullableValue("nullableRealmAnyField") + val actual = dynamicInner.get("nullableRealmAnyField") val actualObject = actual?.asRealmObject() - val actualString = actualObject?.getValue("stringField") + val actualString = actualObject?.get("stringField") assertEquals(sample.stringField, actualString) } } @@ -1386,12 +1372,11 @@ class DynamicRealmObjectTests { .find() .first() - val actualReifiedList = dynamicSample.getNullableValueList( + val actualReifiedList = dynamicSample.get>( Sample::nullableRealmAnyListField.name ) - val actualList = dynamicSample.getNullableValueList( + val actualList = dynamicSample.get>( Sample::nullableRealmAnyListField.name, - RealmAny::class ) for (i in realmAnyValues.indices) { val expected = realmAnyValues[i] @@ -1416,11 +1401,11 @@ class DynamicRealmObjectTests { // Retrieve values now assertEquals( expected?.asRealmObject()?.stringField, - actualReified?.asRealmObject()?.getValue("stringField") + actualReified?.asRealmObject()?.get("stringField") ) assertEquals( expected?.asRealmObject()?.stringField, - actual?.asRealmObject()?.getValue("stringField") + actual?.asRealmObject()?.get("stringField") ) } else { assertEquals(expected, actualReified) @@ -1458,21 +1443,20 @@ class DynamicRealmObjectTests { .find() .first() - val actualList = dynamicSample.getNullableValue( + val actualList = dynamicSample.get( Sample::nullableRealmAnyField.name, - RealmAny::class )!!.asList() actualList[0]!!.let { innerList -> val actualSample = innerList.asList()[0]!!.asRealmObject() assertIs(actualSample) - assertEquals("INNER_LIST", actualSample.getValue("stringField")) + assertEquals("INNER_LIST", actualSample.get("stringField")) } actualList[1]!!.let { innerDictionary -> val actualSample = innerDictionary.asDictionary()!!["key"]!!.asRealmObject() assertIs(actualSample) - assertEquals("INNER_DICT", actualSample.getValue("stringField")) + assertEquals("INNER_DICT", actualSample.get("stringField")) } } } @@ -1495,13 +1479,12 @@ class DynamicRealmObjectTests { .find() .first() - val actualReifiedSet = dynamicSample.getNullableValueSet( + val actualReifiedSet = dynamicSample.get>( Sample::nullableRealmAnySetField.name ) - val actualSet = dynamicSample.getNullableValueSet( - Sample::nullableRealmAnySetField.name, - RealmAny::class - ) +// val actualSet = dynamicSample.get>( +// Sample::nullableRealmAnySetField.name, +// ) fun assertions(actual: RealmAny?) { if (actual?.type == RealmAny.Type.OBJECT) { @@ -1511,7 +1494,7 @@ class DynamicRealmObjectTests { assertEquals( value.asRealmObject().stringField, actual.asRealmObject() - .getValue("stringField") + .get("stringField") ) assertionSucceeded = true return @@ -1526,9 +1509,9 @@ class DynamicRealmObjectTests { for (actual in actualReifiedSet) { assertions(actual) } - for (actual in actualSet) { - assertions(actual) - } +// for (actual in actualSet) { +// assertions(actual) +// } } // In case of testing sets we have to skip dynamic managed objects inside a @@ -1557,12 +1540,11 @@ class DynamicRealmObjectTests { .find() .first() - val actualReifiedDictionary = dynamicSample.getNullableValueDictionary( + val actualReifiedDictionary = dynamicSample.get>( Sample::nullableRealmAnyDictionaryField.name ) - val actualDictionary = dynamicSample.getNullableValueDictionary( + val actualDictionary = dynamicSample.get>( Sample::nullableRealmAnyDictionaryField.name, - RealmAny::class ) for (entry in realmAnyValues.entries) { val expected = realmAnyValues[entry.key] @@ -1586,12 +1568,12 @@ class DynamicRealmObjectTests { // Retrieve values now assertEquals( - expected?.asRealmObject()?.stringField, - actualReified?.asRealmObject()?.getValue("stringField") + expected?.asRealmObject()!!.stringField, + actualReified?.asRealmObject()?.get("stringField") ) assertEquals( expected?.asRealmObject()?.stringField, - actual.asRealmObject()?.getValue("stringField") + actual.asRealmObject()?.get("stringField") ) } else { assertEquals(expected, actualReified) @@ -1629,16 +1611,15 @@ class DynamicRealmObjectTests { .find() .first() - val actualDictionary = dynamicSample.getNullableValue( + val actualDictionary = dynamicSample.get( Sample::nullableRealmAnyField.name, - RealmAny::class )!!.asDictionary() actualDictionary["list"]!!.let { innerList -> val innerSample = innerList.asList()[0]!! val actualSample = innerSample.asRealmObject() assertIs(actualSample) - assertEquals("INNER_LIST", actualSample.getValue("stringField")) + assertEquals("INNER_LIST", actualSample.get("stringField")) assertFailsWith { innerSample.asRealmObject() @@ -1649,7 +1630,7 @@ class DynamicRealmObjectTests { val actualSample = innerSample.asRealmObject() assertIs(actualSample) - assertEquals("INNER_DICT", actualSample.getValue("stringField")) + assertEquals("INNER_DICT", actualSample.get("stringField")) assertFailsWith { innerSample.asRealmObject() @@ -1666,27 +1647,27 @@ class DynamicRealmObjectTests { val dynamicRealm = realm.asDynamicRealm() val dynamicSample = dynamicRealm.query("Sample").find().first() assertFailsWithMessage("Schema for type 'Sample' doesn't contain a property named 'UNKNOWN_FIELD'") { - dynamicSample.getValue("UNKNOWN_FIELD") + dynamicSample.get("UNKNOWN_FIELD") } assertFailsWithMessage("Schema for type 'Sample' doesn't contain a property named 'UNKNOWN_FIELD'") { - dynamicSample.getNullableValue("UNKNOWN_FIELD") + dynamicSample.get("UNKNOWN_FIELD") } assertFailsWithMessage("Schema for type 'Sample' doesn't contain a property named 'UNKNOWN_FIELD'") { - dynamicSample.getObject("UNKNOWN_FIELD") + dynamicSample.get("UNKNOWN_FIELD") } assertFailsWithMessage("Schema for type 'Sample' doesn't contain a property named 'UNKNOWN_FIELD'") { - dynamicSample.getValueList("UNKNOWN_FIELD") + dynamicSample.get>("UNKNOWN_FIELD") } assertFailsWithMessage("Schema for type 'Sample' doesn't contain a property named 'UNKNOWN_FIELD'") { - dynamicSample.getNullableValueList("UNKNOWN_FIELD") + dynamicSample.get>("UNKNOWN_FIELD") } assertFailsWithMessage("Schema for type 'Sample' doesn't contain a property named 'UNKNOWN_FIELD'") { - dynamicSample.getObjectList("UNKNOWN_FIELD") + dynamicSample.get>("UNKNOWN_FIELD") } } @Test - fun getValueVariants_throwsOnWrongTypes() { + fun getVariants_throwsOnWrongTypes() { realm.writeBlocking { copyToRealm(Sample()) } @@ -1695,33 +1676,33 @@ class DynamicRealmObjectTests { // Wrong type assertFailsWithMessage("Trying to access property 'Sample.stringField' as type: 'class kotlin.Long' but actual schema type is 'class kotlin.String'") { - dynamicSample.getValue("stringField") + dynamicSample.get("stringField") } assertFailsWithMessage("Trying to access property 'Sample.nullableStringField' as type: 'class kotlin.Long?' but actual schema type is 'class kotlin.String?'") { - dynamicSample.getNullableValue("nullableStringField") + dynamicSample.get("nullableStringField") } // Wrong nullability assertFailsWithMessage("Trying to access property 'Sample.nullableStringField' as type: 'class kotlin.Long' but actual schema type is 'class kotlin.String?'") { - dynamicSample.getValue("nullableStringField") + dynamicSample.get("nullableStringField") } assertFailsWithMessage("Trying to access property 'Sample.stringField' as type: 'RealmList' but actual schema type is 'class kotlin.String'") { - dynamicSample.getNullableValueList("stringField") + dynamicSample.get>("stringField") } // Wrong variants assertFailsWithMessage("Trying to access property 'Sample.nullableObject' as type: 'class kotlin.String' but actual schema type is 'class io.realm.kotlin.types.BaseRealmObject?'") { - dynamicSample.getValue("nullableObject") + dynamicSample.get("nullableObject") } assertFailsWithMessage("Trying to access property 'Sample.nullableObject' as type: 'class kotlin.String?' but actual schema type is 'class io.realm.kotlin.types.BaseRealmObject?'") { - dynamicSample.getNullableValue("nullableObject") + dynamicSample.get("nullableObject") } assertFailsWithMessage("Trying to access property 'Sample.stringListField' as type: 'class kotlin.Long' but actual schema type is 'RealmList'") { - dynamicSample.getValue("stringListField") + dynamicSample.get("stringListField") } assertFailsWithMessage("Trying to access property 'Sample.nullableStringListField' as type: 'class kotlin.Long?' but actual schema type is 'RealmList'") { - dynamicSample.getNullableValue("nullableStringListField") + dynamicSample.get("nullableStringListField") } } @@ -1735,10 +1716,10 @@ class DynamicRealmObjectTests { // We cannot get wrong mix of types or nullability in the API, so only checking wrong variants assertFailsWithMessage("Trying to access property 'Sample.stringField' as type: 'class io.realm.kotlin.types.BaseRealmObject?' but actual schema type is 'class kotlin.String'") { - dynamicSample.getObject("stringField") + dynamicSample.get("stringField") } assertFailsWithMessage("Trying to access property 'Sample.stringListField' as type: 'class io.realm.kotlin.types.BaseRealmObject?' but actual schema type is 'RealmList'") { - dynamicSample.getObject("stringListField") + dynamicSample.get("stringListField") } } @@ -1754,9 +1735,9 @@ class DynamicRealmObjectTests { val dynamicRealm = realm.asDynamicRealm() val dynamicSample = dynamicRealm.query("Sample").find().first() - val results = dynamicSample.getObjectList("objectListField").query("intField > 2").find() + val results = dynamicSample.get>("objectListField").query("intField > 2").find() assertEquals(3, results.size) - results.forEach { assertTrue { it.getValue("intField") > 2 } } + results.forEach { assertTrue { it.get("intField") > 2 } } } @Test @@ -1771,9 +1752,9 @@ class DynamicRealmObjectTests { val dynamicRealm = realm.asDynamicRealm() val dynamicSample = dynamicRealm.query("Sample").find().first() - val results = dynamicSample.getObjectSet("objectSetField").query("intField > 2").find() + val results = dynamicSample.get>("objectSetField").query("intField > 2").find() assertEquals(3, results.size) - results.forEach { assertTrue { it.getValue("intField") > 2 } } + results.forEach { assertTrue { it.get("intField") > 2 } } } @Test @@ -1786,39 +1767,39 @@ class DynamicRealmObjectTests { // Wrong type assertFailsWithMessage("Trying to access property 'Sample.stringListField' as type: 'RealmList' but actual schema type is 'RealmList'") { - dynamicSample.getValueList("stringListField") + dynamicSample.get>("stringListField") } assertFailsWithMessage("Trying to access property 'Sample.nullableStringListField' as type: 'RealmList' but actual schema type is 'RealmList'") { - dynamicSample.getNullableValueList("nullableStringListField") + dynamicSample.get>("nullableStringListField") } // Wrong nullability assertFailsWithMessage("Trying to access property 'Sample.nullableStringListField' as type: 'RealmList' but actual schema type is 'RealmList'") { - dynamicSample.getValueList("nullableStringListField") + dynamicSample.get>("nullableStringListField") } assertFailsWithMessage("Trying to access property 'Sample.stringListField' as type: 'RealmList' but actual schema type is 'RealmList'") { - dynamicSample.getNullableValueList("stringListField") + dynamicSample.get>("stringListField") } // Wrong variants assertFailsWithMessage("Trying to access property 'Sample.stringField' as type: 'RealmList' but actual schema type is 'class kotlin.String'") { - dynamicSample.getValueList("stringField") + dynamicSample.get>("stringField") } assertFailsWithMessage("Trying to access property 'Sample.nullableObject' as type: 'RealmList' but actual schema type is 'class io.realm.kotlin.types.BaseRealmObject?'") { - dynamicSample.getValueList("nullableObject") + dynamicSample.get>("nullableObject") } assertFailsWithMessage("Trying to access property 'Sample.nullableStringField' as type: 'RealmList' but actual schema type is 'class kotlin.String?'") { - dynamicSample.getNullableValueList("nullableStringField") + dynamicSample.get>("nullableStringField") } assertFailsWithMessage("Trying to access property 'Sample.nullableStringListField' as type: 'RealmList' but actual schema type is 'RealmList'") { - dynamicSample.getObjectList("nullableStringListField") + dynamicSample.get>("nullableStringListField") } } // We don't have an immutable RealmList so verify that we fail in an understandable manner if // trying to update the list @Test - fun getValueList_throwsIfModified() { + fun getList_throwsIfModified() { realm.writeBlocking { copyToRealm(Sample()) } @@ -1826,10 +1807,10 @@ class DynamicRealmObjectTests { val dynamicSample = dynamicRealm.query("Sample").find().first() assertFailsWithMessage("Cannot modify managed List outside of a write transaction.") { - dynamicSample.getValueList("stringListField").add("IMMUTABLE_LIST_ELEMENT") + dynamicSample.get>("stringListField").add("IMMUTABLE_LIST_ELEMENT") } assertFailsWithMessage("Cannot modify managed List outside of a write transaction.") { - dynamicSample.getNullableValueList("nullableStringListField") + dynamicSample.get>("nullableStringListField") .add("IMMUTABLE_LIST_ELEMENT") } } @@ -1863,7 +1844,7 @@ class DynamicRealmObjectTests { realm.close() assertFailsWithMessage("Cannot perform this operation on an invalid/deleted object") { - first.getValue("stringField") + first.get("stringField") } } diff --git a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicRealmTests.kt b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicRealmTests.kt index 8508a6b746..65fedd0909 100644 --- a/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicRealmTests.kt +++ b/packages/test-base/src/commonTest/kotlin/io/realm/kotlin/test/common/dynamic/DynamicRealmTests.kt @@ -35,7 +35,7 @@ package io.realm.kotlin.test.common.dynamic import io.realm.kotlin.Realm import io.realm.kotlin.RealmConfiguration -import io.realm.kotlin.dynamic.getValue +import io.realm.kotlin.dynamic.get import io.realm.kotlin.entities.Sample import io.realm.kotlin.internal.asDynamicRealm import io.realm.kotlin.test.common.utils.assertFailsWithMessage @@ -84,7 +84,7 @@ class DynamicRealmTests { val result = dynamicRealm.query("Sample", "intField = $0", 0).find() assertEquals(5, result.size) result.forEach { sample -> - assertEquals(0L, sample.getValue("intField")) + assertEquals(0L, sample.get("intField")) } } 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 1774409ad7..58f30b5983 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 @@ -23,7 +23,7 @@ import io.realm.kotlin.dynamic.DynamicMutableRealm import io.realm.kotlin.dynamic.DynamicMutableRealmObject import io.realm.kotlin.dynamic.DynamicRealm import io.realm.kotlin.dynamic.DynamicRealmObject -import io.realm.kotlin.dynamic.getValue +import io.realm.kotlin.dynamic.get import io.realm.kotlin.entities.Sample import io.realm.kotlin.entities.primarykey.PrimaryKeyString import io.realm.kotlin.ext.query @@ -136,15 +136,15 @@ class RealmMigrationTests { migrationContext.enumerate("MigrationSample") { oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject? -> newObject?.run { // Merge property - assertEquals("", getValue("fullName")) - set("fullName", "${oldObject.getValue("firstName")} ${ oldObject.getValue("lastName") }") + assertEquals("", get("fullName")) + set("fullName", "${oldObject.get("firstName")} ${ oldObject.get("lastName") }") // Rename property - assertEquals("", getValue("renamedProperty")) - set("renamedProperty", oldObject.getValue("property")) + assertEquals("", get("renamedProperty")) + set("renamedProperty", oldObject.get("property")) // Change type - assertEquals("", getValue("type")) - set("type", oldObject.getValue("type").toString()) + assertEquals("", get("type")) + set("type", oldObject.get("type").toString()) } } } @@ -168,8 +168,8 @@ class RealmMigrationTests { // FIXME Can we get this to have the DataMigrationContext as receiver migration = { it.enumerate("Sample") { oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject? -> - assertEquals(initialValue, oldObject.getValue("stringField")) - assertEquals(initialValue, newObject?.getValue("stringField")) + assertEquals(initialValue, oldObject.get("stringField")) + assertEquals(initialValue, newObject?.get("stringField")) newObject?.set("stringField", migratedValue) } } @@ -193,7 +193,7 @@ class RealmMigrationTests { // FIXME Can we get this to have the DataMigrationContext as receiver migration = { migrationContext -> migrationContext.enumerate("Sample") { oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject? -> - if (oldObject.getValue("intField") == 1L) { + if (oldObject.get("intField") == 1L) { // Delete all objects migrationContext.newRealm.run { delete(query("Sample"))