Last active
January 22, 2020 15:46
-
-
Save valentiay/f7e0419c2be0aaae62c26a898497a36f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.syntax.functor._ | |
sealed trait Foo | |
@ElementCodec | |
case class Bar(a: Int) extends Foo | |
@ElementCodec | |
case class Qux(b: String) extends Foo | |
object Foo { | |
implicit val fooDecoder: ElementDecoder[Foo] = ElementDecoder[Foo] { | |
def decodeAsElement(c: Cursor, localName: String, namespaceUri: Option[String]): ElementDecoder[Foo] = { | |
if (c.isStartElement) { | |
ElementDecoder.errorIfWrongName[String](c, localName, namespaceUri).getOrElse { | |
val idx = c.getAttributeIndex("http://namespace.com", "type") | |
if (idx > -1) { | |
c.getAttributeValue(idx) match { | |
case "bar" => ElementDecoder[Bar].decodeAsElement(c, localName, namespaceUri).widen | |
case "qux" => ElementDecoder[Qux].decodeAsElement(c, localName, namespaceUri).widen | |
case _ => new FailedDecoder(c.error("Unexpected 'type' value")) | |
} | |
} else { | |
new FailedDecoder(c.error("Missing 'type' attribute")) | |
} | |
} | |
} else { | |
new FailedDecoder(c.error("Wrong state")) | |
} | |
} | |
def result(history: List[String]): Either[DecodingError, Foo] = | |
Left(DecodingError("Decoding not complete", history)) | |
val isCompleted: Boolean = false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment