Skip to content

Commit

Permalink
Merge branch 'steve-e-issue-190-fixedtype-nameclash-fqn'
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpeeters committed Jan 6, 2024
2 parents 4a75ee9 + fe909cb commit e099ae6
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 19 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ _Note:_ Currently [Treehugger](http://eed3si9n.com/treehugger/comments.html#Scal

##### Get the dependency with:

"com.julianpeeters" %% "avrohugger-core" % "2.8.0"
"com.julianpeeters" %% "avrohugger-core" % "2.8.2"


##### Description:
Expand Down Expand Up @@ -211,7 +211,7 @@ namespace rewritten. Multiple conflicting wildcards are not permitted.

##### Get the dependency with:

"com.julianpeeters" %% "avrohugger-filesorter" % "2.8.0"
"com.julianpeeters" %% "avrohugger-filesorter" % "2.8.2"


##### Description:
Expand All @@ -231,17 +231,17 @@ To ensure dependent schemas are compiled in the proper order (thus avoiding `org
#### `avrohugger-tools`


Download the avrohugger-tools jar for Scala [2.12](https://search.maven.org/remotecontent?filepath=com/julianpeeters/avrohugger-tools_2.12/2.8.0/avrohugger-tools_2.12-2.8.0-assembly.jar), or Scala [2.13](https://search.maven.org/remotecontent?filepath=com/julianpeeters/avrohugger-tools_2.13/2.8.0/avrohugger-tools_2.13-2.8.0-assembly.jar) (>30MB!) and use it like the avro-tools jar `Usage: [-string] (schema|protocol|datafile) input... outputdir`:
Download the avrohugger-tools jar for Scala [2.12](https://search.maven.org/remotecontent?filepath=com/julianpeeters/avrohugger-tools_2.12/2.8.2/avrohugger-tools_2.12-2.8.2-assembly.jar), or Scala [2.13](https://search.maven.org/remotecontent?filepath=com/julianpeeters/avrohugger-tools_2.13/2.8.2/avrohugger-tools_2.13-2.8.2-assembly.jar) (>30MB!) and use it like the avro-tools jar `Usage: [-string] (schema|protocol|datafile) input... outputdir`:


* `generate` generates Scala case class definitions:

`java -jar /path/to/avrohugger-tools_2.12-2.8.0-assembly.jar generate schema user.avsc . `
`java -jar /path/to/avrohugger-tools_2.12-2.8.2-assembly.jar generate schema user.avsc . `


* `generate-specific` generates definitions that extend Avro's `SpecificRecordBase`:

`java -jar /path/to/avrohugger-tools_2.12-2.8.0-assembly.jar generate-specific schema user.avsc . `
`java -jar /path/to/avrohugger-tools_2.12-2.8.2-assembly.jar generate-specific schema user.avsc . `


## Warnings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ object SpecificImporter extends Importer {
getTopLevelSchemas(schemaOrProtocol, schemaStore, typeMatcher)
val recordSchemas = getRecordSchemas(topLevelSchemas)
val enumSchemas = getEnumSchemas(topLevelSchemas)
val fixedSchemas = getFixedSchemas(topLevelSchemas)
val userDefinedDeps = getUserDefinedImports(recordSchemas ++ enumSchemas ++ fixedSchemas, currentNamespace, typeMatcher)
val userDefinedDeps = getUserDefinedImports(recordSchemas ++ enumSchemas, currentNamespace, typeMatcher)
val shapelessDeps = getShapelessImports(recordSchemas, typeMatcher)
val libraryDeps = shapelessDeps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ object DefaultParamMatcher {
}
case Schema.Type.NULL => NULL
case Schema.Type.FIXED =>
REF(classStore.generatedClasses(avroSchema)).APPLY(CustomDefaultParamMatcher.checkCustomDecimalType(
val name = RootClass.newClass(s"${avroSchema.getNamespace()}.${classStore.generatedClasses(avroSchema)}")
REF(name).APPLY(CustomDefaultParamMatcher.checkCustomDecimalType(
decimalType = typeMatcher.avroScalaTypes.decimal,
schema = avroSchema,
default = ArrayClass.APPLY()))
Expand Down
3 changes: 2 additions & 1 deletion avrohugger-core/src/main/scala/matchers/TypeMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class TypeMatcher(
default = StringClass) {
case UUID => RootClass.newClass(nme.createNameType("java.util.UUID"))
}
case Schema.Type.FIXED => classStore.generatedClasses(schema)
case Schema.Type.FIXED =>
RootClass.newClass(s"${schema.getNamespace()}.${classStore.generatedClasses(schema)}")
case Schema.Type.BYTES => CustomTypeMatcher.checkCustomDecimalType(avroScalaTypes.decimal, schema)
case Schema.Type.RECORD =>
{
Expand Down
25 changes: 25 additions & 0 deletions avrohugger-core/src/test/avro/fixed_two.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"type": "record",
"name": "FixedTwo",
"namespace": "fixedtwo",
"fields": [
{
"name": "first",
"type": {
"type": "fixed",
"name": "fixed",
"namespace": "fixedtwo.one",
"size": 16
}
},
{
"name": "second",
"type": {
"type": "fixed",
"name": "fixed",
"namespace": "fixedtwo.two",
"size": 16
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/** MACHINE-GENERATED FROM AVRO SCHEMA. DO NOT EDIT DIRECTLY */
package fixedtwo

import scala.annotation.switch

final case class FixedTwo(var first: fixedtwo.one.fixed, var second: fixedtwo.two.fixed) extends org.apache.avro.specific.SpecificRecordBase {
def this() = this(fixedtwo.one.fixed(Array()), fixedtwo.two.fixed(Array()))
def get(field$: Int): AnyRef = {
(field$: @switch) match {
case 0 => {
first
}.asInstanceOf[AnyRef]
case 1 => {
second
}.asInstanceOf[AnyRef]
case _ => new org.apache.avro.AvroRuntimeException("Bad index")
}
}
def put(field$: Int, value: Any): Unit = {
(field$: @switch) match {
case 0 => this.first = {
value
}.asInstanceOf[fixedtwo.one.fixed]
case 1 => this.second = {
value
}.asInstanceOf[fixedtwo.two.fixed]
case _ => new org.apache.avro.AvroRuntimeException("Bad index")
}
()
}
def getSchema: org.apache.avro.Schema = fixedtwo.FixedTwo.SCHEMA$
}

object FixedTwo {
val SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"FixedTwo\",\"namespace\":\"fixedtwo\",\"fields\":[{\"name\":\"first\",\"type\":{\"type\":\"fixed\",\"name\":\"fixed\",\"namespace\":\"fixedtwo.one\",\"size\":16}},{\"name\":\"second\",\"type\":{\"type\":\"fixed\",\"name\":\"fixed\",\"namespace\":\"fixedtwo.two\",\"size\":16}}]}")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** MACHINE-GENERATED FROM AVRO SCHEMA. DO NOT EDIT DIRECTLY */
package fixedtwo.one

final case class fixed() extends org.apache.avro.specific.SpecificFixed {
def getSchema: org.apache.avro.Schema = fixedtwo.one.fixed.SCHEMA$
def readExternal(in: java.io.ObjectInput): Unit = {
fixedtwo.one.fixed.READER$.read(this, org.apache.avro.specific.SpecificData.getDecoder(in))
()
}
def writeExternal(out: java.io.ObjectOutput): Unit = {
fixedtwo.one.fixed.WRITER$.write(this, org.apache.avro.specific.SpecificData.getEncoder(out))
}
}

object fixed {
val SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"fixed\",\"name\":\"fixed\",\"namespace\":\"fixedtwo.one\",\"size\":16}")
val READER$ = new org.apache.avro.specific.SpecificDatumReader[fixed](fixedtwo.one.fixed.SCHEMA$)
val WRITER$ = new org.apache.avro.specific.SpecificDatumWriter[fixed](fixedtwo.one.fixed.SCHEMA$)
def apply(data: Array[Byte]): fixed = {
val fixed = new fixedtwo.one.fixed()
fixed.bytes(data)
fixed
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** MACHINE-GENERATED FROM AVRO SCHEMA. DO NOT EDIT DIRECTLY */
package fixedtwo.two

final case class fixed() extends org.apache.avro.specific.SpecificFixed {
def getSchema: org.apache.avro.Schema = fixedtwo.two.fixed.SCHEMA$
def readExternal(in: java.io.ObjectInput): Unit = {
fixedtwo.two.fixed.READER$.read(this, org.apache.avro.specific.SpecificData.getDecoder(in))
()
}
def writeExternal(out: java.io.ObjectOutput): Unit = {
fixedtwo.two.fixed.WRITER$.write(this, org.apache.avro.specific.SpecificData.getEncoder(out))
}
}

object fixed {
val SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"fixed\",\"name\":\"fixed\",\"namespace\":\"fixedtwo.two\",\"size\":16}")
val READER$ = new org.apache.avro.specific.SpecificDatumReader[fixed](fixedtwo.two.fixed.SCHEMA$)
val WRITER$ = new org.apache.avro.specific.SpecificDatumWriter[fixed](fixedtwo.two.fixed.SCHEMA$)
def apply(data: Array[Byte]): fixed = {
val fixed = new fixedtwo.two.fixed()
fixed.bytes(data)
fixed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package example.logical

import scala.annotation.switch

final case class LogicalSc(var data: BigDecimal, var fxField: fxType, var ts: java.time.Instant, var dt: java.time.LocalDate, var uuid: java.util.UUID, var dataBig: BigDecimal, var tm: java.time.LocalTime, var timeMicros: java.time.LocalTime, var timestampMicros: java.time.ZonedDateTime, var localTimestampMicros: java.time.LocalDateTime, var localTimestampMillis: java.time.LocalDateTime) extends org.apache.avro.specific.SpecificRecordBase {
def this() = this(scala.math.BigDecimal(0), fxType(scala.math.BigDecimal(0)), java.time.Instant.now, java.time.LocalDate.now, java.util.UUID.randomUUID, scala.math.BigDecimal(0), java.time.LocalTime.now, java.time.LocalTime.MIDNIGHT, java.time.ZonedDateTime.of(java.time.LocalDateTime.MIN, java.time.ZoneId.of("UTC")), java.time.LocalDateTime.MIN, java.time.LocalDateTime.MIN)
final case class LogicalSc(var data: BigDecimal, var fxField: example.logical.fxType, var ts: java.time.Instant, var dt: java.time.LocalDate, var uuid: java.util.UUID, var dataBig: BigDecimal, var tm: java.time.LocalTime, var timeMicros: java.time.LocalTime, var timestampMicros: java.time.ZonedDateTime, var localTimestampMicros: java.time.LocalDateTime, var localTimestampMillis: java.time.LocalDateTime) extends org.apache.avro.specific.SpecificRecordBase {
def this() = this(scala.math.BigDecimal(0), example.logical.fxType(scala.math.BigDecimal(0)), java.time.Instant.now, java.time.LocalDate.now, java.util.UUID.randomUUID, scala.math.BigDecimal(0), java.time.LocalTime.now, java.time.LocalTime.MIDNIGHT, java.time.ZonedDateTime.of(java.time.LocalDateTime.MIN, java.time.ZoneId.of("UTC")), java.time.LocalDateTime.MIN, java.time.LocalDateTime.MIN)
def get(field$: Int): AnyRef = {
(field$: @switch) match {
case 0 => {
Expand Down Expand Up @@ -83,7 +83,7 @@ final case class LogicalSc(var data: BigDecimal, var fxField: fxType, var ts: ja
}.asInstanceOf[BigDecimal]
case 1 => this.fxField = {
value
}.asInstanceOf[fxType]
}.asInstanceOf[example.logical.fxType]
case 2 => this.ts = {
value match {
case (l: Long) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package example.logical

import shapeless.tag.@@

final case class LogicalSc(data: @@[scala.math.BigDecimal, (shapeless.Nat._9, shapeless.Nat._2)], fxField: fxType, ts: java.time.Instant, dt: java.time.LocalDate, uuid: java.util.UUID, dataBig: @@[scala.math.BigDecimal, ((shapeless.Nat._2, shapeless.Nat._0), (shapeless.Nat._1, shapeless.Nat._2))], tm: java.time.LocalTime, timeMicros: java.time.LocalTime, timestampMicros: java.time.ZonedDateTime, localTimestampMicros: java.time.LocalDateTime, localTimestampMillis: java.time.LocalDateTime)
final case class LogicalSc(data: @@[scala.math.BigDecimal, (shapeless.Nat._9, shapeless.Nat._2)], fxField: example.logical.fxType, ts: java.time.Instant, dt: java.time.LocalDate, uuid: java.util.UUID, dataBig: @@[scala.math.BigDecimal, ((shapeless.Nat._2, shapeless.Nat._0), (shapeless.Nat._1, shapeless.Nat._2))], tm: java.time.LocalTime, timeMicros: java.time.LocalTime, timestampMicros: java.time.ZonedDateTime, localTimestampMicros: java.time.LocalDateTime, localTimestampMillis: java.time.LocalDateTime)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** MACHINE-GENERATED FROM AVRO SCHEMA. DO NOT EDIT DIRECTLY */
package example.logical

final case class LogicalSc(data: BigDecimal, fxField: fxType, ts: java.time.Instant, dt: java.time.LocalDate, uuid: java.util.UUID, dataBig: BigDecimal, tm: java.time.LocalTime, timeMicros: java.time.LocalTime, timestampMicros: java.time.ZonedDateTime, localTimestampMicros: java.time.LocalDateTime, localTimestampMillis: java.time.LocalDateTime)
final case class LogicalSc(data: BigDecimal, fxField: example.logical.fxType, ts: java.time.Instant, dt: java.time.LocalDate, uuid: java.util.UUID, dataBig: BigDecimal, tm: java.time.LocalTime, timeMicros: java.time.LocalTime, timestampMicros: java.time.ZonedDateTime, localTimestampMicros: java.time.LocalDateTime, localTimestampMillis: java.time.LocalDateTime)
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package example.shanested.foo

import example.shanested.{Prop, Sha256}

final case class HashRecord(my_hash: Sha256, prop: example.shanested.Prop)
final case class HashRecord(my_hash: example.shanested.Sha256, prop: example.shanested.Prop)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** MACHINE-GENERATED FROM AVRO SCHEMA. DO NOT EDIT DIRECTLY */
package example.sharecord

final case class HashRecord(my_hash: Sha256)
final case class HashRecord(my_hash: example.sharecord.Sha256)
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class SpecificFileToFileSpec extends Specification {
correctly generate logical types with custom date, timestamp and time types $e27
correctly generate a protocol with special strings $e28
correcty generate a simple case class with a wildcarded custom namespace $e29
correctly generate a simple case class with a wildcarded custom namespace $e29
correctly handle namespaces for complex types $e30
"""
// correctly generate logical types from IDL $e26

Expand Down Expand Up @@ -433,4 +434,19 @@ class SpecificFileToFileSpec extends Specification {
sourceTrait === util.Util.readFile("avrohugger-core/src/test/expected/specific/example/protocol/Mail.scala")
sourceRecord === util.Util.readFile("avrohugger-core/src/test/expected/specific/example/protocol/Message.scala")
}

def e30 = {
val infile = new java.io.File("avrohugger-core/src/test/avro/fixed_two.avsc")
val gen = new Generator(SpecificRecord)
val outDir = gen.defaultOutputDir + "/specific/"
gen.fileToFile(infile, outDir)

val source1 = util.Util.readFile("target/generated-sources/specific/fixedtwo/FixedTwo.scala")
val source2 = util.Util.readFile("target/generated-sources/specific/fixedtwo/one/fixed.scala")
val source3 = util.Util.readFile("target/generated-sources/specific/fixedtwo/two/fixed.scala")

source1 === util.Util.readFile("avrohugger-core/src/test/expected/specific/example/fixedtwo/FixedTwo.scala") and
source2 === util.Util.readFile("avrohugger-core/src/test/expected/specific/example/fixedtwo/one/fixed.scala") and
source3 === util.Util.readFile("avrohugger-core/src/test/expected/specific/example/fixedtwo/two/fixed.scala")
}
}
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lazy val avroVersion = "1.11.3"

lazy val commonSettings = Seq(
organization := "com.julianpeeters",
version := "2.8.1",
version := "2.8.2",
ThisBuild / versionScheme := Some("semver-spec"),
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature"),
Test / scalacOptions ++= Seq("-Yrangepos"),
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.9.7
sbt.version=1.9.8

0 comments on commit e099ae6

Please sign in to comment.