Skip to content

Commit

Permalink
chore(spring): upgrade to spring v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Verissimo Ribeiro committed Jan 13, 2024
1 parent 3cabe72 commit a5afd32
Show file tree
Hide file tree
Showing 41 changed files with 674 additions and 487 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,9 @@ If you'd like to contribute code to this project you can do so through GitHub by

By contributing your code, you agree to license your contribution under the terms of the Apache Licence.

## TODO
1. √ add reactive example
2. add documentation for between
3. remove the SEARCH_IN_SEPARATOR_PRM & SEARCH_IN_SEPARATOR_DEF
4. remove POSTGRES unaccent for reactive
# Spring Compatibility

| jpa-magic-filter version | Spring Version |
|--------------------------|----------------|
| 1.0.* | 2.7.* |
| 3.2.* | 3.2.* |
30 changes: 13 additions & 17 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.Base64

plugins {
id("org.springframework.boot") version "2.7.3"
id("io.spring.dependency-management") version "1.0.13.RELEASE"
kotlin("jvm") version "1.6.21"
kotlin("plugin.spring") version "1.6.21"
kotlin("plugin.jpa") version "1.6.21"
id("org.springframework.boot") version "3.2.1"
id("io.spring.dependency-management") version "1.1.4"
kotlin("jvm") version "1.9.21"
kotlin("plugin.spring") version "1.9.21"
kotlin("plugin.jpa") version "1.9.21"

kotlin("kapt") version "1.6.21"

id("org.jlleitschuh.gradle.ktlint") version "10.3.0"
id("org.jlleitschuh.gradle.ktlint") version "12.0.3"
`java-library`
`maven-publish`
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
id("io.github.gradle-nexus.publish-plugin") version "1.3.0"
signing
jacoco
}

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

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
withSourcesJar()
withJavadocJar()
}
Expand All @@ -50,22 +48,20 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

kapt("org.springframework.boot:spring-boot-configuration-processor")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")

// test service
testImplementation("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-data-jpa")
testImplementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.data:spring-data-relational:2.4.2")
testImplementation("org.springframework.data:spring-data-relational")
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "17"
}
}

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 3 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
31 changes: 23 additions & 8 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand All @@ -80,13 +80,11 @@ do
esac
done

APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

APP_NAME="Gradle"
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -133,22 +131,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -193,6 +198,10 @@ if "$cygwin" || "$msys" ; then
done
fi


# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
Expand All @@ -205,6 +214,12 @@ set -- \
org.gradle.wrapper.GradleWrapperMain \
"$@"

# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi

# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
Expand Down
15 changes: 9 additions & 6 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
Expand All @@ -25,7 +25,8 @@
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand All @@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand Down Expand Up @@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal
Expand Down
51 changes: 34 additions & 17 deletions src/main/kotlin/io/github/verissimor/lib/fieldparser/FieldParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import org.slf4j.LoggerFactory
import java.lang.reflect.Field

