Skip to content

Commit

Permalink
Add a typed ProjectionExpression constructor (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin authored Sep 30, 2024
1 parent 15cb93f commit f04eff0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,11 @@ object ProjectionExpression extends ProjectionExpressionLowPriorityImplicits0 {
* @see [[parse]]
*/
def $(s: String): ProjectionExpression[Any, Unknown] =
$$(s)

def $$[From, To](s: String): ProjectionExpression[From, To] =
parse(s) match {
case Right(a) => a.unsafeFrom[Any].unsafeTo[Unknown]
case Right(a) => a.unsafeFrom[From].unsafeTo[To]
case Left(msg) => throw new IllegalStateException(msg)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ object KeyConditionExprExample extends App {

import zio.dynamodb.KeyConditionExpr._
import zio.dynamodb.KeyConditionExpr.SortKeyEquals
import zio.dynamodb.ProjectionExpression.$
import zio.dynamodb.ProjectionExpression.{ $, $$ }

// Unsafe low-level API
val x6 =
$("foo.bar").partitionKey === 1 && $("foo.baz").sortKey === "y"
val x7 = $("foo.bar").partitionKey === 1 && $("foo.baz").sortKey > 1
Expand All @@ -21,6 +22,14 @@ object KeyConditionExprExample extends App {
val x9 =
$("foo.bar").partitionKey === 1 && $("foo.baz").sortKey.beginsWith(1L)

// More type-safe low-level API
sealed trait UnderlyingModel
val peInt: ProjectionExpression[UnderlyingModel, Int] = $$("foo.bar")
val peString: ProjectionExpression[UnderlyingModel, String] = $$("foo.baz")

val x6Int = peInt.partitionKey === 1 && peString.sortKey === "y"

// High-level API
final case class Elephant(email: String, subject: String, age: Int)
object Elephant {
implicit val schema: Schema.CaseClass3[String, String, Int, Elephant] = DeriveSchema.gen[Elephant]
Expand Down

0 comments on commit f04eff0

Please sign in to comment.