Skip to content

Commit

Permalink
feat(typing-safe): add support to kotlin typing safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Verissimo Ribeiro committed Sep 4, 2022
1 parent 665ebb8 commit 74cbf30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ List<User> getUsers(MagicFilter filter, @AuthenticationPrincipal User currentUse
}
```

## Kotlin typing safe

When using Kotlin is possible to use the helpers functions to write typing safe queries:

```kotlin
val filter = mapOf(
User::name.like("Matthew"),
User::age.gt(20)
).toR2dbcMagicFilter()
```

## Advanced Postgres Function

Execute the following piece of code on your db, [more info](https://stackoverflow.com/a/11007216/5795553):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.verissimor.lib.r2dbcmagicfilter

import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap
import kotlin.reflect.KProperty1

fun Map<String, Any>.toR2dbcMagicFilter(): R2dbcMagicFilter {
val map: MultiValueMap<String, String> = LinkedMultiValueMap()
this.forEach { map.add(it.key, it.value.toString()) }
return R2dbcMagicFilter(map)
}

fun magicFilterOfR2dbc(vararg prm: Pair<String, Any>): R2dbcMagicFilter = prm.toMap().toR2dbcMagicFilter()

fun <T, V> KProperty1<T, V?>.eq(value: Any): Pair<String, Any> = name to value.toString()
fun <T, V> KProperty1<T, V?>.gt(value: Any): Pair<String, Any> = name + "_gt" to value.toString()
fun <T, V> KProperty1<T, V?>.ge(value: Any): Pair<String, Any> = name + "_ge" to value.toString()
fun <T, V> KProperty1<T, V?>.lt(value: Any): Pair<String, Any> = name + "_lt" to value.toString()
fun <T, V> KProperty1<T, V?>.le(value: Any): Pair<String, Any> = name + "_le" to value.toString()
fun <T, V> KProperty1<T, V?>.like(value: Any): Pair<String, Any> = name + "_like" to value.toString()
fun <T, V> KProperty1<T, V?>.likeExp(value: Any): Pair<String, Any> = name + "_like_exp" to value.toString()
fun <T, V> KProperty1<T, V?>.notLike(value: Any): Pair<String, Any> = name + "_not_like" to value.toString()
fun <T, V> KProperty1<T, V?>.notLikeExp(value: Any): Pair<String, Any> = name + "_not_like_exp" to value.toString()
fun <T, V> KProperty1<T, V?>.`in`(value: Any): Pair<String, Any> = name + "_in" to value.toString()
fun <T, V> KProperty1<T, V?>.notIn(value: Any): Pair<String, Any> = name + "_not_in" to value.toString()
fun <T, V> KProperty1<T, V?>.isNull(value: Any): Pair<String, Any> = name + "_is_null" to value.toString()
fun <T, V> KProperty1<T, V?>.isNotNull(value: Any): Pair<String, Any> = name + "_is_not_null" to value.toString()

0 comments on commit 74cbf30

Please sign in to comment.