Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package entity
- import model.{Address, Person, Student}
- case class PersonEntity(firstName: String, lastName: String, city: String, state: String, pin: String) {
- def as[T](implicit f: PersonEntity => T) = f(this)
- }
- object PersonEntity {
- /**
- * Mapper function that takes a PersonEntity and returns a Person case class
- *
- * @return
- */
- implicit def personMapper = (personEntity: PersonEntity) =>
- Person(
- personEntity.firstName,
- personEntity.lastName,
- Address(
- personEntity.city,
- personEntity.state,
- personEntity.pin
- )
- )
- implicit def studentMapper = (personEntity: PersonEntity) =>
- Student(personEntity.firstName, personEntity.city)
- }
- // ---------------------------------
- package model
- import entity.PersonEntity
- case class Person (firstName: String, lastName: String, address: Address) {
- def as[T](implicit f: Person => T) = f(this)
- }
- case class Address(city: String, state: String, pin: String)
- object Person {
- /**
- * Mapper function that takes a PersonEntity and returns a Person case class
- *
- * @return
- */
- implicit def personEntityMapper = (person: Person) =>
- PersonEntity(
- person.firstName,
- person.lastName,
- person.address.city,
- person.address.state,
- person.address.pin
- )
- }
- // ---------------------------------
- object Hello extends Greeting with App {
- val p = PersonRepository.getPerson("Tom")
- println("Found Person: " + p )
- val pe: PersonEntity = p;
- println("Convert Person: " + pe )
- }
- // ---------------------------------
- package model
- case class Student (firstName: String, city: String)
Add Comment
Please, Sign In to add comment