Skip to content

Commit

Permalink
Add a typed ProjectionExpression constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin committed Sep 11, 2024
1 parent 73f391f commit c7be881
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 c7be881

Please sign in to comment.