Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow aggregators on properties annotated with @PersistedName #1569

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Fixed
* Fix craches caused by posting to a released scheduler. (Issue [#1543](https://github.com/realm/realm-kotlin/issues/1543))
* Fix NPE when applying query aggregators on classes annotated with `@PersistedName`. (Issue [1569](https://github.com/realm/realm-kotlin/pull/1569))

### Compatibility
* File format: Generates Realms with file format v23.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal class ObjectQuery<E : BaseRealmObject> constructor(
RealmInterop.realm_query_find_all(queryPointer)
}

private val classMetadata: ClassMetadata? = realmReference.schemaMetadata[clazz.simpleName!!]
private val classMetadata: ClassMetadata? = realmReference.schemaMetadata[classKey]

internal constructor(
realmReference: RealmReference,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import io.realm.kotlin.internal.interop.PropertyKey
import io.realm.kotlin.internal.interop.PropertyType
import io.realm.kotlin.internal.interop.RealmInterop
import io.realm.kotlin.internal.interop.RealmPointer
import io.realm.kotlin.internal.interop.SCHEMA_NO_VALUE
import io.realm.kotlin.types.BaseRealmObject
import io.realm.kotlin.types.TypedRealmObject
import kotlin.reflect.KClass
Expand Down Expand Up @@ -64,6 +65,7 @@ public interface ClassMetadata {

public interface PropertyMetadata {
public val name: String
public val publicName: String
public val key: PropertyKey
public val collectionType: CollectionType
public val type: PropertyType
Expand Down Expand Up @@ -154,7 +156,7 @@ public class CachedClassMetadata(
primaryKeyProperty = properties.firstOrNull { it.isPrimaryKey }
isEmbeddedRealmObject = classInfo.isEmbedded

nameMap = properties.associateBy { it.name }
nameMap = properties.associateBy { it.name } + properties.filterNot { it.publicName == SCHEMA_NO_VALUE }.associateBy { it.publicName }
keyMap = properties.associateBy { it.key }
propertyMap = properties.associateBy { it.accessor }
}
Expand All @@ -169,6 +171,7 @@ public class CachedPropertyMetadata(
override val accessor: KProperty1<BaseRealmObject, Any?>? = null
) : PropertyMetadata {
override val name: String = propertyInfo.name
override val publicName: String = propertyInfo.publicName
override val key: PropertyKey = propertyInfo.key
override val collectionType: CollectionType = propertyInfo.collectionType
override val type: PropertyType = propertyInfo.type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.internal.asDynamicRealm
import io.realm.kotlin.migration.AutomaticSchemaMigration
import io.realm.kotlin.query.max
import io.realm.kotlin.query.min
import io.realm.kotlin.query.sum
import io.realm.kotlin.schema.RealmStorageType
import io.realm.kotlin.test.common.utils.assertFailsWithMessage
import io.realm.kotlin.test.platform.PlatformUtils
Expand Down Expand Up @@ -76,6 +79,54 @@ class PersistedNameTests {
PlatformUtils.deleteTempDir(tmpDir)
}

// --------------------------------------------------
// Aggregators
// --------------------------------------------------

@Test
fun aggregators_byPublicName() {
realm.writeBlocking {
copyToRealm(PersistedNameSample())
}

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().sum<Int>("publicNameIntField").find()
)

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().max<Int>("publicNameIntField").find()
)

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().min<Int>("publicNameIntField").find()
)
}

@Test
fun aggregators_byPersistedName() {
realm.writeBlocking {
copyToRealm(PersistedNameSample())
}

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().sum<Int>("persistedNameIntField").find()
)

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().max<Int>("persistedNameIntField").find()
)

assertEquals(
expected = 10,
actual = realm.query<PersistedNameSample>().min<Int>("persistedNameIntField").find()
)
}

// --------------------------------------------------
// Query
// --------------------------------------------------
Expand Down Expand Up @@ -479,6 +530,9 @@ class PersistedNameSample : RealmObject {
// the underlying schema due to being equal to the persisted name.
@PersistedName("sameName2")
var sameName2 = "Realm"

@PersistedName("persistedNameIntField")
var publicNameIntField: Int = 10
}

class PersistedNameParentSample(var id: Int) : RealmObject {
Expand Down