From 96aaa1ada9d1b303b385be6e7a5dcdf5ff86d48f Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 3 Jun 2023 01:01:33 +0200 Subject: [PATCH] fix toString of parameter --- .../fr/postgresjson/definition/Parameter.kt | 25 ++++++++++++-- .../definition/parse/ParsingFunction.kt | 4 +-- .../postgresjson/definition/FunctionTest.kt | 34 ++++++------------- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/fr/postgresjson/definition/Parameter.kt b/src/main/kotlin/fr/postgresjson/definition/Parameter.kt index f331519..876c3d6 100644 --- a/src/main/kotlin/fr/postgresjson/definition/Parameter.kt +++ b/src/main/kotlin/fr/postgresjson/definition/Parameter.kt @@ -6,16 +6,21 @@ class ParameterType( val name: String, val precision: Int? = null, val scale: Int? = null, - val isArray: Boolean = false, + val array: String? = null, ) { + val isArray: Boolean + get() = array != null + override fun toString(): String { - return if (precision == null && scale == null) { + val type = if (precision == null && scale == null) { name } else if (scale == null) { """$name($precision)""" } else { """$name($precision, $scale)""" } + + return type+array } } @@ -38,6 +43,22 @@ class Parameter( ) enum class Direction { IN, OUT, INOUT } + + override fun toString(): String { + return buildString { + append(direction.name.lowercase()) + if (name?.isNotBlank() == true) { + append(" ") + append(name) + } + append(" ") + append(type.toString()) + if (default?.isNotBlank() == true) { + append(" ") + append(default) + } + } + } } interface ParametersInterface { diff --git a/src/main/kotlin/fr/postgresjson/definition/parse/ParsingFunction.kt b/src/main/kotlin/fr/postgresjson/definition/parse/ParsingFunction.kt index 5a947fa..337bba5 100644 --- a/src/main/kotlin/fr/postgresjson/definition/parse/ParsingFunction.kt +++ b/src/main/kotlin/fr/postgresjson/definition/parse/ParsingFunction.kt @@ -129,14 +129,14 @@ private fun ScriptPart.getParameterType(): NextScript { .apply { rest = nextScriptPart } rest = rest.trimStart(' ', '\n', '\t', ')') - val isArray = rest.restOfScript.contains("""\[[0-9]*]""".toRegex()) + val arrayDef = rest.restOfScript.trim().takeIf(String::isNotBlank) return NextScript( ParameterType( name = name.value.trim().trim('[', ']'), precision = precision.value, scale = scale.value, - isArray = isArray + array = arrayDef ), fullType.nextScriptPart.restOfScript ) diff --git a/src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt b/src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt index 8471c9e..4a69e7e 100644 --- a/src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt +++ b/src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt @@ -451,6 +451,16 @@ class FunctionTest : FreeSpec({ param[0].type.name shouldBe "text" param[1].type.name shouldBe "text" } + + "should return the type with array" { + param[0].type.toString() shouldBe "text[45]" + param[1].type.toString() shouldBe "text[1][]" + } + + "should return the type name" { + param[0].toString() shouldBe "in one text[45]" + param[1].toString() shouldBe "in two text[1][]" + } } } @@ -635,28 +645,4 @@ class FunctionTest : FreeSpec({ // } // } // } -// -// "getDefinition" - { -// TODO("must be implement") -// } -// -// "getParametersIndexedByName" - { -// TODO("must be implement") -// } -// -// "has same definition" - { -// TODO("must be implement") -// } -// -// "is different from" - { -// TODO("must be implement") -// } -// -// "script" - { -// TODO("must be implement") -// } -// -// "source" - { -// TODO("must be implement") -// } })