This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
524 additions
and
28 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version = 1.1.4 | ||
sbt.version = 1.2.1 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
logLevel := Level.Warn | ||
|
||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6") | ||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.7") |
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
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
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
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
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
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
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
16 changes: 16 additions & 0 deletions
16
src/main/scala/com/nulabinc/backlog/c2b/datas/CybozuTextModels.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.nulabinc.backlog.c2b.datas | ||
|
||
import com.nulabinc.backlog.c2b.datas.Types.DateTime | ||
|
||
case class CybozuTextTopic( | ||
title: String, | ||
description: String, | ||
) | ||
|
||
case class CybozuTextPost( | ||
content: String, | ||
postUser: CybozuTextUser, | ||
postedAt: DateTime | ||
) | ||
|
||
case class CybozuTextUser(value: String) extends AnyVal |
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
5 changes: 3 additions & 2 deletions
5
src/main/scala/com/nulabinc/backlog/c2b/parsers/ParseError.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package com.nulabinc.backlog.c2b.parsers | ||
|
||
import com.nulabinc.backlog.c2b.datas.CybozuCSVComment | ||
import com.nulabinc.backlog.c2b.datas.{CybozuCSVComment, CybozuTextPost} | ||
import org.apache.commons.csv.CSVRecord | ||
|
||
sealed trait ParseError[A] | ||
case class CannotParseCSV[A](klass: Class[A], reason: String, record: CSVRecord) extends ParseError[A] | ||
case class CannotParseFromString[A](klass: Class[A], value: String) extends ParseError[A] { | ||
case class CannotParseFromString[A](klass: Class[A], reason: String, value: String) extends ParseError[A] { | ||
override def toString: String = s"$klass $value" | ||
} | ||
case class CannotParseComment(reason: String, data: String) extends ParseError[CybozuCSVComment] | ||
case class CannotParsePost(reason: String, data: String) extends ParseError[CybozuTextPost] |
73 changes: 73 additions & 0 deletions
73
src/main/scala/com/nulabinc/backlog/c2b/parsers/TextFileParser.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.nulabinc.backlog.c2b.parsers | ||
|
||
import com.nulabinc.backlog.c2b.datas.{CybozuTextPost, CybozuTextTopic, CybozuTextUser} | ||
|
||
object TextFileParser { | ||
|
||
private val titlePattern = """.+?: (.+?)""".r | ||
private val MIN_TOPIC_LINES = 4 | ||
private val TITLE_LINE_INDEX = 1 | ||
private val DESCRIPRION_START_INDEX = 3 | ||
|
||
// post | ||
private val MIN_POST_LINES = 5 | ||
private val userPattern = """\d+?: (.+?)""".r | ||
private val USER_LINE_INDEX = 1 | ||
private val POSTED_AT_LINE_INDEX = 2 | ||
private val CONTENT_START_INDEX = 4 | ||
|
||
def topic(topicText: String): Either[ParseError[CybozuTextTopic], CybozuTextTopic] = { | ||
val lines = topicText.split("\n") | ||
|
||
if (lines.length < MIN_TOPIC_LINES) | ||
Left(CannotParseFromString(classOf[CybozuTextTopic], "Invalid topic rows: min", topicText)) | ||
else { | ||
val result = for { | ||
title <- title(lines(TITLE_LINE_INDEX)) | ||
description = arrayToString(lines.slice(DESCRIPRION_START_INDEX, lines.length)) | ||
} yield CybozuTextTopic(title, description) | ||
result match { | ||
case Right(value) => Right(value) | ||
case Left(error) => Left(CannotParseFromString(classOf[CybozuTextTopic], error.getMessage, lines.mkString("\n"))) | ||
} | ||
} | ||
} | ||
|
||
def post(postStr: String): Either[ParseError[CybozuTextPost], CybozuTextPost] = { | ||
val lines = postStr.split("\n") | ||
|
||
if (lines.length < MIN_POST_LINES) | ||
Left(CannotParsePost("Invalid post rows: min", postStr)) | ||
else { | ||
val dateString = lines(POSTED_AT_LINE_INDEX) | ||
val postedAtResult = ZonedDateTimeParser.toZonedDateTime(dateString) match { | ||
case Right(value) => Right(value) | ||
case Left(error) => Left(CannotParseFromString(classOf[CybozuTextPost], error.toString, dateString)) | ||
} | ||
for { | ||
postedAt <- postedAtResult | ||
user <- user(lines(USER_LINE_INDEX)) | ||
} yield CybozuTextPost( | ||
content = arrayToString(lines.slice(CONTENT_START_INDEX, lines.length)), | ||
postUser = CybozuTextUser(user), | ||
postedAt = postedAt | ||
) | ||
} | ||
} | ||
|
||
private[parsers] def title(line: String): Either[Throwable, String] = | ||
line match { | ||
case titlePattern(title) => Right(title) | ||
case _ => Left(new RuntimeException("Cannot find topic title")) | ||
} | ||
|
||
private[parsers] def arrayToString(lines: Seq[String]): String = | ||
lines.mkString("\n") | ||
|
||
private[parsers] def user(line: String): Either[ParseError[CybozuTextPost], String] = | ||
line match { | ||
case userPattern(user) => Right(user) | ||
case _ => Left(CannotParseFromString(classOf[CybozuTextPost], "Cannot find user string", line)) | ||
} | ||
|
||
} |
Oops, something went wrong.