Skip to content

Commit

Permalink
Extra test
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel authored and jerrevanveluw committed Oct 17, 2024
1 parent 28b6bba commit cf7128f
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 120 deletions.
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ kotest_assertions_arrow = { module = "io.kotest.extensions:kotest-assertions-arr
kotest_engine = { module = "io.kotest:kotest-framework-engine", version.ref = "kotest" }
kotlin_reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlin_stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
kotlin_test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlinx_openapi_bindings = { module = "community.flock.kotlinx.openapi.bindings:kotlin-openapi-bindings", version.ref = "kotlinx_openapi_bindings" }
kotlinx_resources = { module = "com.goncalossilva:resources", version.ref = "kotlinx_resources" }
kotlinx_rgxgen = { module = "community.flock.kotlinx.rgxgen:kotlin-rgxgen", version.ref = "kotlinx_rgxgen" }
Expand All @@ -42,7 +43,7 @@ maven_project = { module = "org.apache.maven:maven-project", version.ref = "mave

[bundles]
jackson = ["jackson_databind", "jackson_kotlin"]
kotest = ["kotest_engine", "kotest_assertions", "kotest_assertions_arrow"]
kotest = ["kotlin_test", "kotest_engine", "kotest_assertions", "kotest_assertions_arrow"]
maven_plugin = ["maven_plugin_api", "maven_plugin_annotations", "maven_project"]

[plugins]
Expand All @@ -51,5 +52,6 @@ intellij = { id = "org.jetbrains.intellij.platform", version.ref = "intellij" }
kotest = { id = "io.kotest.multiplatform", version.ref = "kotest" }
kotlin_jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin_multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlin_serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kotlinx_resources = { id = "com.goncalossilva.resources", version.ref = "kotlinx_resources" }
maven_plugin = { id = "de.benediktritter.maven-plugin-development", version.ref = "maven_plugin" }
1 change: 0 additions & 1 deletion src/compiler/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ kotlin {
}
commonTest {
dependencies {
implementation(kotlin("test"))
implementation(libs.bundles.kotest)
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/converter/avro/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("com.goncalossilva.resources") version "0.4.0"
kotlin("jvm") apply false
id("com.github.johnrengelman.shadow") apply false
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.kotlinx.resources)
}

group = "${Settings.GROUP_ID}.converter"
version = Settings.version
group = "${libs.versions.group.id.get()}.converter"
version = System.getenv(libs.versions.from.env.get()) ?: libs.versions.default.get()

repositories {
mavenCentral()
Expand All @@ -18,31 +16,33 @@ kotlin {
macosX64()
macosArm64()
linuxX64()
mingwX64()
js(IR) {
nodejs()
}
jvm {
withJava()
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
languageVersion.set(JavaLanguageVersion.of(libs.versions.java.get()))
}
}
}
sourceSets {
commonMain {
dependencies {
implementation(project(":src:compiler:core"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation(libs.kotlinx.serialization)
}
}
commonTest {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation(kotlin("test-junit"))
implementation("com.goncalossilva:resources:0.4.0")
implementation(libs.kotlinx.resources)
implementation(libs.bundles.kotest)
}
}
val jvmTest by getting {
dependencies {
implementation(libs.bundles.jackson)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
package community.flock.wirespec.convert.avro

import community.flock.wirespec.compiler.core.parse.AST
import community.flock.wirespec.compiler.core.parse.nodes.Type
import community.flock.wirespec.compiler.core.parse.nodes.Enum
import community.flock.wirespec.compiler.core.parse.Enum
import community.flock.wirespec.compiler.core.parse.Field
import community.flock.wirespec.compiler.core.parse.Identifier
import community.flock.wirespec.compiler.core.parse.Reference
import community.flock.wirespec.compiler.core.parse.Type

object AvroConverter {

val nullType = AvroModel.SimpleType("null")
fun AvroModel.TypeList.isNullable() = contains(nullType)
fun AvroModel.SimpleType.toPrimitive() = when (this.value) {
"boolean" -> Type.Shape.Field.Reference.Primitive.Type.Boolean
"int" -> Type.Shape.Field.Reference.Primitive.Type.Integer
"long" -> Type.Shape.Field.Reference.Primitive.Type.Integer
"float" -> Type.Shape.Field.Reference.Primitive.Type.Number
"double" -> Type.Shape.Field.Reference.Primitive.Type.Number
"bytes" -> Type.Shape.Field.Reference.Primitive.Type.String
"string" -> Type.Shape.Field.Reference.Primitive.Type.String
"boolean" -> Reference.Primitive.Type.Boolean
"int" -> Reference.Primitive.Type.Integer
"long" -> Reference.Primitive.Type.Integer
"float" -> Reference.Primitive.Type.Number
"double" -> Reference.Primitive.Type.Number
"bytes" -> Reference.Primitive.Type.String
"string" -> Reference.Primitive.Type.String
else -> TODO("primitive not mapped ${this.value}")
}

fun AvroModel.Type.toReference(isIterable: Boolean = false): Type.Shape.Field.Reference = when (this) {
fun AvroModel.Type.toReference(isIterable: Boolean = false): Reference = when (this) {
is AvroModel.SimpleType -> when (value) {
"null" -> TODO("Map primitive null")
"boolean", "int", "long", "float", "double", "bytes", "string" -> Type.Shape.Field.Reference.Primitive(toPrimitive(), isIterable, false)
else -> Type.Shape.Field.Reference.Custom(value, isIterable, false)
"boolean", "int", "long", "float", "double", "bytes", "string" -> Reference.Primitive(toPrimitive(), isIterable, false)
else -> Reference.Custom(value, isIterable, false)
}

is AvroModel.ArrayType -> items.toReference(true)
is AvroModel.RecordType -> Type.Shape.Field.Reference.Custom(name, isIterable, false)
is AvroModel.EnumType -> Type.Shape.Field.Reference.Custom(name, isIterable, false)
is AvroModel.RecordType -> Reference.Custom(name, isIterable, false)
is AvroModel.EnumType -> Reference.Custom(name, isIterable, false)
}

fun AvroModel.TypeList.toReference(): Type.Shape.Field.Reference {
fun AvroModel.TypeList.toReference(): Reference {
val list = minus(nullType)
if (list.size != 1) {
TODO("Union types are not supported")
Expand All @@ -40,18 +43,21 @@ object AvroConverter {
}

fun AvroModel.RecordType.toType() = Type(
name = name,
identifier = Identifier(name),
shape = Type.Shape(this.fields.map {
Type.Shape.Field(
identifier = Type.Shape.Field.Identifier(it.name),
Field(
identifier = Identifier(it.name),
reference = it.type.toReference(),
isNullable = it.type.isNullable()
)
})
}),
comment = null,
extends = emptyList()
)

fun AvroModel.EnumType.toEnum() = Enum(
name = name,
comment = null,
identifier = Identifier(name),
entries = symbols.toSet()
)

Expand All @@ -61,4 +67,4 @@ object AvroConverter {
is AvroModel.ArrayType -> items.flatten()
is AvroModel.EnumType -> listOf(toEnum())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package community.flock.wirespec.convert.avro

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -64,6 +65,7 @@ object AvroModel {
) : Type


@OptIn(ExperimentalSerializationApi::class)
object TypeListSerializer : KSerializer<TypeList> {

override val descriptor: SerialDescriptor = buildSerialDescriptor("TypeListSerializer", PolymorphicKind.SEALED)
Expand All @@ -84,6 +86,7 @@ object AvroModel {
}
}

@OptIn(ExperimentalSerializationApi::class)
object TypeSerializer : KSerializer<Type> {

override val descriptor: SerialDescriptor = buildSerialDescriptor("TypeSerializer", PolymorphicKind.SEALED)
Expand All @@ -106,7 +109,7 @@ object AvroModel {
element.containsKey("items") -> input.json.decodeFromJsonElement(ArrayType.serializer(), element)
element.containsKey("fields") -> input.json.decodeFromJsonElement(RecordType.serializer(), element)
element.containsKey("symbols") -> input.json.decodeFromJsonElement(EnumType.serializer(), element)
else -> TODO("Unknown object type: ${element}")
else -> TODO("Unknown object type: $element")
}

is JsonArray -> TODO("Type can never be an array")
Expand Down
Loading

0 comments on commit cf7128f

Please sign in to comment.