Skip to content

Instantly share code, notes, and snippets.

@williamho
Created May 13, 2016 19:59
Show Gist options
  • Save williamho/980a7dd3b96d63b4f805a09dfc84803c to your computer and use it in GitHub Desktop.
Save williamho/980a7dd3b96d63b4f805a09dfc84803c to your computer and use it in GitHub Desktop.
scanamo
import shapeless._
import shapeless.labelled._
import shapeless.ops.function._
import shapeless.ops.hlist._
import shapeless.ops.record._
import shapeless.record._
import shapeless.syntax.singleton._
import shapeless.syntax.std._
import shapeless.syntax.std.function._
import shapeless.syntax.std.traversable._
import shapeless.record.recordOps
/**
* {{{
* >>> case class User(name: String, age: Int)
* >>> val df: DynamoFormat[User] = DynamoFormat
* ... .genericRenamed('name, 'n)
* >>> df.write(User("Hello", 5))
* Right(User(Hello,5))
* }}}
*/
def genericRenamed[T, R <: HList, InnerR <: HList](oldKey: Witness, newKey: Witness)(implicit
gen: LabelledGeneric.Aux[T, R],
renamerWrite: Renamer.Aux[R, oldKey.T, newKey.T, InnerR],
renamerRead: Renamer.Aux[InnerR, newKey.T, oldKey.T, R],
formatR: Lazy[ConstructedDynamoFormat[InnerR]]
): DynamoFormat[T] =
new DynamoFormat[T] {
def read(av: AttributeValue): Xor[DynamoReadError, T] =
formatR.value.read(av).map(l => gen.from(l.renameField(newKey, oldKey))).toXor
def write(t: T): AttributeValue =
formatR.value.write(gen.to(t).renameField(oldKey, newKey))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment