Skip to content

Instantly share code, notes, and snippets.

@edwardGunawan
edwardGunawan / Status.scala
Created August 26, 2021 07:30
How to Turn Your Domain Model into DynamoDB AttributeValue
sealed trait Status {
def `type`: String
}
case class Refund(`type` = "refund", refundId: String, refundReason:String, timestamp:Instant, amount:Double) extends Status
case class Paid(`type` = "paid", timestamp: Instant, failureReasonCode:Option[String], failureDescription:Option[String], amount: Double) extends Status
case class Overdue(`type` = "overdue", timestamp:Instant, paymentDate:Instant, amount:Double) extends Status
@edwardGunawan
edwardGunawan / Metadata.scala
Created August 26, 2021 07:30
How to Turn Your Domain Model into DynamoDB AttributeValue
case class Metadata(isTest:Boolean, region: Region, sourceOfRequest:Option[String])
@edwardGunawan
edwardGunawan / InvoiceDomainModel.scala
Created August 26, 2021 07:30
How to Turn Your Domain Model into DynamoDB AttributeValue
Invoice(invoiceId: String, orderCode:String, metadata:Metadata)
@edwardGunawan
edwardGunawan / invoiceSchemaRead.scala
Created August 26, 2021 07:29
How to Turn Your Domain Model into DynamoDB AttributeValue
invoiceSchema.read(DynamoValue(attributeValue)) // Either[WriteError, Invoice]
@edwardGunawan
edwardGunawan / invoiceWrite.scala
Created August 26, 2021 07:26
How to Turn Your Domain Model into DynamoDB AttributeValue
invoiceSchema.write(Invoice(orderCode = "1230", invoiceId = "1"))
@edwardGunawan
edwardGunawan / invoiceSchema.scala
Created August 26, 2021 07:25
How to Turn Your Domain Model into DynamoDB AttributeValue
lazy val invoiceSchema: Schema[Invoice] = Schema.record[Invoice]{field =>
(
field("orderCode", _.orderCode),
field("invoiceId", _.invoiceId)
).mapN(Invoice.apply)
}
@edwardGunawan
edwardGunawan / Invoice.scala
Created August 26, 2021 07:25
How to Turn Your Domain Model into DynamoDB AttributeValue
case class Invoice(orderCode: String, invoiceId:String)
@edwardGunawan
edwardGunawan / mapAttributeValue.scala
Created August 26, 2021 07:24
How to Turn Your Domain Model into DynamoDB AttributeValue
Map(
"Foo" -> AttributeValue.builder.m(
Map(
"a" -> AttributeValue.builder.n("123").build,
.....
).asJava
).build
).asJava
@edwardGunawan
edwardGunawan / Foo.scala
Created August 26, 2021 07:00
How to Turn Your Domain Model into DynamoDB AttributeValue
case class Foo(a:Int, b:Int, c:String)
@edwardGunawan
edwardGunawan / UsingChooseNToImplementChoose.scala
Created August 5, 2021 19:57
Functional Programming has made My Job Easier as a Software Engineer. Here's Why.
def choose(checkStatement: Boolean)(ifTrue: => Unit, ifFalse: => Unit): Unit =
chooseN(if(checkStatement) 0 else 1)(List(ifTrue, ifFalse))