Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better support for parameterised queries in @BigQueryType.fromQuery
- Loading branch information
Showing
5 changed files
with
113 additions
and
14 deletions.
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
41 changes: 41 additions & 0 deletions
41
scio-coders-macros/src/main/scala/com/spotify/scio/coders/Foo.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,41 @@ | ||
package com.spotify.scio.coders | ||
|
||
import scala.collection.mutable | ||
import scala.collection.mutable.ListBuffer | ||
import scala.language.experimental.macros | ||
import scala.reflect.macros._ | ||
|
||
object FooMacros { | ||
def printf(format: String, params: Any*): Unit = macro printfImpl | ||
|
||
def printfImpl(c: whitebox.Context)(format: c.Expr[String], | ||
params: c.Expr[Any]*): c.Expr[Unit] = { | ||
import c.universe._ | ||
val Literal(Constant(s_format: String)) = format.tree | ||
|
||
val evals = ListBuffer[Tree]() | ||
def precompute(value: Tree, tpe: Type): Ident = { | ||
val freshName = TermName(c.freshName("query$")) | ||
evals += q"$freshName: $tpe" | ||
Ident(freshName) | ||
} | ||
|
||
val paramsStack = mutable.Stack[Tree](params.map(_.tree): _*) | ||
val refs = s_format.split("(?<=%[\\w%])|(?=%[\\w%])") map { | ||
case "%d" => precompute(paramsStack.pop, typeOf[Int]) | ||
case "%s" => precompute(paramsStack.pop, typeOf[String]) | ||
case "%%" => Literal(Constant("%")) | ||
case part => Literal(Constant(part)) | ||
} | ||
|
||
println(q""" | ||
def asd(..$evals): String = "" | ||
""") | ||
|
||
// val stats = evals ++ refs.map(ref => reify(print(c.Expr[Any](ref).splice)).tree) | ||
// val asd = c.Expr[Unit](Block(stats.toList, Literal(Constant(())))) | ||
// println(asd.toString()) | ||
|
||
c.Expr[Unit](Block(List.empty, Literal(Constant(())))) | ||
} | ||
} |
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,6 @@ | ||
package com.spotify.scio | ||
|
||
object Foo extends App { | ||
import com.spotify.scio.coders.FooMacros._ | ||
printf("hello %s %s!", "asd", "qwe") | ||
} |