Skip to content

sbt plugin for generating Scala sources for Apache Avro schemas and protocols.

License

Notifications You must be signed in to change notification settings

s-gelazevicius/sbt-avrohugger

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sbt-avrohugger

Join the chat at https://gitter.im/julianpeeters/avrohugger

sbt plugin for generating Scala case classes and ADTs from Apache Avro schemas, datafiles, and protocols.

Install the plugin

Add the following lines to the file myproject/project/plugins.sbt in your project directory:

addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "0.15.1")

Import the plugin settings

To activate the plugin, import its settings by adding one of the following lines to your myproject/build.sbt file:

To get the 'generate' task for generating standard Scala Case Classes use:

sbtavrohugger.SbtAvrohugger.avroSettings

To get the generate-specific task for generating Scala Case Classes that are compatible with the Avro Specific API, use:

sbtavrohugger.SbtAvrohugger.specificAvroSettings

To get the generate-scavro task for generating Scala Case Class wrapper classes (Java generated classes supplied separately) for use with Scavro, use:

sbtavrohugger.SbtAvrohugger.scavroSettings

Scope

All settings and tasks are in the avro scope. E.g., to execute the generate task directly, just run avro:generate.

Settings

Name Name in shell Default Description
sourceDirectory source-directory src/main/avro Path containing *.avsc, *.avdl, and/or *.avro files.
scalaSource scala-source $sourceManaged/main/compiled_avro Path for the generated *.scala or *.java files.
avroScalaCustomTypes avro-scala-custom-types Map.empty[String, Class[_]] Map for reassigning array to Array, List, or Seq.
avroScalaCustomNamespace avro-scala-custom-namespace Map.empty[String, String] Map for reassigning namespaces.
avroScalaCustomEnumStyle avro-scala-custom-enum-style Map.empty[String, String] Map for reassigning enum style to java enum or case object.

Changing Settings

Settings can be overridden by adding lines to myproject/build.sbt:

(scalaSource in avroConfig) := new java.io.File("myscalaSource")

avro-scala-custom-types and avro-scala-custom-namespace require additional imports (I'm not sure why these aren't picked up with the other settings, anybody know why we have to import them separately?):

import sbtavrohugger.AvrohuggerSettings.{
  avroScalaCustomTypes,
  avroScalaCustomNamespace,
  avroScalaCustomEnumStyle
}

(avroScalaCustomTypes in avroConfig) := Map("array"->classOf[Array[_]])

(avroScalaCustomNamespace in avroConfig) := Map("example"->"overridden")

(avroScalaCustomEnumStyle in avroConfig) := Map("enum"->"java enum")

Tasks

Each task is automatically executed every time the project is compiled.*

  • as of Intellij IDEA 14.1.4, and possibly for other IDEs, the task must be run manually from the integrated Terminal: avro:generate.
Name Name in shell Description
generate generate Compiles the Avro files into Scala case classes.
generateSpecific generate-specific Compiles the Avro files into Scala case classes implementing SpecificRecord.
generateScavro generate-scavro Compiles the Avro files into Scala case class Scavro wrapper classes.

Datatypes

Supports generating case classes with arbitrary fields of the following datatypes:

  • INT → Int
  • LONG → Long
  • FLOAT → Float
  • DOUBLE → Double
  • STRING → String
  • BOOLEAN → Boolean
  • NULL → Null
  • MAP → Map
  • ENUM → generate: scala.Enumeration, generate-specific: Java Enum
  • BYTES → Array[Byte]
  • FIXED → //TODO
  • ARRAY → List (generate-scavro: Array). To reassign, please see Settings above.
  • UNION → Option
  • RECORD → case class

Future

  • support for more avro datatypes
  • decimal support for avrohugger via logical types
  • more codegen situations, e.g. exploding Spark Rows?

Credits

sbt-avrohugger is based on sbt-avro by Juan Manuel Caicedo, and depends on avrohugger.

Contributors

Fork away, just make sure the tests pass before you send a pull request.

Criticism is appreciated.

About

sbt plugin for generating Scala sources for Apache Avro schemas and protocols.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Scala 100.0%