Skip to content

yoeluk/xmlteaser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XML Teaser: Ad hoc xml parser with shapeless

import scalaz.Apply, scalaz.Scalaz.optionInstance
import shapeless._, record._, syntax.singleton._, ops.record.Selector

val xml =
    <root>
      <emptyfield/>
      <allsome>
        <some>value</some>
        <some>value</some>
        <some>value</some>lala
      </allsome>
      <rabbit>Longears</rabbit>
      <duckcount>56</duckcount>
    </root>

val rabbitWitness = Witness("rabbit")

def selectRabbit[L <: HList](xs: L)(implicit sel: Selector[L, rabbitWitness.T]) = xs("rabbit")

val maybeNoRabbitRec = xml.toRecord[Record.`"duckcount" -> Int, "allsome" -> String, "root" -> String, "emptyfield" -> String`.T]

maybeNoRabbitRec.map(selectRabbit(_)) // won't compile

val maybeRabbitRec = xml.toRecord[Record.`"rabbit" -> String`.T]

val maybeCombinedRec = Apply[Option].apply2(maybeNoRabbitRec, maybeRabbitRec){_ ++ _}

maybeCombinedRec.map(selectRabbit(_)) // Some("Longears")

xml.toRecord[Record.`"rabbit" -> Int, "duckcount" -> Int`.T] // None

Examples:

See more examples in src/test/scala/com/teaser/TeaserSpec.scala file

About

ad hoc xml parser with shapeless

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages