-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate schemaParser property to avroSchemaParser task #33
Changes from 6 commits
dbb52b8
7d1a4c5
28c0f30
b43acad
6e4468d
95ede36
974c82c
aa099ea
5e871b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.spotify.avro.mojo; | ||
|
||
import org.apache.avro.Schema; | ||
|
||
public interface SchemaParserBuilder { | ||
Schema.Parser build(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package sbtavro | ||
|
||
import java.util | ||
|
||
import com.spotify.avro.mojo.SchemaParserBuilder | ||
import org.apache.avro.Schema | ||
|
||
case class DefaultSchemaParserBuilder(types: util.Map[String, Schema], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice ! we should probably move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that makes sense. Also I will make it List instead, as key is just schema name (and not used anyways later in Avro implementation for this call :D) |
||
validate: Boolean, | ||
validateDefaults: Boolean) | ||
extends SchemaParserBuilder { | ||
|
||
override def build(): Schema.Parser = { | ||
val parser = new Schema.Parser | ||
parser.addTypes(types) | ||
parser.setValidate(validate) | ||
parser.setValidateDefaults(validateDefaults) | ||
parser | ||
} | ||
} | ||
|
||
object DefaultSchemaParserBuilder { | ||
def default(): DefaultSchemaParserBuilder = { | ||
template(new Schema.Parser()) | ||
} | ||
|
||
def template(template: Schema.Parser): DefaultSchemaParserBuilder = { | ||
DefaultSchemaParserBuilder( | ||
template.getTypes, | ||
template.getValidate, | ||
template.getValidateDefaults | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import java.util.Collections.{singletonMap, singletonList} | ||
import org.apache.avro.Schema | ||
|
||
name := "avscparser-test" | ||
|
||
libraryDependencies ++= Seq( | ||
"org.apache.avro" % "avro" % "1.10.0", | ||
"org.specs2" %% "specs2-core" % "4.9.4" % Test | ||
) | ||
|
||
avroSchemaParserBuilder := AnnotateWithArtifactSchemaParser | ||
.newBuilder(projectID.value) | ||
.copy(types = singletonMap( | ||
"B", Schema.createEnum("B", null, "com.cavorite.test.avscparser", singletonList("B1")) | ||
)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import com.spotify.avro.mojo.SchemaParserBuilder | ||
import org.apache.avro.Schema | ||
import sbt.ModuleID | ||
|
||
class AnnotateWithArtifactSchemaParser( | ||
moduleID: ModuleID, | ||
types: java.util.Map[String, Schema] | ||
) extends org.apache.avro.Schema.Parser { | ||
|
||
addTypes(types) | ||
|
||
override def parse(file: java.io.File): org.apache.avro.Schema = { | ||
val schema = super.parse(file) | ||
if (schema.getType == org.apache.avro.Schema.Type.RECORD) { | ||
schema.addProp("com.cavorite.sbt-avro.artifact", moduleID.toString()) | ||
} | ||
schema | ||
} | ||
|
||
} | ||
|
||
object AnnotateWithArtifactSchemaParser { | ||
|
||
case class Builder(moduleID: ModuleID, types: java.util.Map[String, Schema]) | ||
extends SchemaParserBuilder { | ||
|
||
override def build(): Schema.Parser = | ||
new AnnotateWithArtifactSchemaParser(moduleID, types) | ||
} | ||
|
||
def newBuilder(moduleID: ModuleID): AnnotateWithArtifactSchemaParser.Builder = | ||
new Builder(moduleID, java.util.Collections.emptyMap()) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.3.13 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
sys.props.get("plugin.version") match { | ||
case Some(x) => addSbtPlugin("com.cavorite" % "sbt-avro" % x) | ||
case _ => sys.error("""|The system property 'plugin.version' is not defined. | ||
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin) | ||
} | ||
|
||
libraryDependencies += "org.apache.avro" % "avro-compiler" % "1.10.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "A", | ||
"namespace": "com.cavorite.test.avscparser", | ||
"type": "record", | ||
"fields": [ | ||
{ | ||
"name": "supportsCustomRegisteredType", | ||
"type": "B" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package sbtavro | ||
|
||
import java.io.File | ||
|
||
import org.apache.avro.Schema | ||
import org.apache.avro.generic.GenericData.StringType | ||
import org.specs2.mutable.Specification | ||
|
||
import com.cavorite.test.avscparser.A | ||
|
||
class AvscParserSpec extends Specification { | ||
|
||
"A should have artifact property" >> { | ||
A.getClassSchema().getProp("com.cavorite.sbt-avro.artifact") == "avscparser-test:avscparser-test:0.1.0-SNAPSHOT" | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
> compile | ||
|
||
$ exists target/scala-2.12/src_managed/main/compiled_avro/com/cavorite/test/avscparser/A.java | ||
$ exists target/scala-2.12/src_managed/main/compiled_avro/com/cavorite/test/avscparser/B.java | ||
|
||
> test | ||
|
||
> clean | ||
|
||
> compile |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not keeping the
withTypes
,withValidate
andwithValidateDefaults
since allSchema.Parser
have to support those ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When users implement their own builder, they most likely set those values themselves within it.
The default implementation still has ability for setting validations.
This approach allows for smaller api footprint allowing more flexibility how users want to implement builder, and not restricting us to future changes in Avro API (adding more validations, removing some?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then again - adding those function would allow easier control over validations for users if org infra team would provide custom builder (as I imagine it would be for us at Spotify :))