Skip to content

Instantly share code, notes, and snippets.

View Yotamho's full-sized avatar
😃

Yotam Hochman Yotamho

😃
  • Israel
View GitHub Profile
@Yotamho
Yotamho / zod-to-payload.ts
Created November 18, 2025 14:43
A helper that converts a Zod discriminated union into a Payload CMS "blocks" field
import { z, ZodLiteral, type ZodObject } from 'zod/v4'
import type { Field as PayloadField, Block } from 'payload'
type BlocksField = Extract<PayloadField, { type: 'blocks' }>
/**
* Convert a Zod discriminated union into a Payload "blocks" field.
* Assumptions (not validated):
* - Each option is: { [discriminator]: z.literal('<slug>'), body: z.object({ [k]: z.string() }) }
* - "body" keys are non-optional strings only.
import org.typelevel.jawn._
import org.typelevel.jawn.ast.JValue
object JawnChunkParsing {
val parser = AsyncParser[JValue](AsyncParser.ValueStream)
fs2.Stream.emits(
"""
|{"adiel": "isGever", "yotam": 1111}
|{"adiel": "blahblah", "yotam": -14}
import cats.Monoid
import cats.effect.Sync
object Fs2AccumulateWhile {
def mapAccumulateWhile[F[_], S, O, O2](
f: O => (S, O2),
p: S => Boolean,
publishState: S => F[Unit]
)(implicit F: Sync[F], S: Monoid[S]): fs2.Pipe[F, O, O2] = {
package com.checkpoint.datatube.filterpoc
import com.googlecode.cqengine.ConcurrentIndexedCollection
import com.googlecode.cqengine.attribute.SimpleAttribute
import com.googlecode.cqengine.query.option.QueryOptions
import com.googlecode.cqengine.query.parser.sql.SQLParser
object CQEngineExample extends App {
def intAttr(name: String): SimpleAttribute[Map[String, Any], Integer] =
def int_to_string(ls, n, i = 0):
if n == 0:
ls[i] = '\0'
return 0
else:
idx = int_to_string(ls, n // 10, i+1)
ls[idx] = str(n % 10)
return idx + 1
ls = [''] * 100
object ParallelNested {
// Taking inspiration from `cats.IO`'s `Parallel` instance:
// https://github.com/typelevel/cats-effect/blob/a6ebc9fd7f02dc15a74c0e9a1cfca3c843b63efb/core/shared/src/main/scala/cats/effect/IO.scala#L970-L988
implicit val nestedParallel = new NonEmptyParallel[Nested[IO, ValidatedNec[Throwable, *], *]] {
override type F = Nested[IO.Par, ValidatedNec[Throwable, *], *]
override def apply: Apply[F] = Apply[F]
override def flatMap: FlatMap[Nested[IO, ValidatedNec[Throwable, *], *]] = FlatMap[Nested[IO, ValidatedNec[Throwable, *], *]]
package com.checkpoint.datatube.firehose
import cats.effect.{Blocker, ExitCode, IO, IOApp}
import org.http4s.{HttpRoutes, StaticFile}
import org.http4s.dsl.io._
import org.http4s.implicits._
import scala.concurrent.ExecutionContext.global
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
package com.checkpoint.datatube.firehose
import java.util.concurrent.TimeUnit
import cats.effect.{Clock, ExitCode, IO, IOApp}
import cats.implicits._
object CatsParallelExecution extends IOApp{
val clock: Clock[IO] = Clock[IO]
object PatternMatchingMorphBehaviour {
// Intention is to morph behaviour of pattern matching,
// in this example I want to make an unapplied value's "equals"
// method always return true if the value it compares against is a None
// (and additionally, if it is a Some, to extract to value out of the Some)
// this way I can do like a "runtime optional matcher"
trait EqualIfNone {
override def equals(obj: Any): Boolean = obj match {
case Some(x) => super.equals(x)
object SslContextAltering {
def updateGlobalSsl = {
val certStr =
"""
|-----BEGIN CERTIFICATE-----
|MIICwzCCAaugAwIBAgIJAL1OEIqJjvZRMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV
|BAMTCWxvY2FsaG9zdDAeFw0yMDEwMjgxNTE0NThaFw0zMDEwMjYxNTE0NThaMBQx
|EjAQBgNVBAMTCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
|ggEBAMkf8CZbezSszs/H5UEVLg0HPPqfpyHUx1OGaUcrmh7VxY47Gmp08Z3ynBjs
|K0efNORciFj0NTpkwidYqRdEiVLViurNLrugFiUaIWwEyNQUyTTfHIHJN1CtHwyw