lint and simplify code
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
val containerAlwaysOn: String by project
|
||||
val disableLint: String by project
|
||||
val projectName = "postgres-json"
|
||||
|
||||
plugins {
|
||||
jacoco
|
||||
@@ -86,7 +87,7 @@ val sourcesJar by tasks.creating(Jar::class) {
|
||||
|
||||
apply(plugin = "docker-compose")
|
||||
dockerCompose {
|
||||
setProjectName("postgres-json")
|
||||
setProjectName(projectName)
|
||||
setProperty("useComposeFiles", listOf("docker-compose.yml"))
|
||||
setProperty("stopContainers", !containerAlwaysOn.toBoolean())
|
||||
isRequiredBy(project.tasks.test)
|
||||
@@ -95,7 +96,7 @@ dockerCompose {
|
||||
publishing {
|
||||
repositories {
|
||||
maven {
|
||||
name = "postgres-json"
|
||||
name = projectName
|
||||
url = uri("https://maven.pkg.github.com/flecomte/postgres-json")
|
||||
credentials {
|
||||
username = System.getenv("GITHUB_USERNAME")
|
||||
@@ -105,7 +106,7 @@ publishing {
|
||||
}
|
||||
|
||||
publications {
|
||||
create<MavenPublication>("postgres-json") {
|
||||
create<MavenPublication>(projectName) {
|
||||
from(components["java"])
|
||||
artifact(sourcesJar)
|
||||
}
|
||||
|
||||
@@ -14,9 +14,6 @@ import kotlin.jvm.Throws
|
||||
import kotlin.random.Random
|
||||
import kotlin.reflect.full.hasAnnotation
|
||||
|
||||
// TODO move execute function outside
|
||||
// TODO create function executeNullable
|
||||
|
||||
class Connection(
|
||||
private val database: String,
|
||||
private val username: String,
|
||||
|
||||
@@ -28,6 +28,7 @@ internal fun ScriptPart.removeParentheses(): ScriptPart {
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next part of script.
|
||||
* You can define a list of characters that end the part of script. Like `(` or space.
|
||||
@@ -36,35 +37,26 @@ internal fun ScriptPart.removeParentheses(): ScriptPart {
|
||||
internal fun ScriptPart.getNextScript(isEnd: Context.() -> Boolean = { false }): NextScript<String> {
|
||||
val status = Status()
|
||||
|
||||
fun String.unescape(): String {
|
||||
val first = take(1)
|
||||
val last = takeLast(1)
|
||||
return if (first == last && first in listOf("\"", "'")) {
|
||||
drop(1).dropLast(1).replace("$first$first", first)
|
||||
} else {
|
||||
this
|
||||
}
|
||||
for ((index, c) in restOfScript.withIndex()) {
|
||||
val prevChar = restOfScript.getOrNull(index - 1)
|
||||
val nextChar = restOfScript.getOrNull(index + 1)
|
||||
val nestedChars = listOf(prevChar, nextChar)
|
||||
|
||||
if (c == '"' && nestedChars.none { c == it }) {
|
||||
status.doubleQuoted = !status.doubleQuoted
|
||||
} else if (c == '\'' && nestedChars.none { c == it }) {
|
||||
status.simpleQuoted = !status.simpleQuoted
|
||||
}
|
||||
|
||||
for ((index, c) in restOfScript.withIndex()) {
|
||||
val nextChar = restOfScript.getOrNull(index + 1)
|
||||
val prevChar = restOfScript.getOrNull(index - 1)
|
||||
if (c == '"' && (nextChar != '"' && prevChar != '"')) {
|
||||
status.doubleQuoted = !status.doubleQuoted
|
||||
} else if (c == '\'' && (nextChar != '\'' && prevChar != '\'')) {
|
||||
status.simpleQuoted = !status.simpleQuoted
|
||||
} else if (c == '(' && status.isNotQuoted()) {
|
||||
status.parentheses++
|
||||
} else if (c == ')' && status.isNotQuoted()) {
|
||||
status.parentheses--
|
||||
} else if (c == '[' && status.isNotQuoted()) {
|
||||
status.brackets++
|
||||
} else if (c == ']' && status.isNotQuoted()) {
|
||||
status.brackets--
|
||||
} else if (c == '{' && status.isNotQuoted()) {
|
||||
status.braces++
|
||||
} else if (c == '}' && status.isNotQuoted()) {
|
||||
status.braces--
|
||||
if (status.isNotQuoted()) {
|
||||
when (c) {
|
||||
'(' -> status.parentheses++
|
||||
')' -> status.parentheses--
|
||||
'[' -> status.brackets++
|
||||
']' -> status.brackets--
|
||||
'{' -> status.braces++
|
||||
'}' -> status.braces--
|
||||
}
|
||||
}
|
||||
|
||||
if (isEnd(Context(index, c, status.copy(), restOfScript))) {
|
||||
@@ -72,11 +64,21 @@ internal fun ScriptPart.getNextScript(isEnd: Context.() -> Boolean = { false }):
|
||||
}
|
||||
}
|
||||
if (status.isNotEscaped()) {
|
||||
return NextScript(restOfScript.unescape().trim(), "").trimSpace()
|
||||
return NextScript(restOfScript.trim(), "").trimSpace()
|
||||
}
|
||||
throw ParseError()
|
||||
}
|
||||
|
||||
private fun String.unescape(): String {
|
||||
val first = take(1)
|
||||
val last = takeLast(1)
|
||||
return if (first == last && first in listOf("\"", "'")) {
|
||||
drop(1).dropLast(1).replace("$first$first", first)
|
||||
} else {
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
internal fun <T> NextScript<T>.trimSpace(): NextScript<T> {
|
||||
val spaces = charArrayOf(' ', '\n', '\t')
|
||||
return trim(chars = spaces)
|
||||
|
||||
@@ -117,8 +117,8 @@ class FunctionGenerator(private val functionsDirectories: List<URI>) {
|
||||
|
||||
val functionDecl = if (generics.isNotEmpty()) "inline fun <${generics.joinToString(", ")}>" else "fun"
|
||||
|
||||
if (hasReturn) {
|
||||
return """
|
||||
return if (hasReturn) {
|
||||
"""
|
||||
|package fr.postgresjson.functionGenerator.generated
|
||||
|
|
||||
|import com.fasterxml.jackson.core.type.TypeReference
|
||||
@@ -130,7 +130,7 @@ class FunctionGenerator(private val functionsDirectories: List<URI>) {
|
||||
|}
|
||||
""".trimMargin()
|
||||
} else {
|
||||
return """
|
||||
"""
|
||||
|package fr.postgresjson.functionGenerator.generated
|
||||
|
|
||||
|import fr.postgresjson.connexion.Requester
|
||||
|
||||
Reference in New Issue
Block a user