Skip to content

Commit

Permalink
Added DataClassWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Apr 14, 2024
1 parent d8b093b commit a20e53d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 18 deletions.
9 changes: 7 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ allprojects {
version = Ci.version

java {
this.sourceCompatibility = JavaVersion.VERSION_1_8
this.targetCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_11
withSourcesJar()
}

repositories {
Expand All @@ -49,6 +50,10 @@ allprojects {
}
}

kotlin {
target { this. }
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
Expand Down
2 changes: 1 addition & 1 deletion centurion-avro/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies {
api(project(Projects.schemas))
implementation("org.apache.avro:avro:1.10.2")
implementation("org.apache.avro:avro:1.11.3")
}

apply("../publish.gradle.kts")
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object RequiredStringExtractor : Extractor<String> {
return when (val value = record.get(key)) {
is String -> value
is Utf8 -> value.toString()
else -> error("Unknown type $value for key $key")
else -> error("Unsupported type $value for key $key")
}
}
}
Expand All @@ -23,7 +23,7 @@ object OptionalStringExtractor : Extractor<String?> {
null -> null
is String -> value
is Utf8 -> value.toString()
else -> error("Unknown type $value for key $key")
else -> error("Unsupported type $value for key $key")
}
}
}
Expand All @@ -32,7 +32,7 @@ object RequiredDoubleExtractor : Extractor<Double> {
override fun extract(record: GenericRecord, key: String): Double {
return when (val value = record.get(key)) {
is Double -> value
else -> error("Unknown type $value for key $key")
else -> error("Unsupported type $value for key $key")
}
}
}
Expand All @@ -42,7 +42,7 @@ object OptionalDoubleExtractor : Extractor<Double?> {
return when (val value = record.get(key)) {
null -> null
is Double -> value
else -> error("Unknown type $value for key $key")
else -> error("Unsupported type $value for key $key")
}
}
}
Expand All @@ -51,7 +51,7 @@ object RequiredLongExtractor : Extractor<Long> {
override fun extract(record: GenericRecord, key: String): Long {
return when (val value = record.get(key)) {
is Long -> value
else -> error("Unknown type $value for key $key")
else -> error("Unsupported type $value for key $key")
}
}
}
Expand All @@ -61,7 +61,7 @@ object OptionalLongExtractor : Extractor<Long?> {
return when (val value = record.get(key)) {
null -> null
is Long -> value
else -> error("Unknown type $value for key $key")
else -> error("Unsupported type $value for key $key")
}
}
}
Expand All @@ -70,7 +70,7 @@ object RequiredIntExtractor : Extractor<Int> {
override fun extract(record: GenericRecord, key: String): Int {
return when (val value = record.get(key)) {
is Int -> value
else -> error("Unknown type $value for key $key")
else -> error("Unsupported type $value for key $key")
}
}
}
Expand All @@ -80,7 +80,7 @@ object OptionalIntExtractor : Extractor<Int?> {
return when (val value = record.get(key)) {
null -> null
is Int -> value
else -> error("Unknown type $value for key $key")
else -> error("Unsupported type $value for key $key")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.sksamuel.centurion.avro.generation

import org.apache.avro.Schema

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

fun generate(schema: Schema): DataClass {
require(schema.type == Schema.Type.RECORD) { "Type must be a record in order to generate a data class" }
val members = schema.fields.map { member(it) }
return DataClass(schema.namespace, schema.name, members)
}

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
Schema.Type.INT -> Type.IntType
Schema.Type.LONG -> Type.LongType
Schema.Type.FLOAT -> Type.FloatType
Schema.Type.DOUBLE -> Type.DoubleType
Schema.Type.BOOLEAN -> Type.BooleanType
else -> error("Invalid code path")
}
return Member(field.name(), type)
}
}

/**
* Creates a string representation of a data class.
*/
object DataClassWriter {
fun write(ds: DataClass): String {
return buildString {
appendLine("package ${ds.packageName}")
appendLine()
appendLine("data class ${ds.className}(")
appendLine(")")
}
}
}

/**
* Models a data class.
*/
data class DataClass(val packageName: String, val className: String, val members: List<Member>)

sealed interface Type {
data class RecordType(val packageName: String, val className: String) : Type
object BooleanType : Type
object StringType : Type
object IntType : Type
object LongType : Type
object FloatType : Type
object DoubleType : Type
}

data class Member(val name: String, val type: Type)
10 changes: 3 additions & 7 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ pluginManagement {
repositories {
mavenLocal()
mavenCentral()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots/")
}
maven {
url = uri("https://plugins.gradle.org/m2/")
}
maven("https://oss.sonatype.org/content/repositories/snapshots/")
maven("https://plugins.gradle.org/m2/")
}
}

Expand All @@ -18,6 +14,6 @@ enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

include("centurion-arrow")
include("centurion-avro")
include("centurion-schemas")
include("centurion-orc")
include("centurion-parquet")
include("centurion-schemas")

0 comments on commit a20e53d

Please sign in to comment.