Skip to content

Commit

Permalink
Add 0.14 scalafix migration for saveAsAvroFile and update avro coder one
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Jan 30, 2024
1 parent 6b44dd2 commit 21f6e3f
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 38 deletions.
3 changes: 1 addition & 2 deletions scalafix/build.sbt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sbt.librarymanagement.VersionNumber.SemVer

lazy val V = _root_.scalafix.sbt.BuildInfo

inThisBuild(
List(
resolvers ++= Resolver.sonatypeOssRepos("snapshots"),
organization := "com.spotify",
scalaVersion := V.scala212,
scalacOptions ++= List("-Yrangepos"),
Expand Down
21 changes: 21 additions & 0 deletions scalafix/input-0_14/src/main/scala/fix/v0_14_0/AvroRecords.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
rule = FixAvroCoder
*/
package fix.v0_14_0

import org.apache.avro.Schema
import org.apache.avro.specific.{SpecificFixed, SpecificRecord}

import java.io.{ObjectInput, ObjectOutput}

class A extends SpecificRecord {
override def put(i: Int, v: Any): Unit = ???
override def get(i: Int): AnyRef = ???
override def getSchema: Schema = ???
}

class B extends SpecificFixed {
override def getSchema: Schema = ???
override def writeExternal(out: ObjectOutput): Unit = ???
override def readExternal(in: ObjectInput): Unit = ???
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ rule = FixAvroCoder
package fix.v0_14_0

import com.spotify.scio.coders.Coder.{avroGenericRecordCoder, avroSpecificFixedCoder, avroSpecificRecordCoder}
import org.apache.avro.specific.SpecificFixed
import org.apache.avro.{Schema => AvroSchema}
import java.io.{ObjectInput, ObjectOutput}

class B extends SpecificFixed {
override def getSchema: AvroSchema = ???
override def writeExternal(out: ObjectOutput): Unit = ???
override def readExternal(in: ObjectInput): Unit = ???
}

object FixAvroCoder1 {
val schema: AvroSchema = ???
avroGenericRecordCoder
avroGenericRecordCoder(schema)
avroSpecificRecordCoder[A]
avroSpecificFixedCoder[B]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import com.spotify.scio.coders.Coder
import org.apache.avro.generic.GenericRecord

object FixAvroCoder5 {

implicit val c = Coder.avroGenericRecordCoder

def foo[T : Coder]() = ???
val c = foo[GenericRecord]
val r = foo[GenericRecord]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import com.spotify.scio.coders.Coder
import org.apache.avro.generic.GenericRecord

object FixAvroCoder8 {
val c = Coder[GenericRecord]
implicit val c = Coder.avroGenericRecordCoder
val r = Coder[GenericRecord]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ package fix.v0_14_0

import com.spotify.scio.schemas.Schema
import org.apache.avro.{Schema => AvroSchema}
import org.apache.avro.specific.SpecificRecord

class A extends SpecificRecord {
override def put(i: Int, v: Any): Unit = ???
override def get(i: Int): AnyRef = ???
override def getSchema: AvroSchema = ???
}

object FixAvroSchemasPackage1 {
val schema: AvroSchema = ???
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
rule = FixGenericAvro
*/
package fix.v0_14_0

import com.spotify.scio.avro._
import com.spotify.scio.values.SCollection
import org.apache.avro.Schema
import org.apache.avro.generic.GenericRecord

object FixGenericAvro {

val path: String = ???
val numShards: Int = ???
val schema: Schema = ???
val suffix: String = ???

val sc: SCollection[GenericRecord] = ???


sc.saveAsAvroFile(path, numShards, schema)
sc.saveAsAvroFile(path, numShards, schema, suffix)
sc.saveAsAvroFile(path, numShards, suffix = suffix, schema = schema)

// should not be touched
sc.saveAsAvroFile(path, schema = schema, numShards = numShards)
sc.saveAsAvroFile(path, schema = schema)
sc.saveAsAvroFile(numShards = numShards, schema = schema, path = path)

}
19 changes: 19 additions & 0 deletions scalafix/output-0_14/src/main/scala/fix/v0_14_0/AvroRecords.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

package fix.v0_14_0

import org.apache.avro.Schema
import org.apache.avro.specific.{SpecificFixed, SpecificRecord}

import java.io.{ObjectInput, ObjectOutput}

class A extends SpecificRecord {
override def put(i: Int, v: Any): Unit = ???
override def get(i: Int): AnyRef = ???
override def getSchema: Schema = ???
}

class B extends SpecificFixed {
override def getSchema: Schema = ???
override def writeExternal(out: ObjectOutput): Unit = ???
override def readExternal(in: ObjectInput): Unit = ???
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@

package fix.v0_14_0

import org.apache.avro.specific.SpecificFixed
import org.apache.avro.{Schema => AvroSchema}
import java.io.{ObjectInput, ObjectOutput}
import com.spotify.scio.avro._

class B extends SpecificFixed {
override def getSchema: AvroSchema = ???
override def writeExternal(out: ObjectOutput): Unit = ???
override def readExternal(in: ObjectInput): Unit = ???
}

object FixAvroCoder1 {
val schema: AvroSchema = ???
avroGenericRecordCoder
avroGenericRecordCoder(schema)
avroSpecificRecordCoder[A]
avroSpecificFixedCoder[B]
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import org.apache.avro.generic.GenericRecord
import com.spotify.scio.avro._

object FixAvroCoder5 {

implicit val c = avroGenericRecordCoder

def foo[T : Coder]() = ???
val c = foo[GenericRecord]
val r = foo[GenericRecord]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import org.apache.avro.generic.GenericRecord
import com.spotify.scio.avro._

object FixAvroCoder8 {
val c = Coder[GenericRecord]
implicit val c = avroGenericRecordCoder
val r = Coder[GenericRecord]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ package fix.v0_14_0

import com.spotify.scio.schemas.Schema
import org.apache.avro.{Schema => AvroSchema}
import org.apache.avro.specific.SpecificRecord
import com.spotify.scio.avro.schemas._

class A extends SpecificRecord {
override def put(i: Int, v: Any): Unit = ???
override def get(i: Int): AnyRef = ???
override def getSchema: AvroSchema = ???
}

object FixAvroSchemasPackage1 {
val schema: AvroSchema = ???
// direct usage should be converted
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

package fix.v0_14_0

import com.spotify.scio.avro._
import com.spotify.scio.values.SCollection
import org.apache.avro.Schema
import org.apache.avro.generic.GenericRecord

object FixGenericAvro {

val path: String = ???
val numShards: Int = ???
val schema: Schema = ???
val suffix: String = ???

val sc: SCollection[GenericRecord] = ???


sc.saveAsAvroFile(path, schema, numShards)
sc.saveAsAvroFile(path, schema, numShards, suffix)
sc.saveAsAvroFile(path, numShards = numShards, suffix = suffix, schema = schema)

// should not be touched
sc.saveAsAvroFile(path, schema = schema, numShards = numShards)
sc.saveAsAvroFile(path, schema = schema)
sc.saveAsAvroFile(numShards = numShards, schema = schema, path = path)

}
2 changes: 1 addition & 1 deletion scalafix/project/Scio.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ object Scio {
val `0.12` = "0.12.0"
val `0.13` = "0.13.0"
// TODO update
val `0.14` = "0.14.0-dev1"
val `0.14` = "0.14-a1600a4-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ fix.v0_13_0.FixTaps
fix.v0_14_0.FixAvroSchemasPackage
fix.v0_14_0.FixAvroCoder
fix.v0_14_0.FixDynamicAvro
fix.v0_14_0.FixGenericAvro

35 changes: 35 additions & 0 deletions scalafix/rules/src/main/scala/fix/v0_14_0/FixGenericAvro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package fix.v0_14_0

import scalafix.v1._

import scala.meta._

object FixGenericAvro {
private val AvroGenericSaveMatcher = SymbolMatcher.normalized(
"com/spotify/scio/avro/syntax/GenericRecordSCollectionOps#saveAsAvroFile"
)
}

class FixGenericAvro extends SemanticRule("FixGenericAvro") {
import FixGenericAvro._

override def fix(implicit doc: SemanticDocument): Patch = {
doc.tree.collect {
case t @ q"$fn(..$params)" if AvroGenericSaveMatcher.matches(fn.symbol) =>
params.toList match {
case _ :: q"$_ = $_" :: _ =>
// 2nd params is already named
Patch.empty
case path :: numShards :: (next @ q"$name = $_") :: tail if name.syntax != "schema" =>
// 3rd param is named but not schema
val named = path :: q"numShards = $numShards" :: next :: tail
Patch.replaceTree(t, q"$fn(..$named)".syntax)
case path :: numShards :: schema :: tail =>
val reordered = path :: schema :: numShards :: tail
Patch.replaceTree(t, q"$fn(..$reordered)".syntax)
case _ =>
Patch.empty
}
}.asPatch
}
}

0 comments on commit 21f6e3f

Please sign in to comment.