From d5ed95988d80d4a723ca8d95dd0fe4b52a1c4e30 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 27 Oct 2021 12:46:17 +0100 Subject: [PATCH] adding normalisation to the query string cases --- .../android/sdk/api/query/QueryStringValue.kt | 12 ++++++--- .../query/QueryStringValueProcessor.kt | 20 ++++++++++---- .../android/sdk/internal/util/Normalizer.kt | 27 +++++++++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/Normalizer.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/query/QueryStringValue.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/query/QueryStringValue.kt index 1490bff6a8..5141751888 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/query/QueryStringValue.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/query/QueryStringValue.kt @@ -20,7 +20,10 @@ package org.matrix.android.sdk.api.query * Basic query language. All these cases are mutually exclusive. */ sealed interface QueryStringValue { - sealed interface ContentQueryStringValue : QueryStringValue + sealed interface ContentQueryStringValue : QueryStringValue { + val string: String + val case: Case + } object NoCondition : QueryStringValue object IsNull : QueryStringValue @@ -28,11 +31,12 @@ sealed interface QueryStringValue { object IsEmpty : QueryStringValue object IsNotEmpty : QueryStringValue - data class Equals(val string: String, val case: Case = Case.SENSITIVE) : ContentQueryStringValue - data class Contains(val string: String, val case: Case = Case.SENSITIVE) : ContentQueryStringValue + data class Equals(override val string: String, override val case: Case = Case.SENSITIVE) : ContentQueryStringValue + data class Contains(override val string: String, override val case: Case = Case.SENSITIVE) : ContentQueryStringValue enum class Case { SENSITIVE, - INSENSITIVE + INSENSITIVE, + NORMALIZED } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/query/QueryStringValueProcessor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/query/QueryStringValueProcessor.kt index d323943ad4..33564eb610 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/query/QueryStringValueProcessor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/query/QueryStringValueProcessor.kt @@ -21,10 +21,13 @@ import io.realm.RealmObject import io.realm.RealmQuery import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.query.QueryStringValue.ContentQueryStringValue +import org.matrix.android.sdk.internal.util.Normalizer import timber.log.Timber import javax.inject.Inject -class QueryStringValueProcessor @Inject constructor() { +class QueryStringValueProcessor @Inject constructor( + private val normalizer: Normalizer +) { fun RealmQuery.process(field: String, queryStringValue: QueryStringValue): RealmQuery { return when (queryStringValue) { @@ -37,16 +40,23 @@ class QueryStringValueProcessor @Inject constructor() { is QueryStringValue.IsEmpty -> isEmpty(field) is QueryStringValue.IsNotEmpty -> isNotEmpty(field) is ContentQueryStringValue -> when (queryStringValue) { - is QueryStringValue.Equals -> equalTo(field, queryStringValue.string, queryStringValue.case.toRealmCase()) - is QueryStringValue.Contains -> contains(field, queryStringValue.string, queryStringValue.case.toRealmCase()) + is QueryStringValue.Equals -> equalTo(field, queryStringValue.toRealmValue(), queryStringValue.case.toRealmCase()) + is QueryStringValue.Contains -> contains(field, queryStringValue.toRealmValue(), queryStringValue.case.toRealmCase()) } } } + + private fun ContentQueryStringValue.toRealmValue(): String { + return when (case) { + QueryStringValue.Case.NORMALIZED -> normalizer.normalize(string) + QueryStringValue.Case.SENSITIVE, QueryStringValue.Case.INSENSITIVE -> string + } + } } private fun QueryStringValue.Case.toRealmCase(): Case { return when (this) { - QueryStringValue.Case.INSENSITIVE -> Case.INSENSITIVE - QueryStringValue.Case.SENSITIVE -> Case.SENSITIVE + QueryStringValue.Case.INSENSITIVE -> Case.INSENSITIVE + QueryStringValue.Case.SENSITIVE, QueryStringValue.Case.NORMALIZED -> Case.SENSITIVE } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/Normalizer.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/Normalizer.kt new file mode 100644 index 0000000000..2df1faa784 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/Normalizer.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.util + +import java.text.Normalizer +import javax.inject.Inject + +class Normalizer @Inject constructor() { + + fun normalize(input: String): String { + return Normalizer.normalize(input.lowercase(), Normalizer.Form.NFD) + } +}