Skip to content

Commit

Permalink
Merge branch 'andrzejressel-scala3-union'
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpeeters committed Dec 15, 2024
2 parents 27095d3 + 57bd55e commit 68362d7
Show file tree
Hide file tree
Showing 46 changed files with 215 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Install the plugin (compatible with sbt 1.3+)
Add the following lines to the file ``myproject/project/plugins.sbt`` in your
project directory:

addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")



Expand Down Expand Up @@ -98,7 +98,7 @@ Compile / avroScalaSpecificCustomTypes := {
* `array` can be assigned to `ScalaSeq`, `ScalaArray`, `ScalaList`, and `ScalaVector`
* `enum` can be assigned to `JavaEnum`, `ScalaCaseObjectEnum`, `EnumAsScalaString`, and `ScalaEnumeration`
* `fixed` can be assigned to , `ScalaCaseClassWrapper` and `ScalaCaseClassWrapperWithSchema`(with schema in a companion object)
* `union` can be assigned to `OptionEitherShapelessCoproduct` and `OptionalShapelessCoproduct`
* `union` can be assigned to `OptionEitherShapelessCoproduct` (incompatible with `Specific`), `OptionalShapelessCoproduct` and `OptionScala3UnionType` (incompatible with `Standard`)
* `int`, `long`, `float`, `double` can be assigned to `ScalaInt`, `ScalaLong`, `ScalaFloat`, `ScalaDouble`
* `date` logical type can be assigned to `JavaTimeLocalDate` and `JavaSqlDate`
* `timestamp-millis` logical type can be assigned to `JavaTimeInstant` and `JavaSqlTimestamp`
Expand Down
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ThisBuild / organization := "com.julianpeeters"
ThisBuild / description := "Sbt plugin for compiling Avro to Scala"
ThisBuild / version := "2.8.4"
ThisBuild / version := "2.9.0"
ThisBuild / versionScheme := Some("semver-spec")

enablePlugins(SbtPlugin)
Expand All @@ -14,8 +14,8 @@ ThisBuild / crossSbtVersions := Seq(sbtVersion.value)
ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature", "-Ywarn-value-discard")

ThisBuild / libraryDependencies ++= Seq(
"com.julianpeeters" %% "avrohugger-core" % "2.8.4",
"com.julianpeeters" %% "avrohugger-filesorter" % "2.8.4",
"com.julianpeeters" %% "avrohugger-core" % "2.9.0",
"com.julianpeeters" %% "avrohugger-filesorter" % "2.9.0",
"io.spray" %% "spray-json" % "1.3.6",
"org.specs2" %% "specs2-core" % "4.20.2" % "test")

Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
2 changes: 1 addition & 1 deletion project/gpg.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.3.0")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.3.1")
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file("~/.ivy2/local/"))(Resolver.ivyStylePatterns)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Supplemental integration tests. sbt-avrohugger must be updated with any
changes made to avrohugger that need to be tested in a real-world project.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import avrohugger.types.{OptionScala3UnionType, ScalaADT}

Compile / sourceGenerators += (Compile / avroScalaGenerateSpecific).taskValue

organization := "com.julianpeeters"

name := "datatype-avro-serializaton-tests"

version := "0.4-SNAPSHOT"

scalaVersion := "3.3.4"

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")

libraryDependencies += "org.apache.avro" % "avro" % "1.11.4"

libraryDependencies += "org.specs2" %% "specs2-core" % "4.20.2" % Test

