|
1 | 1 | package dev.atedeg
|
2 | 2 |
|
3 |
| -import scala.util.Try |
4 |
| - |
5 |
| -import better.files.File |
6 |
| -import io.circe.parser.{ parse => parseJsonString } |
7 |
| -import io.circe.{ Decoder, Json } |
8 |
| -import io.circe.yaml.parser |
9 |
| -import cats.implicits._ |
10 |
| -import Extensions._ |
11 |
| - |
12 | 3 | sealed trait EntityType {
|
13 | 4 | override def toString: String = this match {
|
14 | 5 | case Class => "class"
|
@@ -39,61 +30,17 @@ case object Type extends EntityType
|
39 | 30 | case object Case extends EntityType
|
40 | 31 | case object Def extends EntityType
|
41 | 32 |
|
42 |
| -final case class Entity(entityType: EntityType, link: String, name: String, packageName: String) |
43 |
| -final case class BaseEntity(entityType: EntityType, name: String) |
44 |
| - |
45 |
| -object Entity { |
46 |
| - private val entitiesFileName = "searchData.js" |
47 |
| - |
48 |
| - private def allEntitiesFile(workingDir: File): File = |
49 |
| - workingDir / "target" / "site" / "scripts" / entitiesFileName |
50 |
| - |
51 |
| - def readAll(workingDir: File): Either[Error, Set[Entity]] = Utils.parseFileWith(allEntitiesFile(workingDir))(parse) |
52 |
| - |
53 |
| - private[atedeg] def parse(raw: String): Either[Error, Set[Entity]] = |
54 |
| - parseJsonString(raw).leftMap(CirceParsingFailure).flatMap(parseJson) |
55 |
| - |
56 |
| - private def parseJson(json: Json): Either[Error, Set[Entity]] = { |
57 |
| - def build(maybeType: Option[EntityType], link: String, name: String, packageName: String): Option[Entity] = |
58 |
| - maybeType.map(Entity(_, link, name, packageName)) |
59 |
| - |
60 |
| - implicit val decodeEntityType: Decoder[Option[EntityType]] = Decoder.decodeString.map(EntityType.fromString) |
61 |
| - implicit val decodeEntity: Decoder[Option[Entity]] = Decoder.forProduct4("k", "l", "n", "d")(build) |
62 |
| - json.as[Set[Option[Entity]]].map(_.dropNone).leftMap(CirceDecodingFailure) |
| 33 | +final case class Entity(entityType: EntityType, link: String, name: String, packageName: String) { |
| 34 | + def toBaseEntity: BaseEntity = BaseEntity(entityType, name) |
| 35 | + def sanitizedLink: String = link.split('#').head |
| 36 | + def entityId: Option[String] = link.split('#').lastOption |
| 37 | + def isClassLike: Boolean = entityType match { |
| 38 | + case Class | Trait | Enum => true |
| 39 | + case Type | Case | Def => false |
63 | 40 | }
|
64 | 41 | }
|
65 |
| - |
| 42 | +final case class BaseEntity(entityType: EntityType, name: String) |
66 | 43 | final case class Configuration(ignored: Set[BaseEntity], tables: List[TableConfig])
|
67 |
| -final case class TableConfig( |
68 |
| - name: String, |
69 |
| - termName: Option[String], |
70 |
| - definitionName: Option[String], |
71 |
| - rows: List[BaseEntity], |
72 |
| -) |
73 |
| - |
74 |
| -object Configuration { |
75 |
| - private val configFile = ".ubidoc.yaml" |
76 |
| - |
77 |
| - def read(workingDir: File): Either[Error, Configuration] = Utils.parseFileWith(workingDir / configFile)(parse) |
| 44 | +final case class TableConfig(name: String, termName: Option[String], defName: Option[String], rows: List[BaseEntity]) |
78 | 45 |
|
79 |
| - private[atedeg] def parse(raw: String): Either[Error, Configuration] = |
80 |
| - parser.parse(raw).leftMap[Error](CirceParsingFailure).flatMap(parseJson) |
81 | 46 |
|
82 |
| - private def parseJson(json: Json): Either[CirceDecodingFailure, Configuration] = { |
83 |
| - import io.circe.generic.auto._ |
84 |
| - implicit val decodeEntity: Decoder[BaseEntity] = List(Class, Trait, Enum, Case, Type, Def) |
85 |
| - .map(t => (t.toString, BaseEntity(t, _))) |
86 |
| - .map(entry => Decoder.forProduct1(entry._1)(entry._2)) |
87 |
| - .reduceLeft(_ or _) |
88 |
| - json.as[Configuration].leftMap(CirceDecodingFailure) |
89 |
| - } |
90 |
| -} |
91 |
| - |
92 |
| -private object Utils { |
93 |
| - |
94 |
| - private def openFile(file: File): Either[Error, String] = |
95 |
| - Try(file.contentAsString).toEither.leftMap(ExternalError) |
96 |
| - |
97 |
| - def parseFileWith[A](file: File)(parser: String => Either[Error, A]): Either[Error, A] = |
98 |
| - openFile(file).flatMap(parser) |
99 |
| -} |
0 commit comments