From df41083066bbb77802cbfc63cdfd55ac6ba62956 Mon Sep 17 00:00:00 2001 From: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Date: Tue, 19 Mar 2024 14:07:44 +0100 Subject: [PATCH] Add `orElse` to Schema for Scala 3 that returns a union type (#669) --- .../src/main/scala-3/zio/schema/package.scala | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/zio-schema/shared/src/main/scala-3/zio/schema/package.scala b/zio-schema/shared/src/main/scala-3/zio/schema/package.scala index 72f1c73c9..57f1f2b5b 100644 --- a/zio-schema/shared/src/main/scala-3/zio/schema/package.scala +++ b/zio-schema/shared/src/main/scala-3/zio/schema/package.scala @@ -1,5 +1,27 @@ package zio +import scala.reflect.ClassTag + package object schema { type Singleton = scala.Singleton + + extension [A: ClassTag, B: ClassTag] (s: Schema.Fallback[A, B]) + def merge: Schema[A | B] = + s.transform(_.merge, { + case left: A => Fallback.Left(left.asInstanceOf[A]) + case right: B => Fallback.Right(right.asInstanceOf[B]) + }) + + extension [A: ClassTag] (s: Schema[A]) + def orElse[B: ClassTag](s0: Schema[B]): Schema[A | B] = + Schema.Fallback(s, s0).merge + + extension [A: ClassTag, B: ClassTag] (fallback: Fallback[A, B]) + def merge: A | B = + fallback match { + case Fallback.Left(left) => left + case Fallback.Right(right) => right + case Fallback.Both(left, _) => left + } + }