Compile / avroScalaSpecificCustomTypes := {
avrohugger.format.Standard.defaultTypes.copy(
union = OptionScala3UnionType,
protocol = ScalaADT
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)


Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"namespace": "com.example.avrohugger",
"name": "NonNullUnion",
"type": "record",
"fields": [
{
"name": "r6",
"type": [
"boolean",
"int",
"string"
]
}
]
},
{
"namespace": "com.example.avrohugger",
"name": "NullableUnion",
"type": "record",
"fields": [
{
"name": "r7",
"type": [
"null",
"boolean",
"int",
"string"
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package test

import java.io.File

import org.apache.avro.io.{DecoderFactory, EncoderFactory}
import org.apache.avro.generic.{ GenericDatumReader, GenericRecord}
import org.apache.avro.specific.{
SpecificDatumReader,
SpecificDatumWriter,
SpecificRecordBase
}
import org.apache.avro.Schema
import org.apache.avro.file.{ DataFileReader, DataFileWriter }

import org.specs2.mutable.Specification

object SpecificTestUtil extends Specification {

def write[T <: SpecificRecordBase](file: File, records: List[T]) = {
val userDatumWriter = new SpecificDatumWriter[T]
val dataFileWriter = new DataFileWriter[T](userDatumWriter)
dataFileWriter.create(records.head.getSchema, file);
records.foreach(record => dataFileWriter.append(record))
dataFileWriter.close();
}

def read[T <: SpecificRecordBase](file: File, records: List[T]) = {
val dummyRecord = new GenericDatumReader[GenericRecord]
val schema = new DataFileReader(file, dummyRecord).getSchema
val userDatumReader = new SpecificDatumReader[T](schema)
val dataFileReader = new DataFileReader[T](file, userDatumReader)
// Adapted from: https://github.com/tackley/avrohugger-list-issue/blob/master/src/main/scala/net/tackley/Reader.scala
// This isn't great scala, but represents how org.apache.avro.mapred.AvroInputFormat
// (via org.apache.avro.file.DataFileStream) interacts with the SpecificDatumReader.
var record: T = null.asInstanceOf[T]
var sameRecord: T = null.asInstanceOf[T]
val recordIter = records.iterator
while (dataFileReader.hasNext) {
sameRecord = dataFileReader.next(sameRecord)
record = recordIter.next()
}
dataFileReader.close()
sameRecord.equals(record)
}

def verifyWriteAndRead[T <: SpecificRecordBase](records: List[T]) = {
val fileName = s"${records.head.getClass.getName}"
val fileEnding = "avro"
val file = File.createTempFile(fileName, fileEnding)
file.deleteOnExit()
write(file, records)
read(file, records)
}

def verifyEncodeDecode[T <: SpecificRecordBase](record: T) = {
val schema = record.getSchema
val writer = new SpecificDatumWriter[T](schema)
val out = new java.io.ByteArrayOutputStream()
val encoder = EncoderFactory.get().binaryEncoder(out, null)
writer.write(record, encoder)
encoder.flush
val ba = out.toByteArray
ba.size must ===(1)
ba(0) must ===(0)
out.close
val reader = new SpecificDatumReader[T](schema)
val decoder = DecoderFactory.get().binaryDecoder(ba, null)
val decoded = reader.read(record, decoder)
decoded must ===(record)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test.specific

import com.example.avrohugger.{NonNullUnion, NullableUnion}
import org.specs2.mutable.Specification
import test.SpecificTestUtil

class SpecificSpec extends Specification {

"A case class with multiple non null types" should {
"serialize and deserialize correctly" in {
val record1 = NonNullUnion(true)
val record2 = NonNullUnion(1)
val record3 = NonNullUnion("abc")
val records = List(record1, record2, record3)
SpecificTestUtil.verifyWriteAndRead(records)
}
}

"A case class with multiple nullable types" should {
"serialize and deserialize correctly" in {
val record1 = NullableUnion(Some(true))
val record2 = NullableUnion(Some(1))
val record3 = NullableUnion(Some("abc"))
val record4 = NullableUnion(None)
val records = List(record1, record2, record3, record4)
SpecificTestUtil.verifyWriteAndRead(records)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
> ++3.3.4

> update

> clean

> test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ protocol Defaults {
bytes byt = "\u00FF";
fix1 fx;
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
2 changes: 1 addition & 1 deletion src/sbt-test/avrohugger/filesorter/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0")
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=1.10.6

0 comments on commit 68362d7

Please sign in to comment.