Skip to content
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

Update GridFS rules #2

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion input/src/main/scala/fix/gridfs/RequireMigration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ import com.github.ghik.silencer.silent
import scala.concurrent.ExecutionContext

import reactivemongo.api.BSONSerializationPack
import reactivemongo.api.gridfs.GridFS
import reactivemongo.api.gridfs.{
BasicMetadata,
ComputedMetadata,
CustomMetadata,
DefaultFileToSave,
DefaultReadFile,
GridFS
}

import play.modules.reactivemongo.JSONFileToSave

object RequireMigration {
@silent
Expand All @@ -22,4 +31,13 @@ object RequireMigration {

@silent
def iterateeWithMD5(gridfs: GridFS[BSONSerializationPack.type])(implicit ec: ExecutionContext) = gridfs.iterateeWithMD5(???, ???)(???, ???, ???, ???)

def foo(
fileToSave: DefaultFileToSave,
rf: DefaultReadFile,
jf: JSONFileToSave,
m1: BasicMetadata[_],
m2: ComputedMetadata,
m3: CustomMetadata[_]) = println(s"fileToSave = $fileToSave, $rf, $jf, $m1, $m2, $m3")

}
8 changes: 8 additions & 0 deletions input/src/main/scala/fix/play/Controller.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package fix.play

import scala.concurrent.Future

import reactivemongo.play.json.JSONSerializationPack

import reactivemongo.play.json.collection.JSONCollection

import play.modules.reactivemongo.{
MongoController,
ReactiveMongoComponents
Expand All @@ -21,4 +25,8 @@ trait Controller extends MongoController { self: ReactiveMongoComponents =>

def lorem(gfs: Future[JsGridFS]) =
gridFSBodyParser(gfs, null)(null, null, null)

def json1(coll: JSONCollection) = coll.name

def json2(pack: JSONSerializationPack.type) = pack.toString
}
10 changes: 10 additions & 0 deletions output/src/main/scala/fix/gridfs/RequireMigration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.ghik.silencer.silent
import scala.concurrent.ExecutionContext

import reactivemongo.api.gridfs.GridFS

import reactivemongo.api.bson.collection.BSONSerializationPack

object RequireMigration {
Expand All @@ -19,4 +20,13 @@ object RequireMigration {

@silent
def iterateeWithMD5(gridfs: GridFS[BSONSerializationPack.type])(implicit ec: ExecutionContext) = gridfs.iterateeWithMD5(???, ???)(???, ???, ???, ???) // Consider: gridfs.readToOutputStream or GridFS support in streaming modules

def foo(
fileToSave: Unit /* Consider: reactivemongo.api.gridfs.GridFS.fileToSave */,
rf: Unit /* Consider: reactivemongo.api.gridfs.ReadFile */,
jf: Unit /* Consider: reactivemongo.api.gridfs.ReadFile */,
m1: Unit /* Consider: reactivemongo.api.gridfs.ReadFile */,
m2: Unit /* Consider: reactivemongo.api.gridfs.ReadFile */,
m3: Unit /* Consider: reactivemongo.api.gridfs.ReadFile */) = println(s"fileToSave = $fileToSave, $rf, $jf, $m1, $m2, $m3")

}
7 changes: 7 additions & 0 deletions output/src/main/scala/fix/play/Controller.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package fix.play

import scala.concurrent.Future



import play.modules.reactivemongo.{
MongoController,
ReactiveMongoComponents
}

import com.github.ghik.silencer.silent
import MongoController.GridFS
import reactivemongo.api.bson.collection.{ BSONCollection, BSONSerializationPack }

trait Controller extends MongoController { self: ReactiveMongoComponents =>
@silent
Expand All @@ -19,4 +22,8 @@ trait Controller extends MongoController { self: ReactiveMongoComponents =>

def lorem(gfs: Future[GridFS]) =
gridFSBodyParser(gfs)(null)

def json1(coll: BSONCollection) = coll.name

def json2(pack: BSONSerializationPack.type) = pack.toString
}
247 changes: 176 additions & 71 deletions rules/src/main/scala/Upgrade.scala
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,59 @@ final class Upgrade extends SemanticRule("ReactiveMongoUpgrade") { self =>

Patch.fromIterable(patches.result())
}

case Importer(
Term.Select(
Term.Select(
Term.Select(Term.Name("reactivemongo"), Term.Name("play")),
Term.Name("json")
),
Term.Name("collection")
),
importees) => {
val patches = Seq.newBuilder[Patch]

importees.foreach {
case i @ Importee.Name(Name.Indeterminate("JSONCollection")) =>
patches += (Patch.removeImportee(i) + Patch.addGlobalImport(
Importer(
q"reactivemongo.api.bson.collection",
List(importee"BSONCollection")))).atomic

case _ =>
}

Patch.fromIterable(patches.result())
}

case Importer(
Term.Select(
Term.Select(Term.Name("reactivemongo"), Term.Name("play")),
Term.Name("json")
), importees) => {
val patches = Seq.newBuilder[Patch]

importees.foreach {
case i @ Importee.Name(Name.Indeterminate("JSONSerializationPack")) =>
patches += (Patch.removeImportee(i) + Patch.addGlobalImport(
Importer(
q"reactivemongo.api.bson.collection",
List(importee"BSONSerializationPack")))).atomic

case _ =>
}

Patch.fromIterable(patches.result())
}
},
refactor = {
case n @ Type.Name("JSONCollection") if (n.symbol.info.exists(
_.toString startsWith "reactivemongo/play/json/collection/")) =>
Patch.replaceTree(n, "BSONCollection")

case n @ Type.Singleton(Term.Name("JSONSerializationPack")) =>
Patch.replaceTree(n, "BSONSerializationPack.type")

case n @ Type.Name("JsGridFS") =>
Patch.replaceTree(n, "GridFS")

Expand Down Expand Up @@ -599,92 +650,146 @@ final class Upgrade extends SemanticRule("ReactiveMongoUpgrade") { self =>
Patch.replaceTree(u, s"${a}.getAsUnflattenedTry[reactivemongo.api.bson.BSONValue]($f)")
})

private def gridfsUpgrade(implicit doc: SemanticDocument) = Fix({
// Extractors
private def gridfsUpgrade(implicit doc: SemanticDocument) = Fix(
`import` = {
case Importer(
Term.Select(
Term.Select(Term.Name("reactivemongo"), Term.Name("api")),
Term.Name("gridfs")
), importees) => {
val patches = Seq.newBuilder[Patch]

importees.foreach {
case i @ Importee.Name(Name.Indeterminate(
"DefaultFileToSave" | "DefaultReadFile" |
"ComputedMetadata" | "BasicMetadata" | "CustomMetadata")) =>
patches += Patch.removeImportee(i)

object GridFSTermName {
def unapply(t: Term): Boolean = t match {
case g @ Term.Name("GridFS") =>
g.symbol.owner.toString == "reactivemongo/api/gridfs/"
case _ =>
}

case _ =>
false
Patch.fromIterable(patches.result())
}
}

// ---
case Importer(
Term.Select(
Term.Select(Term.Name("play"), Term.Name("modules")),
Term.Name("reactivemongo")
), importees) => {
val patches = Seq.newBuilder[Patch]

importees.foreach {
case i @ Importee.Name(Name.Indeterminate("JSONFileToSave")) =>
patches += Patch.removeImportee(i)

object ReadFileTypeLike {
def unapply(t: Type): Boolean = t match {
case Type.Name("BasicMetadata" |
"ComputedMetadata" | "CustomMetadata" | "ReadFile") =>
t.symbol.owner.toString == "reactivemongo/api/gridfs/"
case _ =>
}

case _ =>
false
Patch.fromIterable(patches.result())
}
}
},
refactor = {
// Extractors

{
case gridfsRes @ Term.Apply(
Term.ApplyType(
GridFSTermName(),
List(Type.Singleton(Term.Name("BSONSerializationPack")))
),
List(Term.Name(db))
) =>
Patch.replaceTree(gridfsRes, s"${db}.gridfs")
object GridFSTermName {
def unapply(t: Term): Boolean = t match {
case g @ Term.Name("GridFS") =>
g.symbol.owner.toString == "reactivemongo/api/gridfs/"

case gridfsRes @ Term.Apply(
Term.ApplyType(
GridFSTermName(),
List(Type.Singleton(Term.Name("BSONSerializationPack")))
),
List(Term.Name(db), prefix)
) =>
Patch.replaceTree(gridfsRes, s"${db}.gridfs($prefix)")
case _ =>
false
}
}

// ---

case readFileTpe @ Type.Apply(
ReadFileTypeLike(),
List(
Type.Singleton(Term.Name("BSONSerializationPack")),
Type.Name(idTpe)
)
) =>
Patch.replaceTree(
readFileTpe, s"ReadFile[$idTpe, reactivemongo.api.bson.BSONDocument]")
object ReadFileTypeLike {
def unapply(t: Type): Boolean = t match {
case Type.Name("BasicMetadata" |
"ComputedMetadata" | "CustomMetadata" | "ReadFile") =>
t.symbol.owner.toString == "reactivemongo/api/gridfs/"

case save @ Term.Apply(
Term.Apply(
Term.Select(g, Term.Name("save" | "saveWithMD5")),
List(Term.Name(_), Term.Name(file), Term.Name(chunkSize))
),
List(Term.Name(_), Term.Name(ec), Term.Name(_), Term.Name(_))
) if (save.symbol.owner.
toString == "reactivemongo/api/gridfs/GridFS#") =>
Patch.addRight(save, s" // Consider: ${g}.writeFromInputStream(${file}, _streamNotEnumerator, $chunkSize)($ec)")
case _ =>
false
}
}

case iteratee @ Term.Apply(
Term.Apply(
Term.Select(g, Term.Name("iteratee" | "iterateeWithMD5")),
List(Term.Name(_), Term.Name(_))
),
List(Term.Name(_), Term.Name(_), Term.Name(_), Term.Name(_))
) if (iteratee.symbol.owner.
toString == "reactivemongo/api/gridfs/GridFS#") =>
Patch.addRight(iteratee, s" // Consider: ${g}.readToOutputStream or GridFS support in streaming modules")

case gridfsRm @ Term.Apply(
Term.Select(Term.Name(gt), Term.Name("remove")),
List(Term.Name(ref))
) if (gridfsRm.symbol.owner.
toString == "reactivemongo/api/gridfs/GridFS#") =>
Patch.replaceTree(gridfsRm, s"${gt}.remove(${ref}.id)")
{
case gridfsRes @ Term.Apply(
Term.ApplyType(
GridFSTermName(),
List(Type.Singleton(Term.Name("BSONSerializationPack")))
),
List(Term.Name(db))
) =>
Patch.replaceTree(gridfsRes, s"${db}.gridfs")

}
})
case gridfsRes @ Term.Apply(
Term.ApplyType(
GridFSTermName(),
List(Type.Singleton(Term.Name("BSONSerializationPack")))
),
List(Term.Name(db), prefix)
) =>
Patch.replaceTree(gridfsRes, s"${db}.gridfs($prefix)")

// ---

case readFileTpe @ Type.Apply(
ReadFileTypeLike(),
List(
Type.Singleton(Term.Name("BSONSerializationPack")), Type.Name(idTpe))
) =>
Patch.replaceTree(
readFileTpe, s"ReadFile[$idTpe, reactivemongo.api.bson.BSONDocument]")

case save @ Term.Apply(
Term.Apply(
Term.Select(g, Term.Name("save" | "saveWithMD5")),
List(Term.Name(_), Term.Name(file), Term.Name(chunkSize))
),
List(Term.Name(_), Term.Name(ec), Term.Name(_), Term.Name(_))
) if (save.symbol.owner.
toString == "reactivemongo/api/gridfs/GridFS#") =>
Patch.addRight(save, s" // Consider: ${g}.writeFromInputStream(${file}, _streamNotEnumerator, $chunkSize)($ec)")

case iteratee @ Term.Apply(
Term.Apply(
Term.Select(g, Term.Name("iteratee" | "iterateeWithMD5")),
List(Term.Name(_), Term.Name(_))
),
List(Term.Name(_), Term.Name(_), Term.Name(_), Term.Name(_))
) if (iteratee.symbol.owner.
toString == "reactivemongo/api/gridfs/GridFS#") =>
Patch.addRight(iteratee, s" // Consider: ${g}.readToOutputStream or GridFS support in streaming modules")

case gridfsRm @ Term.Apply(
Term.Select(Term.Name(gt), Term.Name("remove")),
List(Term.Name(ref))
) if (gridfsRm.symbol.owner.
toString == "reactivemongo/api/gridfs/GridFS#") =>
Patch.replaceTree(gridfsRm, s"${gt}.remove(${ref}.id)")

case t @ Type.Name("DefaultFileToSave") if (t.symbol.info.exists(
_.toString startsWith "reactivemongo/api/gridfs/")) =>
Patch.replaceTree(t, "Unit /* Consider: reactivemongo.api.gridfs.GridFS.fileToSave */")

case t @ Type.Apply(Type.Name(
"BasicMetadata" | "CustomMetadata"), _) if (t.symbol.info.exists(
_.toString startsWith "reactivemongo/api/gridfs/")) =>
Patch.replaceTree(t, "Unit /* Consider: reactivemongo.api.gridfs.ReadFile */")

case t @ Type.Name("DefaultReadFile" | "ComputedMetadata") if (
t.symbol.info.exists(
_.toString startsWith "reactivemongo/api/gridfs/")) =>
Patch.replaceTree(t, "Unit /* Consider: reactivemongo.api.gridfs.ReadFile */")

case t @ Type.Name("JSONFileToSave") if (t.symbol.info.exists(
_.toString startsWith "play/modules/reactivemongo/")) =>
Patch.replaceTree(t, "Unit /* Consider: reactivemongo.api.gridfs.ReadFile */")

}
})

// ---

Expand Down