-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from verissimor/feature/reactive
Feature/reactive
- Loading branch information
Showing
23 changed files
with
1,014 additions
and
29 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# R2dbc Magic Filter Example | ||
|
||
This is an example of usage of the library `R2dbc Magic Filter` | ||
|
||
## Running the project | ||
|
||
To run this project, use: | ||
|
||
```shell | ||
./gradlew bootRun | ||
``` | ||
|
||
## get users | ||
|
||
Use the paraters as described in the readme.md of the main lib. Eg.: | ||
|
||
``` | ||
http://localhost:8080/api/users?name_like=erick&gender_in=MALE,FEMALE&cityId_gt=1 | ||
http://localhost:8080/api/users/paged?size=1&gender=FEMALE | ||
http://localhost:8080/api/users/fluent?size=1&gender=FEMALE | ||
``` | ||
|
||
Have fun ;) |
15 changes: 15 additions & 0 deletions
15
...imor/examples/reactive/javagradlereactive/configuration/PageableWebFluxConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.github.verissimor.examples.reactive.javagradlereactive.configuration; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.web.ReactivePageableHandlerMethodArgumentResolver; | ||
import org.springframework.web.reactive.config.WebFluxConfigurer; | ||
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer; | ||
|
||
@Configuration | ||
public class PageableWebFluxConfiguration implements WebFluxConfigurer { | ||
|
||
@Override | ||
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) { | ||
configurer.addCustomResolver(new ReactivePageableHandlerMethodArgumentResolver()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/io/github/verissimor/lib/r2dbcmagicfilter/EnableR2dbcMagicFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.verissimor.lib.r2dbcmagicfilter | ||
|
||
import org.springframework.context.annotation.Import | ||
|
||
@Import(R2dbcMagicFilterConfigurer::class) | ||
annotation class EnableR2dbcMagicFilter |
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/io/github/verissimor/lib/r2dbcmagicfilter/R2dbcFieldParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.github.verissimor.lib.r2dbcmagicfilter | ||
|
||
import io.github.verissimor.lib.jpamagicfilter.domain.FilterOperator | ||
import io.github.verissimor.lib.r2dbcmagicfilter.domain.R2dbcParsedField | ||
import java.lang.reflect.Field | ||
|
||
object R2dbcFieldParser { | ||
|
||
fun parseField(field: String, value: Array<String>?, clazz: Class<*>): R2dbcParsedField { | ||
val normalized = normalize(field) | ||
val filterType = fieldToType(normalized, value) | ||
val resolvedFieldName = resolveFieldName(normalized, filterType) | ||
val fieldClass: Field? = fieldToClass(resolvedFieldName, clazz) | ||
|
||
return R2dbcParsedField(filterType, resolvedFieldName, fieldClass) | ||
} | ||
|
||
private fun normalize(field: String) = field.trim().replace("[]", "") | ||
|
||
private fun fieldToType(field: String, value: Array<String>?): FilterOperator { | ||
val type = FilterOperator.values() | ||
.sortedByDescending { it.suffix.length } | ||
.firstOrNull { field.contains(it.suffix) } ?: FilterOperator.EQUAL | ||
|
||
if (value != null && type == FilterOperator.EQUAL && value.size > 1) { | ||
return FilterOperator.IN | ||
} | ||
|
||
return type | ||
} | ||
|
||
private fun resolveFieldName(field: String, type: FilterOperator?) = | ||
type?.let { field.replace(it.suffix, "") } ?: field | ||
|
||
private fun fieldToClass(field: String, root: Class<*>): Field? { | ||
var resultField: Field? = null | ||
field.split(".") | ||
.forEach { fieldP -> | ||
resultField = if (resultField == null) { | ||
root.declaredFields.firstOrNull { it.name == fieldP } | ||
} else { | ||
resultField?.type?.declaredFields?.firstOrNull { it.name == fieldP } | ||
} | ||
} | ||
|
||
return resultField | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/io/github/verissimor/lib/r2dbcmagicfilter/R2dbcMagicFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.github.verissimor.lib.r2dbcmagicfilter | ||
|
||
import io.github.verissimor.lib.jpamagicfilter.MagicFilter.Companion.SEARCH_TYPE_AND | ||
import io.github.verissimor.lib.jpamagicfilter.MagicFilter.Companion.SEARCH_TYPE_OR | ||
import io.github.verissimor.lib.jpamagicfilter.MagicFilter.Companion.SEARCH_TYPE_PRM | ||
import io.github.verissimor.lib.jpamagicfilter.domain.DbFeatures | ||
import io.github.verissimor.lib.jpamagicfilter.domain.DbFeatures.NONE | ||
import io.github.verissimor.lib.jpamagicfilter.toSingleParameter | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.data.relational.core.query.Criteria | ||
import org.springframework.util.MultiValueMap | ||
|
||
class R2dbcMagicFilter( | ||
private val parameterMap: MultiValueMap<String, String> | ||
) { | ||
|
||
val log: Logger = LoggerFactory.getLogger(R2dbcMagicFilter::class.java) | ||
|
||
fun toCriteria(clazz: Class<*>, dbFeatures: DbFeatures = NONE): Criteria { | ||
|
||
val map: Map<String, Array<String>?> = parameterMap.keys.associateWith { parameterMap[it]?.toTypedArray() } | ||
val parsed = R2dbcPredicateParser.parsePredicates(map, clazz, dbFeatures) | ||
|
||
if (parsed.isEmpty()) { | ||
return Criteria.empty() | ||
} | ||
|
||
val criteria = when (map.toSingleParameter(SEARCH_TYPE_PRM)) { | ||
SEARCH_TYPE_AND, null -> parsed.reduce { acc, criteria -> acc.and(criteria) } | ||
SEARCH_TYPE_OR -> parsed.reduce { acc, criteria -> acc.or(criteria) } | ||
else -> error("Invalid searchType. Only allowed: and, or") | ||
} | ||
|
||
log.info(criteria.toString()) | ||
|
||
return criteria | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/io/github/verissimor/lib/r2dbcmagicfilter/R2dbcMagicFilterConfigurer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.github.verissimor.lib.r2dbcmagicfilter | ||
|
||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.core.MethodParameter | ||
import org.springframework.web.reactive.BindingContext | ||
import org.springframework.web.reactive.config.WebFluxConfigurer | ||
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver | ||
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer | ||
import org.springframework.web.server.ServerWebExchange | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc | ||
import reactor.core.publisher.Mono | ||
|
||
@Configuration | ||
@EnableWebMvc | ||
class R2dbcMagicFilterConfigurer : WebFluxConfigurer { | ||
override fun configureArgumentResolvers(configurer: ArgumentResolverConfigurer) { | ||
super.configureArgumentResolvers(configurer) | ||
configurer.addCustomResolver(R2dbcMagicFilterAttributeResolver()) | ||
} | ||
} | ||
|
||
class R2dbcMagicFilterAttributeResolver : HandlerMethodArgumentResolver { | ||
override fun supportsParameter(parameter: MethodParameter): Boolean { | ||
return parameter.parameterType == R2dbcMagicFilter::class.java | ||
} | ||
|
||
override fun resolveArgument(parameter: MethodParameter, bindingContext: BindingContext, exchange: ServerWebExchange): Mono<Any> { | ||
return Mono.just(R2dbcMagicFilter(exchange.request.queryParams)) | ||
} | ||
} |
Oops, something went wrong.