Skip to content

Instantly share code, notes, and snippets.

View blast-hardcheese's full-sized avatar

Devon Stewart blast-hardcheese

View GitHub Profile
@blast-hardcheese
blast-hardcheese / Changeset.diff
Created May 27, 2022 05:23
guardrail sbt extensibility example
diff --git a/target/scala-2.13/src_managed/main/example/client/definitions/Pet.scala b/target/scala-2.13/src_managed/main/example/client/definitions/Pet.scala
index 14c35aa..2200d91 100644
--- a/target/scala-2.13/src_managed/main/example/client/definitions/Pet.scala
+++ b/target/scala-2.13/src_managed/main/example/client/definitions/Pet.scala
@@ -8,7 +8,7 @@ import io.circe._
import io.circe.syntax._
import cats.implicits._
import _root_.example.client.Implicits._
-case class Pet(category: Option[Category] = None, id: Option[Long] = None, name: String = "Fluffy", photoUrls: Vector[String] = Vector.empty, status: Option[Pet.Status] = None, tags: Option[_root_.scala.Vector[Tag]] = None)
+case class Pet(category: Option[Category], id: Option[Long], name: String, photoUrls: Vector[String], status: Option[Pet.Status], tags: Option[_root_.scala.Vector[Tag]])
@blast-hardcheese
blast-hardcheese / guardrail-http4s-BasicAuth-example.diff
Last active December 26, 2021 01:17
guardrail http4s basic authentication example
diff --git a/modules/sample-http4s/src/test/scala/core/Http4s/Http4sRoundTripTest.scala b/modules/sample-http4s/src/test/scala/core/Http4s/Http4sRoundTripTest.scala
index b0641e74..d8cf827f 100644
--- a/modules/sample-http4s/src/test/scala/core/Http4s/Http4sRoundTripTest.scala
+++ b/modules/sample-http4s/src/test/scala/core/Http4s/Http4sRoundTripTest.scala
@@ -3,6 +3,7 @@ package core.Http4s
import java.security.MessageDigest
import java.util.Locale.US
+import cats.data.Kleisli
import _root_.examples.client.http4s.pet.PetClient
@blast-hardcheese
blast-hardcheese / cli.md
Last active October 15, 2021 17:28
guardrail repackaging notes

Classes that have been moved to cli with no repackaging:

Classes that have been moved to cli as well as repackaged:

dev/guardrail/CLI$.class -> dev/guardrail/cli/CLI$.class
dev/guardrail/CLI.class -> dev/guardrail/cli/CLI.class
dev/guardrail/CLICommon$.class -> dev/guardrail/cli/CLICommon$.class

dev/guardrail/CLICommon.class -> dev/guardrail/cli/CLICommon.class

@blast-hardcheese
blast-hardcheese / CheckThat.java
Last active September 2, 2021 07:17
Combinator-style assertion "library", in Scala and Java
import java.util.function.Function;
import java.util.Arrays;
class CheckThat<A> {
// Wrap a simple assertion
Function<A, Boolean> func;
public CheckThat(Function<A, Boolean> func) {
this.func = func;
}
package bug.example
import org.scalatest.funsuite.AnyFunSuite
class SetSuite extends AnyFunSuite {
test("An empty Set should have size 0") {
// Option.empty[String].contains(1234) // WARNS
assert(Set.empty.size == 0)
}
@blast-hardcheese
blast-hardcheese / 1 Infrastructure.scala
Created December 10, 2020 06:26
Higher-order monadic syntax leveraging Scala's typesystem for consistency and correctness
// Need to be able to support different AST types
class LanguageAbstraction {
type Term
type Apply
}
// Equivalent to case class Foo[A](value: A), but with two additional properties:
// - L <: LanguageAbstraction, for letting us specify the language of what's inside
// - Z , for letting us describe the type of what's inside
case class Phantom[A, L <: LanguageAbstraction, Z](value: A)
@blast-hardcheese
blast-hardcheese / client.py
Last active August 5, 2020 20:52
composable structure decoders for Python
def foo_api(cluster, realm, path, decode={200: lambda x: x}):
url='https://{cluster}.{realm}.example.com/{path}'.format(
cluster=cluster,
realm=realm,
path=path,
)
resp = requests.get(url, auth=(creds.user, creds.password))
return decode[resp.status_code](resp.content)

A problem I had with Free interpreters was seemingly being unable to call functions defined inside any given interface from sibling members to the same interface.

Recently, I converted https://github.com/twilio/guardrail to Tagless Final, and in so doing discovered a way to use sibling functions while preserving the ability to provide different implementations of those functions.

The following is a sample, note that the desired behaviour is that my overridden function in second is the desired behaviour for all attempted

@blast-hardcheese
blast-hardcheese / TypeBag.scala
Created April 20, 2020 21:59
Example code to go along with the guardrail internals talk on LanguageAbstraction from https://www.youtube.com/playlist?list=PL5ecYVRYIkSGtui_rZkXNXCRIWlTFdaGr
import java.math.BigInteger
import cats._, cats.implicits._
trait TypeBag {
type Number
type String
}
trait HighLevel extends TypeBag {
type Number = BigInteger
import scala.meta._
val classdef = q"case class Foo(..${(1 until 30).map({ i => Term.Param(Nil, Term.Name(s"a$i"), Some(Type.Name("Int")), None) }).toList})"
val exampleParameters: List[List[(Term.Param, Int)]] = (1 until 30).map({ i => param"${Term.Name(s"a$i")}: Int" }).zipWithIndex.toList.grouped(22).toList
exampleParameters.foldLeft((1, List.empty[List[Term]])) { case ((drop, acc), next) =>
val first: List[Lit.Int] = next.map({ case (_, foo) => TermLit.Int(foo) })
val rest: List[Term.Ascribe] = exampleParameters.drop(drop).flatten.map(_._1).map { case param"$_: ${Some(tpe)}" => q"_: $tpe" }