Skip to content

Commit

Permalink
Merge branch 'long-string-literals' of https://github.com/LeonPoon/av…
Browse files Browse the repository at this point in the history
…rohugger into LeonPoon-long-string-literals
  • Loading branch information
julianpeeters committed Oct 7, 2023
2 parents 68aa44d + d6bd62d commit 44c1949
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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,13 @@ package specific

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

import java.io.File
import scala.io.Source
import scala.language.postfixOps

class SpecificStringToFileSpec extends Specification {

def is = s2"""
Expand All @@ -31,6 +36,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 +256,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(",")
LazyList.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
}
}

0 comments on commit 44c1949

Please sign in to comment.