From c4535f7a7aa4dc64b381af773e3ea4fb01e73a3a Mon Sep 17 00:00:00 2001 From: Willem Veelenturf Date: Thu, 5 Dec 2024 22:02:36 +0100 Subject: [PATCH] Build parser test --- .../compiler/core/emit/JavaEmitter.kt | 10 ++- .../compiler/core/emit/KotlinEmitter.kt | 10 ++- .../compiler/core/emit/ScalaEmitter.kt | 10 ++- .../compiler/core/parse/TypeParser.kt | 10 ++- .../compiler/core/parse/ParseTypeTest.kt | 70 +++++++++++++++++++ .../spring/kotlin/generated/Petstorev3.kt | 14 ++-- 6 files changed, 109 insertions(+), 15 deletions(-) diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt index 8e91252cb..ca1104876 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt @@ -100,8 +100,14 @@ open class JavaEmitter( private fun Reference.Primitive.emit() = when (type) { is Reference.Primitive.Type.String -> "String" - is Reference.Primitive.Type.Integer -> "Long" - is Reference.Primitive.Type.Number -> "Double" + is Reference.Primitive.Type.Integer -> when(type.precision){ + Reference.Primitive.Type.Precision._32 -> "int" + Reference.Primitive.Type.Precision._64 -> "Long" + } + is Reference.Primitive.Type.Number -> when(type.precision){ + Reference.Primitive.Type.Precision._32 -> "Float" + Reference.Primitive.Type.Precision._64 -> "Double" + } is Reference.Primitive.Type.Boolean -> "Boolean" } diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt index f31d405b4..489fa8cda 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt @@ -80,8 +80,14 @@ open class KotlinEmitter( is Reference.Custom -> value is Reference.Primitive -> when (type) { is Reference.Primitive.Type.String -> "String" - is Reference.Primitive.Type.Integer -> "Long" - is Reference.Primitive.Type.Number -> "Double" + is Reference.Primitive.Type.Integer -> when(type.precision){ + Reference.Primitive.Type.Precision._32 -> "Int" + Reference.Primitive.Type.Precision._64 -> "Long" + } + is Reference.Primitive.Type.Number -> when(type.precision){ + Reference.Primitive.Type.Precision._32 -> "Float" + Reference.Primitive.Type.Precision._64 -> "Double" + } is Reference.Primitive.Type.Boolean -> "Boolean" } } diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/ScalaEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/ScalaEmitter.kt index 1f90d6342..f339be7d4 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/ScalaEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/ScalaEmitter.kt @@ -79,8 +79,14 @@ open class ScalaEmitter( is Reference.Custom -> value is Reference.Primitive -> when (type) { is Reference.Primitive.Type.String -> "String" - is Reference.Primitive.Type.Integer -> "Long" - is Reference.Primitive.Type.Number -> "Double" + is Reference.Primitive.Type.Integer -> when(type.precision){ + Reference.Primitive.Type.Precision._32 -> "Int" + Reference.Primitive.Type.Precision._64 -> "Long" + } + is Reference.Primitive.Type.Number -> when(type.precision){ + Reference.Primitive.Type.Precision._32 -> "Float" + Reference.Primitive.Type.Precision._64 -> "Double" + } is Reference.Primitive.Type.Boolean -> "Boolean" } } diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/parse/TypeParser.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/parse/TypeParser.kt index 9206f0a76..12abff4eb 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/parse/TypeParser.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/parse/TypeParser.kt @@ -84,13 +84,19 @@ class TypeParser(logger: Logger) : AbstractParser(logger) { ) is WsInteger -> Reference.Primitive( - type = Reference.Primitive.Type.Integer(), + type = Reference.Primitive.Type.Integer(when(value) { + "Integer32" -> Reference.Primitive.Type.Precision._32 + else -> Reference.Primitive.Type.Precision._64 + }), isIterable = isIterable, isDictionary = isDict ) is WsNumber -> Reference.Primitive( - type = Reference.Primitive.Type.Number(), + type = Reference.Primitive.Type.Number(when(value) { + "Number32" -> Reference.Primitive.Type.Precision._32 + else -> Reference.Primitive.Type.Precision._64 + }), isIterable = isIterable, isDictionary = isDict ) diff --git a/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/parse/ParseTypeTest.kt b/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/parse/ParseTypeTest.kt index f56a8c669..9b0cf60a8 100644 --- a/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/parse/ParseTypeTest.kt +++ b/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/parse/ParseTypeTest.kt @@ -82,4 +82,74 @@ class ParseTypeTest { second shouldBe Reference.Custom(value = "Bal", isIterable = false, isDictionary = false) } } + + @Test + fun testIntegerNumberParser() { + val source = """ + |type Bar { int32: Integer32, int64: Integer[] } + |type Foo { num32: Number32, num64: Number64? } + """.trimMargin() + + WirespecSpec.tokenize(source) + .let(parser()::parse) + .shouldBeRight() + .also { it.size shouldBe 2 } + .let { + val (first, second) = it.toList() + first shouldBe Type( + comment = null, + identifier = DefinitionIdentifier("Bar"), + extends = emptyList(), + shape = Type.Shape( + value = listOf( + Field( + identifier = FieldIdentifier("int32"), + isNullable = false, + reference = Reference.Primitive( + type = Reference.Primitive.Type.Integer(Reference.Primitive.Type.Precision._32), + isIterable = false, + isDictionary = false + ) + ), + Field( + identifier = FieldIdentifier("int64"), + isNullable = false, + reference = Reference.Primitive( + type = Reference.Primitive.Type.Integer(Reference.Primitive.Type.Precision._64), + isIterable = true, + isDictionary = false + ) + ) + ) + ) + ) + second shouldBe Type( + comment = null, + identifier = DefinitionIdentifier("Foo"), + extends = emptyList(), + shape = Type.Shape( + value = listOf( + Field( + identifier = FieldIdentifier("num32"), + isNullable = false, + reference = Reference.Primitive( + type = Reference.Primitive.Type.Number(Reference.Primitive.Type.Precision._32), + isIterable = false, + isDictionary = false + ) + ), + Field( + identifier = FieldIdentifier("num64"), + isNullable = true, + reference = Reference.Primitive( + type = Reference.Primitive.Type.Number(Reference.Primitive.Type.Precision._64), + isIterable = false, + isDictionary = false + ) + ) + ) + ) + ) + } + } } diff --git a/src/integration/spring/src/jvmTest/kotlin/community/flock/wirespec/integration/spring/kotlin/generated/Petstorev3.kt b/src/integration/spring/src/jvmTest/kotlin/community/flock/wirespec/integration/spring/kotlin/generated/Petstorev3.kt index 8c4829745..ce0e7df23 100644 --- a/src/integration/spring/src/jvmTest/kotlin/community/flock/wirespec/integration/spring/kotlin/generated/Petstorev3.kt +++ b/src/integration/spring/src/jvmTest/kotlin/community/flock/wirespec/integration/spring/kotlin/generated/Petstorev3.kt @@ -812,9 +812,9 @@ object GetInventoryEndpoint : Wirespec.Endpoint { sealed interface Response2XX : Response - sealed interface ResponseMapStringLong : Response> + sealed interface ResponseMapStringInt : Response> - data class Response200(override val body: Map) : Response2XX>, ResponseMapStringLong { + data class Response200(override val body: Map) : Response2XX>, ResponseMapStringInt { override val status = 200 override val headers = Headers data object Headers : Wirespec.Response.Headers @@ -825,14 +825,14 @@ object GetInventoryEndpoint : Wirespec.Endpoint { is Response200 -> Wirespec.RawResponse( statusCode = response.status, headers = mapOf(), - body = serialization.serialize(response.body, typeOf>()), + body = serialization.serialize(response.body, typeOf>()), ) } fun fromResponse(serialization: Wirespec.Deserializer, response: Wirespec.RawResponse): Response<*> = when (response.statusCode) { 200 -> Response200( - body = serialization.deserialize(requireNotNull(response.body) { "body is null" }, typeOf>()), + body = serialization.deserialize(requireNotNull(response.body) { "body is null" }, typeOf>()), ) else -> error("Cannot match response with status: ${response.statusCode}") } @@ -1792,7 +1792,7 @@ enum class FindPetsByStatusParameterStatus (override val label: String): Wirespe data class Order( val id: Long?, val petId: Long?, - val quantity: Long?, + val quantity: Int?, val shipDate: String?, val status: OrderStatus?, val complete: Boolean? @@ -1833,7 +1833,7 @@ data class User( val email: String?, val password: String?, val phone: String?, - val userStatus: Long? + val userStatus: Int? ) data class Tag( @@ -1860,7 +1860,7 @@ enum class PetStatus (override val label: String): Wirespec.Enum { } data class ApiResponse( - val code: Long?, + val code: Int?, val type: String?, val message: String? )