Skip to content

Commit

Permalink
Updates for Contingency changes
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Jul 5, 2024
1 parent 880bf04 commit 047042a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/core/xmlreader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ trait XmlDecoder[ValueType]:
def map[ValueType2](lambda: ValueType => ValueType2): XmlDecoder[ValueType2] = list => lambda(read(list))

object XmlDecoder extends Derivation[XmlDecoder]:
given text(using Errant[XmlReadError]): XmlDecoder[Text] = list =>
given text(using Tactic[XmlReadError]): XmlDecoder[Text] = list =>
val elements = childElements(list).collect { case XmlAst.Textual(text) => text }
if elements.length == 0 then raise(XmlReadError())("".tt) else elements.head
if elements.length == 0 then raise(XmlReadError(), "".tt) else elements.head

given [ValueType](using decoder: Decoder[ValueType]): XmlDecoder[ValueType] = value =>
(value: @unchecked) match
case XmlAst.Element(_, XmlAst.Textual(text) :: _, _, _) +: _ => text.decodeAs[ValueType]
Expand All @@ -44,9 +44,9 @@ object XmlDecoder extends Derivation[XmlDecoder]:
elements.collect { case element: XmlAst.Element => element }
.find(_.name.name == label)
.get

context.read(List(element))

inline def split[DerivationType: SumReflection]: XmlDecoder[DerivationType] = list =>
(list.head: @unchecked) match
case XmlAst.Element(_, children, attributes, _) =>
Expand Down
6 changes: 3 additions & 3 deletions src/core/xylophone.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ case class XmlNode(head: Int, path: XmlPath, root: XmlAst.Root) extends Xml, Dyn
infix def + (other: Xml): XmlDoc raises XmlAccessError =
XmlDoc(XmlAst.Root(Xml.normalize(this) ++ Xml.normalize(other)*))

def as[ValueType: XmlDecoder](using Errant[XmlReadError], Errant[XmlAccessError]): ValueType =
def as[ValueType: XmlDecoder](using Tactic[XmlReadError], Tactic[XmlAccessError]): ValueType =
summon[XmlDecoder[ValueType]].read(Xml.normalize(this))

case class XmlDoc(root: XmlAst.Root) extends Xml, Dynamic:
Expand All @@ -200,14 +200,14 @@ case class XmlDoc(root: XmlAst.Root) extends Xml, Dynamic:
infix def + (other: Xml): XmlDoc raises XmlAccessError =
XmlDoc(XmlAst.Root(Xml.normalize(this) ++ Xml.normalize(other)*))

def as[ValueType: XmlDecoder](using Errant[XmlAccessError], Errant[XmlReadError]): ValueType =
def as[ValueType: XmlDecoder](using Tactic[XmlAccessError], Tactic[XmlReadError]): ValueType =
summon[XmlDecoder[ValueType]].read(Xml.normalize(this))

case class Attribute(node: XmlNode, attribute: Text):
def as
[ValueType]
(using decoder: XmlDecoder[ValueType])
(using Errant[XmlReadError], Errant[XmlAccessError]): ValueType =
(using Tactic[XmlReadError], Tactic[XmlAccessError]): ValueType =

val attributes = Xml.normalize(node).prim match
case XmlAst.Element(_, _, attributes, _) => attributes
Expand Down
32 changes: 16 additions & 16 deletions src/test/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import contingency.*
import printers.compact

import unsafeExceptions.canThrowAny
import errorHandlers.throwUnsafely
import strategies.throwUnsafely

case class Worker(name: Text, age: Int)
case class Firm(name: Text, ceo: Worker)
Expand All @@ -44,7 +44,7 @@ object Tests extends Suite(t"Xylophone tests"):
test(t"extract integer"):
Xml.parse(t"""<message>1</message>""").as[Int]
.assert(_ == 1)

test(t"extract string"):
Xml.parse(t"""<message>Hello world</message>""").as[Text]
.assert(_ == t"Hello world")
Expand All @@ -53,7 +53,7 @@ object Tests extends Suite(t"Xylophone tests"):
val xml = Xml.parse(t"""<message><info>Hello world</info></message>""")
xml.info.as[Text]
.assert(_ == t"Hello world")

test(t"extract string from node"):
val xml = Xml.parse(t"""<message><info>Hello world</info></message>""")
xml.info().as[Text]
Expand All @@ -63,7 +63,7 @@ object Tests extends Suite(t"Xylophone tests"):
val person = Worker(t"Jack", 50)
person.xml.string
.assert(_ == t"<Worker><name>Jack</name><age>50</age></Worker>")

test(t"serialize nested case class"):
val person = Worker(t"Jack", 50)
val company = Firm(t"Acme Inc", person)
Expand All @@ -74,13 +74,13 @@ object Tests extends Suite(t"Xylophone tests"):
val xml = Xml.parse(t"""<events><eventId>1</eventId><eventId>2</eventId></events>""")
xml.eventId(1).as[Int]
.assert(_ == 2)

test(t"extract to simple case class"):
val string = t"<jack><name>Jack</name><age>50</age></jack>"
val xml = Xml.parse(string)
xml.as[Worker]
.assert(_ == Worker(t"Jack", 50))

test(t"extract to nested case class"):
val string = t"<Firm><name>Acme Inc</name><ceo><name>Jack</name><age>50</age></ceo></Firm>"
val xml = Xml.parse(string)
Expand All @@ -91,7 +91,7 @@ object Tests extends Suite(t"Xylophone tests"):
val book = Book(t"Lord of the Flies", t"9780399529207")
book.xml.string
.assert(_ == t"<Book isbn=\"9780399529207\"><title>Lord of the Flies</title></Book>")

test(t"serialize nested type with attribute"):
val bibliography = Bibliography(t"William Golding", Book(t"Lord of the Flies", t"9780399529207"))
bibliography.xml.string
Expand All @@ -101,7 +101,7 @@ object Tests extends Suite(t"Xylophone tests"):
val color: ColorVal = ColorVal.Rgb(5, 10, 15)
color.xml.string
.assert(_ == t"""<ColorVal type="Rgb"><red>5</red><green>10</green><blue>15</blue></ColorVal>""")

test(t"serialize nested coproduct"):
val pixel: Pixel = Pixel(100, 200, ColorVal.Cmyk(1, 2, 3, 4))
pixel.xml.string
Expand All @@ -112,7 +112,7 @@ object Tests extends Suite(t"Xylophone tests"):
val xml = Xml.parse(string)
xml.as[ColorVal]
.assert(_ == ColorVal.Cmyk(1, 2, 3, 4))

test(t"read nested coproduct"):
val string = t"""<Pixel><x>100</x><y>200</y><color type="Cmyk"><cyan>1</cyan><magenta>2</magenta><yellow>3</yellow><key>4</key></color></Pixel>"""
val xml = Xml.parse(string)
Expand All @@ -124,7 +124,7 @@ object Tests extends Suite(t"Xylophone tests"):
val xml = Xml.parse(string)
xml.content.attribute(t"key").as[Text]
.assert(_ == t"value")

test(t"read attribute value from node"):
val string = t"""<node><content key="value"/></node>"""
val xml = Xml.parse(string)
Expand All @@ -150,7 +150,7 @@ object Tests extends Suite(t"Xylophone tests"):
val xs = List(t"one", t"two", t"three")
Xml.print(xs.xml)
.assert(_ == t"<Seq><Text>one</Text><Text>two</Text><Text>three</Text></Seq>")

test(t"serialize list of complex objects"):
val book1 = Book(t"Lord of the Flies", t"9780399529207")
val book2 = Book(t"Brave New World", t"9781907704345")
Expand Down Expand Up @@ -184,21 +184,21 @@ object Tests extends Suite(t"Xylophone tests"):
val xml = Xml.parse(t"""<root><company><staff><ceo><name>Xyz</name></ceo></staff></company></root>""")
capture(xml.company().staff().cto().name().as[Text])
.assert(_ == XmlAccessError(0, List(t"company", 0, t"staff", 0, t"cto")))

test(t"access error; taking all children"):
val xml = Xml.parse(t"""<root><company><staff><ceo><name>Xyz</name></ceo></staff></company></root>""")
capture(xml.company.staff.cto.name().as[Text])
.assert(_ == XmlAccessError(0, List(t"company", t"staff", t"cto", t"name")))

test(t"access non-zero node"):
val xml = Xml.parse(t"""<root><company><staff><ceo><name>Xyz</name></ceo></staff></company></root>""")
capture(xml.company(1).staff().cto.name().as[Text])
.assert(_ == XmlAccessError(1, List(t"company")))
// test(t"simple literal content is as expected"):

// test(t"simple literal content is as expected"):
// x"""<root attribute=""/>""".show
// .assert(_ == t"""<root attribute=""/>""")

// test(t"literal content is as expected"):
// test(t"literal content is as expected"):
// x"<root><company><staff><ceo><name>Xyz</name></ceo></staff></company></root>"
// .assert(_ == t"""<root><company><staff><ceo><name>Xyz</name></ceo></staff></company></root>""")

0 comments on commit 047042a

Please sign in to comment.