Skip to content

Commit

Permalink
finetuning, fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Nov 9, 2023
1 parent 05588a0 commit 0139859
Showing 1 changed file with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@

package org.knora.webapi.slice.admin.domain.model

import dsp.errors.ValidationException
import dsp.valueobjects.{Iri, V2}
import org.knora.webapi.slice.admin.domain.model.KnoraProject._
import org.knora.webapi.slice.resourceinfo.domain.InternalIri
import sttp.tapir.Schema
import zio.NonEmptyChunk
import zio.json._
import zio.prelude.Validation

import scala.util.matching.Regex

import dsp.errors.ValidationException
import dsp.valueobjects.Iri
import dsp.valueobjects.V2
import org.knora.webapi.slice.admin.domain.model.KnoraProject._
import org.knora.webapi.slice.resourceinfo.domain.InternalIri

case class KnoraProject(
id: InternalIri,
shortname: Shortname,
Expand All @@ -37,17 +35,17 @@ object KnoraProject {

object Shortcode {

private val shortcodeRegex: Regex = ("^\\p{XDigit}{4,4}$").r

implicit val codec: JsonCodec[Shortcode] =
JsonCodec[String].transformOrFail(value => Shortcode.make(value).toEitherWith(e => e.head.getMessage), _.value)
private val shortcodeRegex: Regex = "^\\p{XDigit}{4}$".r

def unsafeFrom(str: String): Shortcode = make(str).fold(e => throw e.head, identity)

def make(value: String): Validation[ValidationException, Shortcode] =
if (value.isEmpty) Validation.fail(ValidationException("Shortcode cannot be empty."))

Check notice on line 43 in webapi/src/main/scala/org/knora/webapi/slice/admin/domain/model/KnoraProject.scala

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

webapi/src/main/scala/org/knora/webapi/slice/admin/domain/model/KnoraProject.scala#L43

Consider using case matching instead of else if blocks
else if (shortcodeRegex.matches(value.toUpperCase)) { Validation.succeed(Shortcode(value.toUpperCase)) }
else { Validation.fail(ValidationException(s"Shortcode is invalid: $value")) }
else if (shortcodeRegex.matches(value.toUpperCase)) Validation.succeed(Shortcode(value.toUpperCase))
else Validation.fail(ValidationException(s"Shortcode is invalid: $value"))

implicit val codec: JsonCodec[Shortcode] =
JsonCodec[String].transformOrFail(Shortcode.make(_).toEitherWith(e => e.head.getMessage), _.value)
}

final case class Shortname private (value: String) extends AnyVal
Expand All @@ -56,9 +54,6 @@ object KnoraProject {

private val shortnameRegex: Regex = "^[a-zA-Z][a-zA-Z0-9_-]{2,19}$".r

implicit val codec: JsonCodec[Shortname] =
JsonCodec[String].transformOrFail(value => Shortname.make(value).toEitherWith(e => e.head.getMessage), _.value)

def unsafeFrom(str: String): Shortname = make(str).fold(e => throw e.head, identity)

def make(value: String): Validation[ValidationException, Shortname] =
Expand All @@ -73,80 +68,85 @@ object KnoraProject {
.mapError(_ => ValidationException(s"Shortname is invalid: $value"))
.map(Shortname(_))
}

implicit val codec: JsonCodec[Shortname] =
JsonCodec[String].transformOrFail(Shortname.make(_).toEitherWith(e => e.head.getMessage), _.value)
}

final case class Longname private (value: String) extends AnyVal

object Longname {

implicit val codec: JsonCodec[Longname] =
JsonCodec[String].transformOrFail(Longname.make(_).toEitherWith(e => e.head.getMessage), _.value)

private val longnameRegex: Regex = "^.{3,256}$".r

def unsafeFrom(str: String): Longname = make(str).fold(e => throw e.head, identity)

def make(value: String): Validation[ValidationException, Longname] =
if (longnameRegex.matches(value)) Validation.succeed(Longname(value))
else Validation.fail(ValidationException("Longname must be 3 to 256 characters long."))

implicit val codec: JsonCodec[Longname] =
JsonCodec[String].transformOrFail(Longname.make(_).toEitherWith(e => e.head.getMessage), _.value)
}

final case class Description private (value: V2.StringLiteralV2)

object Description {

implicit val codec: JsonCodec[Description] = JsonCodec[V2.StringLiteralV2]
.transformOrFail(value => Description.make(value).toEitherWith(e => e.head.getMessage), _.value)

def unsafeFrom(str: V2.StringLiteralV2): Description = make(str).fold(e => throw e.head, identity)

def make(value: V2.StringLiteralV2): Validation[ValidationException, Description] =
if (value.value.length >= 3 && value.value.length <= 40960) { Validation.succeed(Description(value)) }
def make(literal: V2.StringLiteralV2): Validation[ValidationException, Description] =
if (literal.value.length >= 3 && literal.value.length <= 40960) Validation.succeed(Description(literal))
else Validation.fail(ValidationException("Description must be 3 to 40960 characters long."))

implicit val codec: JsonCodec[Description] =
JsonCodec[V2.StringLiteralV2].transformOrFail(Description.make(_).toEitherWith(e => e.head.getMessage), _.value)
}

final case class Keyword private (value: String) extends AnyVal

object Keyword {

implicit val codec: JsonCodec[Keyword] =
JsonCodec[String].transformOrFail(it => Keyword.make(it).toEitherWith(e => e.head.getMessage), _.value)

private val keywordRegex: Regex = "^.{3,64}$".r

def unsafeFrom(str: String): Keyword = make(str).fold(e => throw e.head, identity)

def make(value: String): Validation[ValidationException, Keyword] =
if (keywordRegex.matches(value)) Validation.succeed(Keyword(value))
else Validation.fail(ValidationException("Keyword must be 3 to 64 characters long."))

implicit val codec: JsonCodec[Keyword] =
JsonCodec[String].transformOrFail(Keyword.make(_).toEitherWith(e => e.head.getMessage), _.value)
}

final case class Logo private (value: String) extends AnyVal

object Logo {

implicit val codec: JsonCodec[Logo] =
JsonCodec[String].transformOrFail(value => Logo.make(value).toEitherWith(e => e.head.getMessage), _.value)

def unsafeFrom(str: String): Logo = make(str).fold(e => throw e.head, identity)

def make(value: String): Validation[ValidationException, Logo] =
if (value.isEmpty) Validation.fail(ValidationException("Logo cannot be empty."))
else Validation.succeed(Logo(value))

implicit val codec: JsonCodec[Logo] =
JsonCodec[String].transformOrFail(Logo.make(_).toEitherWith(e => e.head.getMessage), _.value)
}

trait ProjectStatus { def value: Boolean }

object ProjectStatus {

case object Active extends ProjectStatus { val value = true }
case object Inactive extends ProjectStatus { val value = false }

def from(value: Boolean): ProjectStatus = if (value) Active else Inactive

implicit val codec: JsonCodec[ProjectStatus] =
JsonCodec[Boolean].transformOrFail(value => Right(ProjectStatus.from(value)), _.value)
JsonCodec[Boolean].transformOrFail(b => Right(ProjectStatus.from(b)), _.value)

implicit val schema: Schema[ProjectStatus] = Schema.schemaForBoolean.map(b => Some(ProjectStatus.from(b)))(_.value)

def from(value: Boolean): ProjectStatus = if (value) Active else Inactive
}

trait ProjectSelfJoin { def value: Boolean }
Expand All @@ -156,12 +156,12 @@ object KnoraProject {
case object CanJoin extends ProjectSelfJoin { val value = true }
case object CannotJoin extends ProjectSelfJoin { val value = false }

def from(value: Boolean): ProjectSelfJoin = if (value) CanJoin else CannotJoin

implicit val codec: JsonCodec[ProjectSelfJoin] =
JsonCodec[Boolean].transformOrFail(value => Right(ProjectSelfJoin.from(value)), _.value)
JsonCodec[Boolean].transformOrFail(b => Right(ProjectSelfJoin.from(b)), _.value)

implicit val schema: Schema[ProjectSelfJoin] =
Schema.schemaForBoolean.map(b => Some(ProjectSelfJoin.from(b)))(_.value)

def from(value: Boolean): ProjectSelfJoin = if (value) CanJoin else CannotJoin
}
}

0 comments on commit 0139859

Please sign in to comment.