adding normalisation to the query string cases

This commit is contained in:
Adam Brown 2021-10-27 12:46:17 +01:00
parent 2681601d35
commit d5ed95988d
3 changed files with 50 additions and 9 deletions

View file

@ -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
}
}

View file

@ -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 <T : RealmObject> RealmQuery<T>.process(field: String, queryStringValue: QueryStringValue): RealmQuery<T> {
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.SENSITIVE, QueryStringValue.Case.NORMALIZED -> Case.SENSITIVE
}
}

View file

@ -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)
}
}