Skip to content

Commit

Permalink
Added writer tests and more build setup
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Apr 14, 2024
1 parent a20e53d commit abd5a28
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 27 deletions.
29 changes: 4 additions & 25 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat

buildscript {
repositories {
mavenLocal()
mavenCentral()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots/")
}
}
}

plugins {
id("java")
id("java-library")
id("maven-publish")
kotlin("jvm")
id("signing")
kotlin("jvm").version(Libs.kotlinVersion)
}

allprojects {
Expand All @@ -30,12 +17,6 @@ allprojects {
withSourcesJar()
}

repositories {
mavenLocal()
mavenCentral()
maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots") }
}

dependencies {
testImplementation(Libs.Kotest.assertions)
testImplementation(Libs.Kotest.junit5)
Expand All @@ -50,11 +31,9 @@ allprojects {
}
}

kotlin {
target { this. }
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.jvmTarget = "11"
kotlinOptions.apiVersion = "1.8"
kotlinOptions.languageVersion = "1.8"
}
}
1 change: 1 addition & 0 deletions centurion-avro/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dependencies {
api(project(Projects.schemas))
testImplementation(libs.bundles.testing)
implementation("org.apache.avro:avro:1.11.3")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.sksamuel.centurion.avro.generation
import org.apache.avro.Schema

/**
* Generates a data class for a given schema.
* Generates a data class model for a given schema.
*/
class DataClassGenerator {

Expand All @@ -13,7 +13,7 @@ class DataClassGenerator {
return DataClass(schema.namespace, schema.name, members)
}

fun member(field: Schema.Field): Member {
private fun member(field: Schema.Field): Member {
val type = when (field.schema().type) {
Schema.Type.RECORD -> Type.RecordType(field.schema().namespace, field.schema().name)
Schema.Type.STRING -> Type.StringType
Expand All @@ -37,9 +37,24 @@ object DataClassWriter {
appendLine("package ${ds.packageName}")
appendLine()
appendLine("data class ${ds.className}(")
ds.members.forEach {
appendLine(" val ${it.name}: ${typeToString(it.type)},")
}
appendLine(")")
}
}

private fun typeToString(type: Type): String {
return when (type) {
Type.BooleanType -> "Boolean"
Type.DoubleType -> "Double"
Type.FloatType -> "Float"
Type.IntType -> "Int"
Type.LongType -> "Long"
is Type.RecordType -> type.packageName + "." + type.className
Type.StringType -> "String"
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.sksamuel.centurion.avro.generation

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class DataClassWriterTest : FunSpec({

test("should write all types") {
DataClassWriter.write(
DataClass(
packageName = "com.sksamuel",
className = "Foo",
members = listOf(
Member("a", Type.StringType),
Member("b", Type.BooleanType),
Member("c", Type.LongType),
Member("d", Type.IntType),
Member("e", Type.FloatType),
Member("f", Type.DoubleType),
Member("g", Type.RecordType("x", "y")),
)
)
).trim() shouldBe
"""
package com.sksamuel
data class Foo(
val a: String,
val b: Boolean,
val c: Long,
val d: Int,
val e: Float,
val f: Double,
val g: x.y,
)
""".trim()
}

})
36 changes: 36 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pluginManagement {
maven("https://oss.sonatype.org/content/repositories/snapshots/")
maven("https://plugins.gradle.org/m2/")
}
plugins {
kotlin("jvm").version("1.8.21")
}
}

enableFeaturePreview("STABLE_CONFIGURATION_CACHE")
Expand All @@ -17,3 +20,36 @@ include("centurion-avro")
include("centurion-orc")
include("centurion-parquet")
include("centurion-schemas")

dependencyResolutionManagement {
repositories {
mavenCentral()
mavenLocal()
maven("https://oss.sonatype.org/content/repositories/snapshots")
maven("https://s01.oss.sonatype.org/content/repositories/snapshots")
}
versionCatalogs {
create("libs") {
val tabby = "2.2.11"
library("sksamuel-tabby", "com.sksamuel.tabby:tabby-fp:$tabby")

val kotest = "5.8.1"
library("kotest-datatest", "io.kotest:kotest-framework-datatest:$kotest")
library("kotest-junit5", "io.kotest:kotest-runner-junit5:$kotest")
library("kotest-core", "io.kotest:kotest-assertions-core:$kotest")
library("kotest-json", "io.kotest:kotest-assertions-json:$kotest")
library("kotest-property", "io.kotest:kotest-property:$kotest")

bundle(
"testing",
listOf(
"kotest-datatest",
"kotest-junit5",
"kotest-core",
"kotest-json",
"kotest-property",
)
)
}
}
}

0 comments on commit abd5a28

Please sign in to comment.