From 697f1f304487b65ec0408030c97dbb3567403a30 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Wed, 3 Feb 2021 08:55:37 +0100 Subject: [PATCH] ignore invalid chapter nodes --- modules/study/src/main/BSONHandlers.scala | 49 ++++++++++++++-------- modules/study/src/main/StudyFlatTree.scala | 16 +++---- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/modules/study/src/main/BSONHandlers.scala b/modules/study/src/main/BSONHandlers.scala index 29295a9fed..8d3d76b016 100644 --- a/modules/study/src/main/BSONHandlers.scala +++ b/modules/study/src/main/BSONHandlers.scala @@ -158,24 +158,37 @@ object BSONHandlers { ) } - def readNode(doc: Bdoc, id: UciCharPair): Node = { - import Node.BsonFields._ - val r = new Reader(doc) - Node( - id = id, - ply = r int ply, - move = WithSan(r.get[Uci](uci), r.str(san)), - fen = r.get[FEN](fen), - check = r boolD check, - shapes = r.getO[Shapes](shapes) | Shapes.empty, - comments = r.getO[Comments](comments) | Comments.empty, - gamebook = r.getO[Gamebook](gamebook), - glyphs = r.getO[Glyphs](glyphs) | Glyphs.empty, - score = r.getO[Score](score), - crazyData = r.getO[Crazyhouse.Data](crazy), - clock = r.getO[Centis](clock), - children = Node.emptyChildren, - forceVariation = r boolD forceVariation + def readNode(doc: Bdoc, id: UciCharPair): Option[Node] = { + import Node.{ BsonFields => F } + for { + ply <- doc.getAsOpt[Int](F.ply) + uci <- doc.getAsOpt[Uci](F.uci) + san <- doc.getAsOpt[String](F.san) + fen <- doc.getAsOpt[FEN](F.fen) + check = ~doc.getAsOpt[Boolean](F.check) + shapes = doc.getAsOpt[Shapes](F.shapes) getOrElse Shapes.empty + comments = doc.getAsOpt[Comments](F.comments) getOrElse Comments.empty + gamebook = doc.getAsOpt[Gamebook](F.gamebook) + glyphs = doc.getAsOpt[Glyphs](F.glyphs) getOrElse Glyphs.empty + score = doc.getAsOpt[Score](F.score) + clock = doc.getAsOpt[Centis](F.clock) + crazy = doc.getAsOpt[Crazyhouse.Data](F.crazy) + forceVariation = ~doc.getAsOpt[Boolean](F.forceVariation) + } yield Node( + id, + ply, + WithSan(uci, san), + fen, + check, + shapes, + comments, + gamebook, + glyphs, + score, + clock, + crazy, + Node.emptyChildren, + forceVariation ) } diff --git a/modules/study/src/main/StudyFlatTree.scala b/modules/study/src/main/StudyFlatTree.scala index fefa0cf7d8..1c9e97aabd 100644 --- a/modules/study/src/main/StudyFlatTree.scala +++ b/modules/study/src/main/StudyFlatTree.scala @@ -13,9 +13,10 @@ private object StudyFlatTree { private case class FlatNode(path: Path, data: Bdoc) { val depth = path.ids.size - def toNodeWithChildren(children: Option[Children]): Node = { - readNode(data, path.ids.last) - }.copy(children = children | Node.emptyChildren) + def toNodeWithChildren(children: Option[Children]): Option[Node] = + readNode(data, path.ids.last) map { + _.copy(children = children | Node.emptyChildren) + } } object reader { @@ -41,10 +42,11 @@ private object StudyFlatTree { // assumes that node has a greater depth than roots (sort beforehand) private def update(roots: Map[Path, Children], flat: FlatNode): Map[Path, Children] = { - val node = flat.toNodeWithChildren(roots get flat.path) - roots.removed(flat.path).updatedWith(flat.path.parent) { - case None => Children(Vector(node)).some - case Some(siblings) => siblings.addNode(node).some + flat.toNodeWithChildren(roots get flat.path).fold(roots) { node => + roots.removed(flat.path).updatedWith(flat.path.parent) { + case None => Children(Vector(node)).some + case Some(siblings) => siblings.addNode(node).some + } } } }