propensive / rapture

Rapture
183 stars 39 forks source link

Rapture Xml with optionals attributes #253

Open OscarSdA opened 7 years ago

OscarSdA commented 7 years ago

Environment:

I'm working with:

case class Foo(
     no: Int,
     bar: Option[String],
     bar2: String,
     greeting: String
   )
 object Foo {
   implicit val mensajeExt = Xml.extractor[Xml].map { x =>
     Foo(
       x.no.as[Int],
       x('bar).as[Option[String]],
       x('bar2).as[String],
       x.greeting.as[String]
     )
   }
 }

val xml = """<?xml version="1.0" encoding="ISO-8859-1"?>
  |<foo bar2="baz2">
  |  <greeting>Hello world</greeting>
  |  <no>42</no>
  |</foo>""".stripMargin

if I try to get a Foo, fails because it can not find the attribute 'bar:

Xml.parse(xml).as[Foo]

Error:

key not found: bar
java.util.NoSuchElementException: key not found: bar
    at scala.collection.MapLike$class.default(MapLike.scala:228)
...