Skip to content

Instantly share code, notes, and snippets.

@deusaquilus
deusaquilus / LayerProject.scala
Last active March 12, 2021 06:42
Various Zio Bracketing Functions: TODO Check if these work in Dotty
// Provide a single Environment Has clause:
// ZLayer[Orig, E, Has[From] with Rest] => ZLayer[Orig, E, Has[To] with Rest]
def projectionWithEffect[From: Tag, To: Tag, Rest <: Has[_]: Tag, E](effect: (From, Rest) => IO[E, To]): ZLayer[Has[From] with Rest, E, Has[To] with Rest] =
(for {
from <- ZIO.service[From]
rest <- ZIO.environment[Rest]
result <- effect(from, rest)
} yield Has(result) ++ rest).toLayerMany
@deusaquilus
deusaquilus / search_maven_gav.sh
Created January 5, 2021 04:35
Get maven artifacts with coordinates from central
#!/bin/bash
group=$1
artifact=$2
version=$3
# Diff like this:
# diff <(search_maven_gav.sh io.getquill 'quill-*_2.13' 3.6.0-RC2 | awk '{print $2}' | sort) <(search_maven_gav.sh io.getquill 'quill-*_2.13' 3.6.0-RC3 | awk '{print $2}' | sort)
curl -s "https://search.maven.org/solrsearch/select?rows=100&q=g:${group}+AND+a:${artifact}+AND+v:${version}" | jq --raw-output '.response.docs[] | [.g, .a, .v]|@tsv'
@deusaquilus
deusaquilus / Example.scala
Created December 10, 2020 20:28
Grouping into an Array with Quill Spark
case class Address(street: String, zip: Int, personName: String)
case class Person(name: String, addresses: List[String])
/*
In some Data-Models used with Spark, you may want to embed arrays into an object.
You may then want to be able to set array-properties of this kind of object when
a group-by is done. Normally this would be done in Spark using the following way
*/
val addresses: Dataset[Address] = ???
val people = addresses.groupByKey(a => a.personName).mapGroups { case (name, addresses) =>
@deusaquilus
deusaquilus / build.sbt
Created September 8, 2020 18:24
Add Snapshot OSS Quill to Build.sbt
resolvers ++= Seq(
("OSS-Snapshots" at "https://oss.sonatype.org/service/local/repositories/snapshots/content/")
)
libraryDependencies ++= Seq(
"io.getquill" %% "quill-sql" % "3.5.3-SNAPSHOT",
)
@deusaquilus
deusaquilus / ExpandJoin.scala
Last active August 5, 2020 15:33
Better Typed ExpandJoin which uses FlatMap/FlatJoin
package io.getquill.sql.norm
import io.getquill.ast._
import io.getquill.ast.Implicits._
import io.getquill.norm.BetaReduction
import io.getquill.norm.TypeBehavior.{ ReplaceWithReduction => RWR }
import io.getquill.norm.Normalize
import io.getquill.quat.Quat
// def nestedWithRenames(): Unit = {
@deusaquilus
deusaquilus / MakeQuat.scala
Created July 19, 2020 06:22
Runtime Entity Quat Using Java Reflection
/**
* Only runtime, only for Direct entity Types. Do not support things with generic parameters like Query[T] etc...
* This is needed because the quill-core/quill-sql JS use Scala JS which does not support TypeTags via the Dynamic Query API,
* (the Macro API is fine to use them).
*/
object RuntimeEntityQuat {
def apply[T](implicit ct: ClassTag[T]): Quat =
forClassTopLevel(ct.runtimeClass)
@deusaquilus
deusaquilus / ApplyMap.scala
Last active June 18, 2020 09:05
ApplyMap via Gin and Tonic
// ********** Before: **********
// a.map(b => c).*join(d).on((iA, iB) => on)
// a.*join(d).on((b, iB) => on[iA := c]).map(t => (c[b := t._1], t._2))
// Quat Equivalence: a.quat == b.quat == iA.quat
case Join(tpe, DetachableMap(a, b, c), d, iA, iB, on) =>
val onr = BetaReduction(on, iA -> c)
val t = Ident("t")
val t1 = BetaReduction(c, b -> Property(t, "_1"))
val t2 = Property(t, "_2")
Some(Map(Join(tpe, a, d, b, iB, onr), t, Tuple(List(t1, t2))))
@deusaquilus
deusaquilus / FieldCollector.scala
Last active January 15, 2020 08:44
Matching MirroredElemLabels and MirroredElemTypes at the same time
trait FieldCollector[T] {
def collect(fields: Collector): Collector
}
object FieldCollector {
inline def collectFromChild[T](fields: ArrayBuffer[String]): Collector =
summonFrom {
case fc: FieldCollector[T] => fc.collect(fields)
}
@deusaquilus
deusaquilus / Example.scala
Created August 15, 2019 16:21
Potential Need for PropertySet in ExpandNestedQueries
// When doing a query like this (this is in the unit tests)
val q = quote {
qr1.leftJoin(qr2).on {
(a, b) =>
a.i == b.i
}.filter {
ab => {
val (a, b) = ab
b.map(bv => bv.l).contains(3L)
}
@deusaquilus
deusaquilus / gist:b3556a72571a142f40b364de60fcc397
Created August 15, 2019 16:07
Scalatest - Compare Quill AST Opinions Neutralized
implicit def astEq[T <: io.getquill.ast.Ast](implicit tt: scala.reflect.runtime.universe.TypeTag[T]) =
new Equality[T] {
def areEqual(a: T, b: Any): Boolean =
b match {
case p: io.getquill.ast.Ast => p.neutralize == a.neutralize
case _ => false
}
}