object FieldParser {

private val log: Logger = LoggerFactory.getLogger(this::class.java)

fun parseFields(params: Map<String, List<String>?>, clazz: Class<*>): List<ParsedField> {
fun parseFields(
params: Map<String, List<String>?>,
clazz: Class<*>,
): List<ParsedField> {
return params.mapNotNull { (field, value) ->

val parsedField = parseField(field, value, clazz)
Expand All @@ -41,7 +43,11 @@ object FieldParser {
}
}

private fun parseField(field: String, value: List<String>?, clazz: Class<*>): ParsedField {
private fun parseField(
field: String,
value: List<String>?,
clazz: Class<*>,
): ParsedField {
val group: Int = parseGroup(field)
val combineOperator: CombineOperator = if (field.startsWith("or__")) OR else AND
val normalized = normalize(field, group)
Expand All @@ -55,23 +61,27 @@ object FieldParser {
return ParsedField(resolvedOperator, resolvedFieldName, fieldClass, value, group, combineOperator)
}

private fun normalize(field: String, group: Int) = field.trim()
private fun normalize(
field: String,
group: Int,
) = field.trim()
.replace("[]", "") // remove array format of a few js libraries
.replace("__$group", "") // remove the group
.let { if (it.startsWith("or__")) it.replace("or__", "") else it } // remove the or

private fun fieldToFilterOperator(field: String): FilterOperator {
val type = FilterOperator.values()
.sortedByDescending { it.suffix.length }
.firstOrNull { field.lowercase().endsWith(it.suffix) } ?: FilterOperator.EQUAL
val type =
FilterOperator.values()
.sortedByDescending { it.suffix.length }
.firstOrNull { field.lowercase().endsWith(it.suffix) } ?: FilterOperator.EQUAL

return type
}

private fun overloadFilterOperator(
filterOperator: FilterOperator,
value: List<String>?,
fieldClass: Field?
fieldClass: Field?,
): FilterOperator {
val shouldTryOverload = value != null && filterOperator == FilterOperator.EQUAL
val fieldType: FieldType? = fieldClass.toFieldType()
Expand All @@ -87,25 +97,32 @@ object FieldParser {
val shouldTrySplitComma = value!!.size == 1 && listOf(ENUMERATED, NUMBER, LOCAL_DATE, UUID).contains(fieldType)
if (shouldTryOverload && shouldTrySplitComma && value.firstOrNull()!!.contains(",")) {
val list = parseStringIntoList(value.firstOrNull()!!)
if (list?.isNotEmpty() == true)
if (list?.isNotEmpty() == true) {
return FilterOperator.IN
}
}

return filterOperator
}

private fun resolveFieldName(field: String, type: FilterOperator?) =
type?.let { field.replace(it.suffix, "") } ?: field
private fun resolveFieldName(
field: String,
type: FilterOperator?,
) = type?.let { field.replace(it.suffix, "") } ?: field

private fun fieldToClass(field: String, root: Class<*>): 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 }
}
resultField =
if (resultField == null) {
root.declaredFields.firstOrNull { it.name == fieldP }
} else {
resultField?.type?.declaredFields?.firstOrNull { it.name == fieldP }
}
}

return resultField
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.verissimor.lib.fieldparser.domain

enum class CombineOperator {
AND, OR;
AND,
OR,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,36 @@ enum class FieldType {
INSTANT,
BOOLEAN,
UUID,
GENERIC;
GENERIC,
;

companion object {
val comparisonOperators = listOf(GREATER_THAN, GREATER_THAN_EQUAL, LESS_THAN, LESS_THAN_EQUAL, BETWEEN)
val comparisonTypes = listOf(NUMBER, LOCAL_DATE, INSTANT)

fun Field?.toFieldType(): FieldType? = when {
this == null -> null
this.type?.superclass?.name == "java.lang.Enum" -> ENUMERATED
this.type == LocalDate::class.java -> LOCAL_DATE
this.type == Instant::class.java -> INSTANT
this.type == Boolean::class.java ||
// this solves conflicts between kotlin/java
this.type.name == "java.lang.Boolean" -> BOOLEAN
fun Field?.toFieldType(): FieldType? =
when {
this == null -> null
this.type?.superclass?.name == "java.lang.Enum" -> ENUMERATED
this.type == LocalDate::class.java -> LOCAL_DATE
this.type == Instant::class.java -> INSTANT
this.type == Boolean::class.java ||
// this solves conflicts between kotlin/java
this.type.name == "java.lang.Boolean" -> BOOLEAN

this.type == java.util.UUID::class.java -> UUID
this.type == java.util.UUID::class.java -> UUID

this.type == Int::class.java ||
this.type == Long::class.java ||
this.type == BigDecimal::class.java ||
this.type.isAssignableFrom(Number::class.java) ||
// this solves conflicts between kotlin/java
this.type.name == "java.lang.Integer" ||
this.type.name == "java.math.BigDecimal" ||
this.type.name == "java.lang.Long" ||
this.type.isAssignableFrom(java.lang.Number::class.java) -> NUMBER
this.type == Int::class.java ||
this.type == Long::class.java ||
this.type == BigDecimal::class.java ||
this.type.isAssignableFrom(Number::class.java) ||
// this solves conflicts between kotlin/java
this.type.name == "java.lang.Integer" ||
this.type.name == "java.math.BigDecimal" ||
this.type.name == "java.lang.Long" ||
this.type.isAssignableFrom(java.lang.Number::class.java) -> NUMBER

else -> GENERIC
}
else -> GENERIC
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ enum class FilterOperator(val suffix: String, val allowNullableValue: Boolean =
BETWEEN("_is_between"),

NOT_EQUAL("_ne"),
EQUAL("")
EQUAL(""),
}
Loading

0 comments on commit a5afd32

Please sign in to comment.