Apply scalafmt to all files

This commit is contained in:
Eike Kettner
2019-12-30 21:44:13 +01:00
parent 57e274e2b0
commit fc3e22e399
133 changed files with 3003 additions and 2112 deletions

View File

@ -1,12 +1,14 @@
package docspell.common
case class Banner( component: String
, version: String
, gitHash: Option[String]
, jdbcUrl: LenientUri
, configFile: Option[String]
, appId: Ident
, baseUrl: LenientUri) {
case class Banner(
component: String,
version: String,
gitHash: Option[String],
jdbcUrl: LenientUri,
configFile: Option[String],
appId: Ident,
baseUrl: LenientUri
) {
private val banner =
"""______ _ _
@ -17,16 +19,16 @@ case class Banner( component: String
||___/ \___/ \___|___/ .__/ \___|_|_|
| | |
|""".stripMargin +
s""" |_| v$version (#${gitHash.map(_.take(8)).getOrElse("")})"""
s""" |_| v$version (#${gitHash.map(_.take(8)).getOrElse("")})"""
def render(prefix: String): String = {
val text = banner.split('\n').toList ++ List(
s"<< $component >>"
, s"Id: ${appId.id}"
, s"Base-Url: ${baseUrl.asString}"
, s"Database: ${jdbcUrl.asString}"
, s"Config: ${configFile.getOrElse("")}"
, ""
s"<< $component >>",
s"Id: ${appId.id}",
s"Base-Url: ${baseUrl.asString}",
s"Database: ${jdbcUrl.asString}",
s"Config: ${configFile.getOrElse("")}",
""
)
text.map(line => s"$prefix $line").mkString("\n")

View File

@ -21,7 +21,7 @@ object Direction {
str.toLowerCase match {
case "incoming" => Right(Incoming)
case "outgoing" => Right(Outgoing)
case _ => Left(s"No direction: $str")
case _ => Left(s"No direction: $str")
}
def unsafe(str: String): Direction =

View File

@ -3,9 +3,7 @@ package docspell.common
import io.circe._
import io.circe.generic.semiauto._
case class IdRef(id: Ident, name: String) {
}
case class IdRef(id: Ident, name: String) {}
object IdRef {
@ -13,4 +11,4 @@ object IdRef {
deriveEncoder[IdRef]
implicit val jsonDecoder: Decoder[IdRef] =
deriveDecoder[IdRef]
}
}

View File

@ -10,18 +10,18 @@ sealed trait ItemState { self: Product =>
object ItemState {
case object Premature extends ItemState
case object Premature extends ItemState
case object Processing extends ItemState
case object Created extends ItemState
case object Confirmed extends ItemState
case object Created extends ItemState
case object Confirmed extends ItemState
def fromString(str: String): Either[String, ItemState] =
str.toLowerCase match {
case "premature" => Right(Premature)
case "premature" => Right(Premature)
case "processing" => Right(Processing)
case "created" => Right(Created)
case "confirmed" => Right(Confirmed)
case _ => Left(s"Invalid item state: $str")
case "created" => Right(Created)
case "confirmed" => Right(Confirmed)
case _ => Left(s"Invalid item state: $str")
}
def unsafe(str: String): ItemState =
@ -32,4 +32,3 @@ object ItemState {
implicit val jsonEncoder: Encoder[ItemState] =
Encoder.encodeString.contramap(_.name)
}

View File

@ -31,14 +31,12 @@ object Language {
def fromString(str: String): Either[String, Language] = {
val lang = str.toLowerCase
all.find(_.allNames.contains(lang)).
toRight(s"Unsupported or invalid language: $str")
all.find(_.allNames.contains(lang)).toRight(s"Unsupported or invalid language: $str")
}
def unsafe(str: String): Language =
fromString(str).fold(sys.error, identity)
implicit val jsonDecoder: Decoder[Language] =
Decoder.decodeString.emap(fromString)
implicit val jsonEncoder: Encoder[Language] =

View File

@ -11,8 +11,8 @@ sealed trait LogLevel { self: Product =>
object LogLevel {
case object Debug extends LogLevel { val toInt = 0 }
case object Info extends LogLevel { val toInt = 1 }
case object Warn extends LogLevel { val toInt = 2 }
case object Info extends LogLevel { val toInt = 1 }
case object Warn extends LogLevel { val toInt = 2 }
case object Error extends LogLevel { val toInt = 3 }
def fromInt(n: Int): LogLevel =
@ -26,12 +26,12 @@ object LogLevel {
def fromString(str: String): Either[String, LogLevel] =
str.toLowerCase match {
case "debug" => Right(Debug)
case "info" => Right(Info)
case "warn" => Right(Warn)
case "debug" => Right(Debug)
case "info" => Right(Info)
case "warn" => Right(Warn)
case "warning" => Right(Warn)
case "error" => Right(Error)
case _ => Left(s"Invalid log-level: $str")
case "error" => Right(Error)
case _ => Left(s"Invalid log-level: $str")
}
def unsafeString(str: String): LogLevel =
@ -41,4 +41,4 @@ object LogLevel {
Decoder.decodeString.emap(fromString)
implicit val jsonEncoder: Encoder[LogLevel] =
Encoder.encodeString.contramap(_.name)
}
}

View File

@ -12,7 +12,7 @@ case class MimeType(primary: String, sub: String) {
def matches(other: MimeType): Boolean =
primary == other.primary &&
(sub == other.sub || sub == "*" )
(sub == other.sub || sub == "*")
}
object MimeType {
@ -26,9 +26,10 @@ object MimeType {
def image(sub: String): MimeType =
MimeType("image", partFromString(sub).throwLeft)
private[this] val validChars: Set[Char] = (('A' to 'Z') ++ ('a' to 'z') ++ ('0' to '9') ++ "*-").toSet
private[this] val validChars: Set[Char] =
(('A' to 'Z') ++ ('a' to 'z') ++ ('0' to '9') ++ "*-").toSet
def parse(str: String): Either[String, MimeType] = {
def parse(str: String): Either[String, MimeType] =
str.indexOf('/') match {
case -1 => Left(s"Invalid MIME type: $str")
case n =>
@ -37,7 +38,6 @@ object MimeType {
sub <- partFromString(str.substring(n + 1))
} yield MimeType(prim.toLowerCase, sub.toLowerCase)
}
}
def unsafe(str: String): MimeType =
parse(str).throwLeft
@ -47,12 +47,12 @@ object MimeType {
else Left(s"Invalid identifier: $s. Allowed chars: ${validChars.mkString}")
val octetStream = application("octet-stream")
val pdf = application("pdf")
val png = image("png")
val jpeg = image("jpeg")
val tiff = image("tiff")
val html = text("html")
val plain = text("plain")
val pdf = application("pdf")
val png = image("png")
val jpeg = image("jpeg")
val tiff = image("tiff")
val html = text("html")
val plain = text("plain")
implicit val jsonEncoder: Encoder[MimeType] =
Encoder.encodeString.contramap(_.asString)

View File

@ -2,6 +2,4 @@ package docspell.common
import java.time.LocalDate
case class NerDateLabel(date: LocalDate, label: NerLabel) {
}
case class NerDateLabel(date: LocalDate, label: NerLabel) {}

View File

@ -3,9 +3,7 @@ package docspell.common
import io.circe.generic.semiauto._
import io.circe.{Decoder, Encoder}
case class NerLabel(label: String, tag: NerTag, startPosition: Int, endPosition: Int) {
}
case class NerLabel(label: String, tag: NerTag, startPosition: Int, endPosition: Int) {}
object NerLabel {
implicit val jsonEncoder: Encoder[NerLabel] = deriveEncoder[NerLabel]

View File

@ -10,13 +10,13 @@ sealed trait NodeType { self: Product =>
object NodeType {
case object Restserver extends NodeType
case object Joex extends NodeType
case object Joex extends NodeType
def fromString(str: String): Either[String, NodeType] =
str.toLowerCase match {
case "restserver" => Right(Restserver)
case "joex" => Right(Joex)
case _ => Left(s"Invalid node type: $str")
case "joex" => Right(Joex)
case _ => Left(s"Invalid node type: $str")
}
def unsafe(str: String): NodeType =

View File

@ -4,7 +4,7 @@ import io.circe.{Decoder, Encoder}
final class Password(val pass: String) extends AnyVal {
def isEmpty: Boolean= pass.isEmpty
def isEmpty: Boolean = pass.isEmpty
override def toString: String =
if (pass.isEmpty) "<empty>" else "***"

View File

@ -16,25 +16,23 @@ object Priority {
case object Low extends Priority
def fromString(str: String): Either[String, Priority] =
str.toLowerCase match {
case "high" => Right(High)
case "low" => Right(Low)
case _ => Left(s"Invalid priority: $str")
case "low" => Right(Low)
case _ => Left(s"Invalid priority: $str")
}
def unsafe(str: String): Priority =
fromString(str).fold(sys.error, identity)
def fromInt(n: Int): Priority =
if (n <= toInt(Low)) Low
else High
def toInt(p: Priority): Int =
p match {
case Low => 0
case Low => 0
case High => 10
}

View File

@ -6,14 +6,13 @@ import ProcessItemArgs._
case class ProcessItemArgs(meta: ProcessMeta, files: List[File]) {
def makeSubject: String = {
def makeSubject: String =
files.flatMap(_.name) match {
case Nil => s"${meta.sourceAbbrev}: No files"
case n :: Nil => n
case Nil => s"${meta.sourceAbbrev}: No files"
case n :: Nil => n
case n1 :: n2 :: Nil => s"$n1, $n2"
case _ => s"${files.size} files from ${meta.sourceAbbrev}"
case _ => s"${files.size} files from ${meta.sourceAbbrev}"
}
}
}
@ -21,11 +20,13 @@ object ProcessItemArgs {
val taskName = Ident.unsafe("process-item")
case class ProcessMeta( collective: Ident
, language: Language
, direction: Option[Direction]
, sourceAbbrev: String
, validFileTypes: Seq[MimeType])
case class ProcessMeta(
collective: Ident,
language: Language,
direction: Option[Direction],
sourceAbbrev: String,
validFileTypes: Seq[MimeType]
)
object ProcessMeta {
implicit val jsonEncoder: Encoder[ProcessMeta] = deriveEncoder[ProcessMeta]

View File

@ -30,9 +30,7 @@ object Timestamp {
def current[F[_]: Sync]: F[Timestamp] =
Sync[F].delay(Timestamp(Instant.now))
implicit val encodeTimestamp: Encoder[Timestamp] =
implicit val encodeTimestamp: Encoder[Timestamp] =
BaseJsonCodecs.encodeInstantEpoch.contramap(_.value)
implicit val decodeTimestamp: Decoder[Timestamp] =

View File

@ -12,19 +12,18 @@ object UserState {
/** The user is blocked by an admin. */
case object Disabled extends UserState
def fromString(s: String): Either[String, UserState] =
s.toLowerCase match {
case "active" => Right(Active)
case "active" => Right(Active)
case "disabled" => Right(Disabled)
case _ => Left(s"Not a state value: $s")
case _ => Left(s"Not a state value: $s")
}
def unsafe(str: String): UserState =
fromString(str).fold(sys.error, identity)
def asString(s: UserState): String = s match {
case Active => "active"
case Active => "active"
case Disabled => "disabled"
}
@ -34,4 +33,4 @@ object UserState {
implicit val userStateDecoder: Decoder[UserState] =
Decoder.decodeString.emap(UserState.fromString)
}
}

View File

@ -4,18 +4,18 @@ trait EitherSyntax {
implicit final class LeftStringEitherOps[A](e: Either[String, A]) {
def throwLeft: A = e match {
case Right(a) => a
case Right(a) => a
case Left(err) => sys.error(err)
}
}
implicit final class ThrowableLeftEitherOps[A](e: Either[Throwable, A]) {
def throwLeft: A = e match {
case Right(a) => a
case Right(a) => a
case Left(err) => throw err
}
}
}
object EitherSyntax extends EitherSyntax
object EitherSyntax extends EitherSyntax

View File

@ -11,13 +11,18 @@ trait StreamSyntax {
implicit class StringStreamOps[F[_]](s: Stream[F, String]) {
def parseJsonAs[A](implicit d: Decoder[A], F: Sync[F]): F[Either[Throwable, A]] =
s.fold("")(_ + _).
compile.last.
map(optStr => for {
str <- optStr.map(_.trim).toRight(new Exception("Empty string cannot be parsed into a value"))
json <- parse(str).leftMap(_.underlying)
value <- json.as[A]
} yield value)
s.fold("")(_ + _)
.compile
.last
.map(optStr =>
for {
str <- optStr
.map(_.trim)
.toRight(new Exception("Empty string cannot be parsed into a value"))
json <- parse(str).leftMap(_.underlying)
value <- json.as[A]
} yield value
)
}