1

I would like to get value of a nested case class giving a "path" in this nested case class.

For example, giving:

case class Address(street : String, city : String, postcode : String)
// Nested case class
case class Person(name : String, age : Int, address : Address)

val person = Person("Joe Grey", 37, Address("Southover Street", "Brighton", "BN2 9UA"))

val path = "address_street" // _ is arbitrary separator

I would like getter(path) returns "Southover Street".

I try different things with shapeless' lenses like

val steetLens = lens[Person] >> Witness(Symbol("address")) >> Witness(Symbol("street"))
println(steetLens.get(person))

which return the right thing but I can't use it in a generic way because something like this:

 val steetLens = lens[Person] >> addressField >> streetField

doesn't work..

As far as I understand we can't create Symbol from generic string.

Thomas
  • 1,164
  • 13
  • 41

1 Answers1

0

Can you use standard composition of lenses?

val address: Lens[Person, Address] = lens[Person].address
val street: Lens[Address, String] = lens[Address].street
val addressStreet: Lens[Person, String] = street compose address
addressStreet.get(person) // Southover Street
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • The idea is something like that but I would like to use string to access object in Lens, like: `lens[Person]("address")` or something similar – Thomas Oct 29 '18 at 09:19