title: Trim values of String fields of a case class
body: <p>I am writing a generic function using <a href="https://github.com/milessabin/shapeless" rel="noreferrer">shapeless</a> that takes an instance of a <code>case class</code> and trims all values of fields that are string. The case class can have optional fields, nested objects, lists etc.</p>
  
  <p>I have a case class <code>Person</code>.</p>
  
  <pre><code>case class Person(name: Option[String], address: List[String], friends: List[Person])
  </code></pre>
  
  <p>The function that I currently have:</p>
  
  <pre><code>import shapeless._, ops.hlist._
  
    object trimmer extends Poly1 {
      implicit val stringOptCase = at[Option[String]](_.map(_.trim))
      implicit val stringListCase = at[List[String]](_.map(_.trim))
      implicit def skipCase[A] = at[A](identity)
    }
  
    def trimStringValues[A, R &lt;: HList](a: A)(implicit
                                       gen: Generic.Aux[A, R],
                                       mapper: Mapper.Aux[trimmer.type, R, R]
    ) = gen.from(mapper(gen.to(a)))
  </code></pre>
  
  <p>When I use the above function, it only works for the root level <code>name</code> field of class <code>Person</code>. It doesn't work for list or object field.</p>
  
  <pre><code>val person = Person(name = Some(" john "), address = List(" ny"," vegas "), friends = List(Person(Some(" alicia"), List(" peter"), Nil)))
  
  trimStringValues(person) // Person(Some(john),List(ny, vegas),List(Person(Some( alicia),List( peter),List())))
  </code></pre>
  
  <p>How can I solve this ?</p>