Skip to content

Commit

Permalink
Add missing BQ methods, expand IntelliJ range, bump to 0.1.24 (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
kellen authored Oct 25, 2023
1 parent f23a9f2 commit 4aadc19
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.5.9"
version = "3.7.15"

runner.dialect = scala213source3

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ Inside IntelliJ, `Preferences` -> `Plugins` -> `Browse repositories ...` and sea
## Build locally:

```bash
sbt packageArtifact
sbt packageArtifactZip
```

Zipped and ready to install plugin is inside `target`.
To install, inside IntelliJ: `Preferences` -> `Plugins` -> `Install plugin from disk`
To install, inside IntelliJ: `Settings` -> `Plugins` -> `Gear Icon` -> `Install plugin from disk`

# Usage

Expand All @@ -47,7 +47,7 @@ which sporadically take a while to propagate.

# Logging

This pluging uses IDEA diagnostic logger, you can find log files
This plugin uses IDEA diagnostic logger, you can find log files
under standard IntelliJ directory ([doc](https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs)). For example for macOS: `~/Library/Logs/<PRODUCT><VERSION>/idea.log`.

If there is error level message logged, it will show up in IntelliJ Event Log.
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

lazy val Guava = "com.google.guava" % "guava" % "30.1.1-jre"
lazy val Guava = "com.google.guava" % "guava" % "32.1.3-jre"
lazy val Scalatest = "org.scalatest" %% "scalatest" % "3.2.17"

lazy val commonSettings = Def.settings(
Expand Down Expand Up @@ -48,7 +48,7 @@ lazy val commonSettings = Def.settings(
lazy val ideaSettings = Def.settings(
ThisBuild / intellijPluginName := "scio-idea",
ThisBuild / intellijPlatform := IntelliJPlatform.IdeaCommunity,
ThisBuild / intellijBuild := "213.6461.79",
ThisBuild / intellijBuild := "232.10072.27",
intellijPlugins += "org.intellij.scala".toPlugin
)

Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.9.6
sbt.version = 1.9.7
6 changes: 3 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<idea-plugin>
<id>com.spotify.scio-idea</id>
<name>Scio IDEA</name>
<version>0.1.23</version>
<vendor>Spotify</vendor>
<version>0.1.24</version>
<vendor url="https://github.com/spotify/scio-idea-plugin">Spotify</vendor>

<description>IntelliJ IDEA plugin for Scio - https://github.com/spotify/scio</description>

<idea-version since-build="203.0" until-build="213.*" />
<idea-version since-build="203.0" until-build="232.*" />

<depends>com.intellij.modules.java</depends>
<depends>org.intellij.scala</depends>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ object AnnotationTypeInjector {
*/
def hash(owner: String, srcFile: String): String =
Hashing
.murmur3_32()
.murmur3_32_fixed()
.newHasher()
.putString(owner, Charsets.UTF_8)
.putString(srcFile, Charsets.UTF_8)
Expand Down
18 changes: 12 additions & 6 deletions src/main/scala/com/spotify/scio/AvroTypeInjector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ final class AvroTypeInjector extends AnnotationTypeInjector {
override def injectFunctions(source: ScTypeDefinition): Seq[String] =
source match {
case c: ScClass if avroAnnotation(c).isDefined =>
val parent = c.containingClass.getQualifiedName.init
val caseClasses = generatedCaseClasses(parent, c).find(_.contains(CaseClassSuper))
val result = for {
cc <- Option(c.containingClass)
qn <- Option(cc.getQualifiedName)
parent = qn.init
defs <- {
generatedCaseClasses(parent, c)
.find(_.contains(CaseClassSuper))
.map(getApplyPropsSignature)
.map(v => s"def $v = ???")
}
} yield defs

caseClasses
.map(getApplyPropsSignature)
.map(v => s"def $v = ???")
.toSeq
result.toSeq
case _ => Seq.empty
}

Expand Down
59 changes: 51 additions & 8 deletions src/main/scala/com/spotify/scio/BigQueryTypeInjector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package com.spotify.scio

import com.intellij.openapi.diagnostic.Logger
import org.jetbrains.plugins.scala.lang.psi.api.base.ScLiteral
import org.jetbrains.plugins.scala.lang.psi.api.base.literals.{ScBooleanLiteral, ScIntegerLiteral}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScClass, ScTypeDefinition}
import org.jetbrains.plugins.scala.lang.psi.impl.base.literals.ScIntegerLiteralImpl

object BigQueryTypeInjector {
private val Log = Logger.getInstance(classOf[BigQueryTypeInjector])
Expand Down Expand Up @@ -46,13 +49,47 @@ object BigQueryTypeInjector {
.map(_.getText)
.find(t => BigQueryAnnotations.exists(t.contains))

private def bqQuerySignature(sc: ScClass): Option[String] = {
sc.annotations.iterator
.find(sa => sa.getText.contains(FromQuery))
.flatMap { sa =>
sa.annotationExpr.getAnnotationParameters.toList match {
case Nil => None // no args
case _ :: Nil => None // only a query
case _ :: tail =>
// query with args
val optResult = tail
.foldLeft(Option((0, List.empty[String]))) {
case (res @ None, _) => res
case (Some((idx, acc)), paramLiteral: ScLiteral) =>
val tpe = "_root_." + paramLiteral.getValue().getClass.getCanonicalName
Some((idx + 1, s"queryArg$$${idx}: ${tpe}" :: acc))
case _ => None
}
optResult.map { case (_, params) => params.reverse.mkString(", ") }
}
}
}

private def fetchExtraBQTypeCompanionMethods(source: ScTypeDefinition, c: ScClass): String = {
val annotation = bqAnnotation(c).getOrElse("")
Log.debug(s"Found $annotation in ${source.getQualifiedNameForDebugger}")

annotation match {
case a if a.contains(FromQuery) =>
"def query: _root_.java.lang.String = ???"
val simple = """
|def query: _root_.java.lang.String = ???
|def queryRaw: _root_.java.lang.String = ???
|""".stripMargin

bqQuerySignature(c)
.map { params =>
simple + s"""
|def query($params): _root_.java.lang.String = ???
|def queryAsSource($params): _root_.com.spotify.scio.bigquery.Query = ???
|""".stripMargin
}
.getOrElse(simple)
case a if a.contains(FromTable) =>
"def table: _root_.java.lang.String = ???"
case a if a.contains(FromStorage) =>
Expand All @@ -75,13 +112,19 @@ final class BigQueryTypeInjector extends AnnotationTypeInjector {
override def injectFunctions(source: ScTypeDefinition): Seq[String] =
source match {
case c: ScClass if bqAnnotation(c).isDefined =>
val parent = c.containingClass.getQualifiedName.init
val caseClasses = generatedCaseClasses(parent, c).find(_.contains(CaseClassSuper))

caseClasses
.map(getApplyPropsSignature)
.getOrElse(Seq.empty[String])
.map(v => s"def $v = ???")
val result = for {
cc <- Option(c.containingClass)
qn <- Option(cc.getQualifiedName)
parent = qn.init
defs <- {
generatedCaseClasses(parent, c)
.find(_.contains(CaseClassSuper))
.map(getApplyPropsSignature)
.map(v => s"def $v = ???")
}
} yield defs

result.toSeq
case _ => Seq.empty
}

Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "0.1.23"
ThisBuild / version := "0.1.24"

0 comments on commit 4aadc19

Please sign in to comment.