Created
January 24, 2024 19:41
-
-
Save CucumisSativus/1f1ac07a14eaa084e4800fec839a8e9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.cucumbersome.example | |
import io.scalaland.chimney.{PartialTransformer, Transformer} | |
import io.scalaland.chimney.dsl.* | |
import io.scalaland.chimney.partial.Result | |
import io.scalaland.chimney.partial | |
object Example2 { | |
case class DomainPerson(name: String, age: Int) | |
case class ApiPerson(name: Option[String], age: Option[Int]) | |
def main(args: Array[String]): Unit = { | |
val apiperson2 = ApiPerson(Some("John"), None) | |
val domainPerson2: Result[DomainPerson] = apiperson2.transformIntoPartial[DomainPerson] | |
println(domainPerson2.asErrorPathMessageStrings.mkString) | |
} | |
} | |
object PartialTransformerExample { | |
case class User(name: String, role: String) | |
case class ApiUser(name: String) | |
def main(args: Array[String]): Unit = { | |
val user = User("John", "admin") | |
val apiUser: ApiUser = user.transformInto[ApiUser] | |
println(apiUser) | |
} | |
} | |
object TransformationIntoAnyVals { | |
class UserName(val name: String) extends AnyVal | |
case class User(name: UserName) | |
case class ApiUser(name: String) | |
def main(args: Array[String]): Unit = { | |
val user = User(new UserName("John")) | |
val apiUser: ApiUser = user.into[ApiUser].transform | |
apiUser.into[User].transform | |
} | |
} | |
object FromTuples { | |
case class User(name: String, age: Int) | |
def main(args: Array[String]): Unit = { | |
("name", 11).transformInto[User] | |
} | |
} | |
object ReadFromMethods { | |
case class User(name: String) { | |
def role: String = "admin" | |
} | |
case class ApiUser(name: String, role: String) | |
def main(args: Array[String]): Unit = { | |
val user = User("John") | |
val apiUser: ApiUser = user.into[ApiUser].enableMethodAccessors.transform | |
} | |
} | |
object BeanGetters { | |
class User { | |
def getName: String = "John" | |
def getRole: String = "admin" | |
} | |
case class ApiUser(name: String, role: String) | |
def main(args: Array[String]): Unit = { | |
val user = new User | |
val apiUser: ApiUser = user.into[ApiUser].enableBeanGetters.transform | |
} | |
} | |
object Transformers { | |
enum Role: | |
case Admin | |
case User | |
case Other(value: String) | |
case class User(name: String, role: Role) | |
case class ApiUser(name: String, role: String) | |
implicit val roleTransformer: Transformer[String, Role] = { | |
case "admin" => Role.Admin | |
case "user" => Role.User | |
case other => Role.Other(other) | |
} | |
def main(args: Array[String]): Unit = { | |
val apiUser = ApiUser("John", "admin") | |
val user: User = apiUser.transformInto[User] | |
println(user) | |
} | |
} | |
object PartialTransformers { | |
enum Role: | |
case Admin | |
case User | |
case class User(name: String, role: Role) | |
case class ApiUser(name: String, role: String) | |
implicit val transformer: PartialTransformer[String, Role] = PartialTransformer{ | |
case "admin" => Result.Value(Role.Admin) | |
case "user" => Result.Value(Role.User) // or Result.Role.User | |
case other => Result.fromErrorString(s"invalid role $other") | |
} | |
def main(args: Array[String]): Unit = { | |
val apiUser = ApiUser("John", "admin") | |
val user: Result[User] = apiUser.transformIntoPartial[User] | |
val invalidRoleApiUser = ApiUser("John", "invalid") | |
val invalidRoleUser: Result[User] = invalidRoleApiUser.transformIntoPartial[User] | |
print(invalidRoleUser) | |
} | |
} | |
object ExampleErrorHandling { | |
implicit val transformer: PartialTransformer[String, Int] = PartialTransformer[String, Int] { | |
case "" => partial.Result.fromEmpty | |
case "msg" => partial.Result.fromErrorString("error message") | |
case "error" => partial.Result.fromErrorThrowable(new Throwable("an error happened")) | |
case value => partial.Result.fromCatching(value.toInt) | |
} | |
} | |
object AnyValsTransformer { | |
class UserName private(val name: String) extends AnyVal | |
object UserName { | |
def fromString(name: String): Either[String, UserName] = | |
if(name.contains(" ")) Left("invalid name") else Right(new UserName(name)) | |
} | |
case class ApiUser(name: String) | |
case class User(name: UserName) | |
implicit val userNameTransformer: PartialTransformer[String, UserName] = PartialTransformer { name => | |
Result.fromEitherString(UserName.fromString(name)) | |
} | |
def main(args: Array[String]): Unit = { | |
val apiUser = ApiUser("John Smith") | |
val user: Result[User] = apiUser.into[User].partial.transform | |
println(user) | |
} | |
} | |
object Example5 { | |
case class Foo(a: Int, b: Option[Foo]) | |
case class Bar(a: Int, b: Option[Bar]) | |
// needs this, it cannot derive the recursive transformation | |
implicit val foobar: Transformer[Foo, Bar] = Transformer.derive[Foo, Bar] | |
def main(args: Array[String]): Unit = { | |
val foo = Foo(10, Some(Foo(20, None))) | |
val bar = foo.transformInto[Bar] | |
print(bar) | |
} | |
} | |
object FieldRename { | |
case class User(userName: String) | |
case class ApiUser(name: String) | |
def main(args: Array[String]): Unit = { | |
val user = User("John") | |
val apiUser: ApiUser = user.into[ApiUser].withFieldRenamed(_.userName, _.name).transform | |
} | |
} | |
object ComputingField { | |
case class User(firstName: String, surname: String, age: Int) | |
case class ApiUser(name: String, age: Int) | |
def main(args: Array[String]): Unit = { | |
val user = User("John", "Smith", 30) | |
val apiUser: ApiUser = user.into[ApiUser] | |
.withFieldComputed(apiUser => apiUser.name , user => s"${user.firstName} ${user.surname}") | |
.transform | |
println(apiUser) | |
} | |
} | |
object Patching { | |
case class User(firstName: String, surname: String, age: Int) | |
case class UserUpdate(firstName: Option[String], surname: Option[String], age: Option[Int]) | |
def main(args: Array[String]): Unit = { | |
val user = User("John", "Smith", 30) | |
val userUpdate = UserUpdate(None, Some("Schmidt"), Some(30)) | |
println(user.patchUsing(userUpdate)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment