Skip to content

Commit

Permalink
Use List instead of Seq
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Feb 16, 2024
1 parent a0dbb1d commit 106dbcb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/core/xmlast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object Ast:

enum Ast:
case Element
(name: XmlName, children: Seq[Ast], attributes: Map[XmlName, Text] = Map(),
(name: XmlName, children: List[Ast], attributes: Map[XmlName, Text] = Map(),
namespaces: List[Namespace] = Nil)
case Comment(content: Text)
case ProcessingInstruction(target: Text, content: Text)
Expand Down
6 changes: 3 additions & 3 deletions src/core/xmlreader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import anticipation.*
import spectacular.*

trait XmlDecoder[ValueType]:
def read(xml: Seq[Ast]): Option[ValueType]
def read(xml: List[Ast]): Option[ValueType]
def map[ValueType2](lambda: ValueType => Option[ValueType2]): XmlDecoder[ValueType2] = read(_).flatMap(lambda(_))

object XmlDecoder:
Expand All @@ -39,7 +39,7 @@ object XmlDecoder:
// elems
// .collect { case e: Ast.Element => e }
// .find(_.name.name.s == param.label)
// .flatMap { e => param.typeclass.read(Seq(e)) }.get
// .flatMap { e => param.typeclass.read(List(e)) }.get

// def split[DerivationType](sealedTrait: SealedTrait[XmlDecoder, DerivationType]): XmlDecoder[DerivationType] = seq =>
// seq.headOption match
Expand All @@ -51,5 +51,5 @@ object XmlDecoder:
// case _ =>
// None

private def childElements(seq: Seq[Ast]): Seq[Ast] =
private def childElements(seq: List[Ast]): Seq[Ast] =
seq.collect { case e@Ast.Element(_, children, _, _) => children }.flatten
2 changes: 1 addition & 1 deletion src/core/xmlwriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object XmlEncoder:

given [ValueType: XmlEncoder, CollectionType[ElementType] <: Seq[ElementType]]
: XmlEncoder[CollectionType[ValueType]] =
elements => Ast.Element(XmlName(t"Seq"), elements.map(summon[XmlEncoder[ValueType]].write(_)))
elements => Ast.Element(XmlName(t"Seq"), elements.to(List).map(summon[XmlEncoder[ValueType]].write(_)))

given XmlEncoder[Int] = int =>
Ast.Element(XmlName(t"Int"), List(Ast.Textual(int.show)))
Expand Down
15 changes: 6 additions & 9 deletions src/core/xylophone.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ object Xml:
case ELEMENT_NODE =>
val xmlName = XmlName(Text(node.getLocalName.nn), getNamespace(node))
val childNodes = node.getChildNodes

val children =
(0 until childNodes.nn.getLength).map(childNodes.nn.item(_).nn).map(readNode(_))

val children = List.range(0, childNodes.nn.getLength).map(childNodes.nn.item(_).nn).map(readNode(_))
val atts = (0 until node.getAttributes.nn.getLength).map(node.getAttributes.nn.item(_).nn)
val attributes = atts.map { att =>
val alias: Optional[Namespace] = getNamespace(att)
Expand All @@ -127,14 +124,14 @@ object Xml:
(readNode(root.getDocumentElement.nn): @unchecked) match
case elem@Ast.Element(_, _, _, _) => XmlDoc(Ast.Root(elem))

def normalize(xml: Xml): Seq[Ast] raises XmlAccessError =
def recur(path: XmlPath, current: Seq[Ast]): Seq[Ast] = (path: @unchecked) match
def normalize(xml: Xml): List[Ast] raises XmlAccessError =
def recur(path: XmlPath, current: List[Ast]): List[Ast] = (path: @unchecked) match
case Nil =>
current

case (idx: Int) :: tail =>
if current.length <= idx then abort(XmlAccessError(idx, path))
recur(tail, Seq(current(idx)))
recur(tail, List(current(idx)))

case (unit: Unit) :: tail =>
val next = current
Expand All @@ -151,7 +148,7 @@ object Xml:

recur(tail, next)

try recur(xml.pointer, xml.root.content)
try recur(xml.pointer, xml.root.content.to(List))
catch case err: XmlAccessError =>
abort(XmlAccessError(err.index, xml.pointer.dropRight(err.path.length)))

Expand Down Expand Up @@ -215,7 +212,7 @@ case class Attribute(node: XmlNode, attribute: Text):
case _ => abort(XmlReadError())

summon[XmlDecoder[T]]
.read(Seq(Ast.Element(XmlName(t"empty"), Seq(Ast.Textual(attributes(XmlName(attribute)))))))
.read(List(Ast.Element(XmlName(t"empty"), List(Ast.Textual(attributes(XmlName(attribute)))))))
.getOrElse(abort(XmlReadError()))

case class xmlAttribute() extends StaticAnnotation
Expand Down

0 comments on commit 106dbcb

Please sign in to comment.