Skip to content

Commit

Permalink
Merge branch 'LeonPoon-long-string-literals'
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpeeters committed Oct 7, 2023
2 parents 68aa44d + 775712c commit 4846c9b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import treehugger.forest._
import definitions._
import treehuggerDSL._

import java.nio.charset.StandardCharsets
import scala.jdk.CollectionConverters._

// only companions, so no doc generation is required here
Expand Down Expand Up @@ -52,7 +53,10 @@ object SpecificObjectTree {
case None => OBJECTDEF(schema.getName)
}
val schemaDef = VAL(REF("SCHEMA$")) := {
(NEW(ParserClass)) APPLY(Nil) DOT "parse" APPLY(LIT(schema.toString))
NEW(ParserClass) APPLY (Nil) DOT "parse" APPLY (schema.toString match {
case schemaString if schemaString.getBytes(StandardCharsets.UTF_8).length > 65535 => LIST(schemaString.split("},\\{\"").map(LIT)) DOT "mkString" APPLY (LIT("},{\""))
case schemaString => LIT(schemaString)
})
}
val externalReader = VAL("READER$") := NEW("org.apache.avro.specific.SpecificDatumReader").APPLYTYPE(TYPE_REF(schema.getName)).APPLY(
REF(s"${schema.getFullName()}.SCHEMA$$")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ package specific

import avrohugger._
import avrohugger.format.SpecificRecord
import org.apache.avro.Schema
import org.specs2._

import java.io.File
import scala.io.Source

class SpecificStringToFileSpec extends Specification {

def is = s2"""
Expand All @@ -31,6 +35,8 @@ class SpecificStringToFileSpec extends Specification {


correctly generate a protocol with no ADT when asked $e21
correctly generate large string of embedded schema $e23

"""
// correctly generate logical types from IDL $e22
// """
Expand Down Expand Up @@ -249,4 +255,39 @@ class SpecificStringToFileSpec extends Specification {
// source === util.Util.readFile("avrohugger-core/src/test/expected/specific/example/idl/LogicalIdl.scala")
// }


def e23 = {

val min = 70000 * 2

val schema = {
val primitives = List("null", "boolean", "int", "long", "float", "double", "bytes", "string")
val fields = primitives.map(n => s"""{"name":"${n}Field","type":"$n"}""").mkString(",")
Stream.from(1).scanLeft(s"""{"type":"record","name":"massive._0","fields":[$fields]}""") { case (field, i) =>
val fields = primitives.map(n => s"""{"name":"${n}Field","type":${field.replace("_", s"_$n")}}""")
s"""{"type":"record","name":"massive._$i","fields":[${fields.mkString(",")}]}"""
}.filter(_.length > min).map(new Schema.Parser().parse(_)).find(_.toString().length > min).get
}

val schemaString = schema.toString()
val gen = Generator(format = SpecificRecord)
val outDir = gen.defaultOutputDir + "/specific/"
val outDirF = new File(outDir, "massive")
outDirF.mkdirs()
outDirF.list().foreach(new File(outDirF, _).delete())
gen.stringToFile(schemaString, outDir)

val sources = outDirF.list().map(f => new File(outDirF, f)).map { f =>
f -> {
val src = Source.fromFile(f)
try {
src.mkString("")
} finally {
src.close()
}
}
}.toMap

sources.collect { case (f, source) if source.contains("mkString") => f } must not beEmpty
}
}
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 := "1.5.6",
version := "1.5.7-SNAPSHOT",
ThisBuild / versionScheme := Some("semver-spec"),
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature"),
Test / scalacOptions ++= Seq("-Yrangepos"),
Expand Down

0 comments on commit 4846c9b

Please sign in to comment.