Skip to content

Commit

Permalink
Merge pull request #14 from verissimor/feature/reactive
Browse files Browse the repository at this point in the history
fix(reactive): force to use operator IN when there are comma
  • Loading branch information
verissimor authored Aug 26, 2022
2 parents 2fb93bb + bf9ef56 commit 9ff895d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/badges/branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion .github/badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {
}

group = "io.github.verissimor.lib"
version = System.getenv("RELEASE_VERSION") ?: "1.0.1-SNAPSHOT"
version = System.getenv("RELEASE_VERSION") ?: "1.0.2-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.ReactivePageableHandlerMethodArgumentResolver;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;

Expand All @@ -12,4 +13,12 @@ public class PageableWebFluxConfiguration implements WebFluxConfigurer {
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
configurer.addCustomResolver(new ReactivePageableHandlerMethodArgumentResolver());
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:4400/") // only for tests
.allowedMethods("GET", "POST")
.allowedHeaders("*");
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
package io.github.verissimor.lib.r2dbcmagicfilter

import io.github.verissimor.lib.jpamagicfilter.domain.FieldType
import io.github.verissimor.lib.jpamagicfilter.domain.FilterOperator
import io.github.verissimor.lib.jpamagicfilter.domain.ParsedField
import io.github.verissimor.lib.jpamagicfilter.toSingleString
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 filterOperator = fieldToFilterOperator(normalized)
val resolvedFieldName = resolveFieldName(normalized, filterOperator)
val fieldClass: Field? = fieldToClass(resolvedFieldName, clazz)

return R2dbcParsedField(filterType, resolvedFieldName, fieldClass)
val resolvedOperator = overloadFilterOperator(filterOperator, value, fieldClass)

return R2dbcParsedField(resolvedOperator, resolvedFieldName, fieldClass)
}

private fun normalize(field: String) = field.trim().replace("[]", "")

private fun fieldToType(field: String, value: Array<String>?): FilterOperator {
private fun fieldToFilterOperator(field: 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 type
}

private fun overloadFilterOperator(filterOperator: FilterOperator, value: Array<String>?, fieldClass: Field?): FilterOperator {
val shouldTryOverload = value != null && filterOperator == FilterOperator.EQUAL
if (shouldTryOverload && value!!.size > 1) {
return FilterOperator.IN
}

return type
val fieldType: FieldType? = ParsedField.getFieldType(fieldClass)
if (shouldTryOverload && fieldType == FieldType.NUMBER && value!!.toSingleString()!!.contains(",")) {
return FilterOperator.IN
}

return filterOperator
}

private fun resolveFieldName(field: String, type: FilterOperator?) =
Expand Down

0 comments on commit 9ff895d

Please sign in to comment.