-
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dbb52b8
Migrate schemaParser property to avroSchemaParser task
sisidra 7d1a4c5
parser builder
RustedBones 28c0f30
Update contributors
RustedBones b43acad
Remove trace leftover
RustedBones 6e4468d
Merge pull request #1 from RustedBones/parser-builder
sisidra 95ede36
Remove redundant properties and schemaParser values
sisidra 974c82c
Use scala collection as argument type for DefaultSchemaParserBuilder
sisidra aa099ea
Update README
RustedBones 5e871b0
Update version to 3.0.0-SNAPSOT
RustedBones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package sbtavro | ||
|
||
import com.spotify.avro.mojo.SchemaParserBuilder | ||
import org.apache.avro.Schema | ||
import scala.collection.JavaConverters._ | ||
|
||
case class DefaultSchemaParserBuilder(types: Iterable[Schema], | ||
validate: Boolean, | ||
validateDefaults: Boolean) | ||
extends SchemaParserBuilder { | ||
|
||
override def build(): Schema.Parser = { | ||
val parser = new Schema.Parser | ||
parser.addTypes(types.map(el => el.getFullName() -> el).toMap.asJava) | ||
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.values().asScala, | ||
template.getValidate, | ||
template.getValidateDefaults | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
)) |
34 changes: 34 additions & 0 deletions
34
src/sbt-test/sbt-avro/avscparser/project/AnnotateWithArtifactSchemaParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.3.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
src/sbt-test/sbt-avro/avscparser/src/test/scala/sbtavro/AvscParserSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 :))