Skip to content

Commit

Permalink
feat: Support String or Map type for Operator.RAW (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Nov 6, 2024
1 parent 7757dea commit 66b0d3a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import me.ahoo.wow.api.query.Condition
import me.ahoo.wow.query.converter.AbstractConditionConverter
import me.ahoo.wow.serialization.MessageRecords
import me.ahoo.wow.serialization.state.StateAggregateRecords
import me.ahoo.wow.serialization.toJsonString
import java.io.StringReader
import java.time.LocalDateTime
import java.time.ZoneOffset

Expand Down Expand Up @@ -237,11 +239,23 @@ object ElasticsearchConditionConverter : AbstractConditionConverter<Query>() {
}

override fun raw(condition: Condition): Query {
require(condition.value is Query) {
"raw condition value must be a Query."
return when (condition.value) {
is Query -> {
condition.valueAs<Query>()
}

is String -> {
condition.valueAs<String>().toQuery()
}

else -> {
condition.value.toJsonString().toQuery()
}
}
}

return condition.valueAs<Query>()
private fun String.toQuery(): Query {
return Query.Builder().withJson(StringReader(this)).build()
}

fun Condition.toQuery(): Query {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,18 @@ class ElasticsearchConditionConverterTest {
}

@Test
fun `raw to query - should throw exception when raw query is String`() {
val exception = assertThrows<IllegalArgumentException> {
condition {
raw("")
}.toQuery()
}
assertThat(exception.message, equalTo("raw condition value must be a Query."))
fun `string raw to query`() {
val query = condition {
raw("""{"match_all":{}}""")
}.toQuery()
assertThat(query._kind(), equalTo(Query.Kind.MatchAll))
}

@Test
fun `map raw to query`() {
val query = condition {
raw(mapOf("match_all" to emptyMap<String, String>()))
}.toQuery()
assertThat(query._kind(), equalTo(Query.Kind.MatchAll))
}
}

0 comments on commit 66b0d3a

Please sign in to comment.