Skip to content

Commit

Permalink
refactor: Improve KnoraProject model NO-TICKET (#2648)
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed May 4, 2023
1 parent a5b030f commit 8d08abd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Expand Up @@ -4,6 +4,8 @@
*/

package org.knora.webapi.slice.admin.domain.model
import zio.NonEmptyChunk

import dsp.valueobjects.V2.StringLiteralV2
import org.knora.webapi.slice.resourceinfo.domain.InternalIri

Expand All @@ -12,7 +14,7 @@ case class KnoraProject(
shortname: String,
shortcode: String,
longname: Option[String],
description: List[StringLiteralV2],
description: NonEmptyChunk[StringLiteralV2],
keywords: List[String],
logo: Option[String],
status: Boolean,
Expand Down
Expand Up @@ -67,8 +67,8 @@ final case class KnoraProjectRepoLive(
shortcode <- mapper.getSingleOrFail[StringLiteralV2](ProjectShortcode, propsMap).map(_.value)
longname <- mapper.getSingleOption[StringLiteralV2](ProjectLongname, propsMap).map(_.map(_.value))
description <- mapper
.getListOrFail[StringLiteralV2](ProjectDescription, propsMap)
.map(_.map(desc => V2.StringLiteralV2(desc.value, desc.language)))
.getNonEmptyChunkOrFail[StringLiteralV2](ProjectDescription, propsMap)
.map(_.map(it => V2.StringLiteralV2(it.value, it.language)))
keywords <- mapper.getList[StringLiteralV2](ProjectKeyword, propsMap).map(_.map(_.value).sorted)
logo <- mapper.getSingleOption[StringLiteralV2](ProjectLogo, propsMap).map(_.map(_.value))
status <- mapper.getSingleOrFail[BooleanLiteralV2](Status, propsMap).map(_.value)
Expand Down
Expand Up @@ -61,9 +61,32 @@ final case class PredicateObjectMapper(private val iriConverter: IriConverter) {
* @return a list of values, fails with an [[InconsistentRepositoryDataException]] if key was not present.
*/
def getListOrFail[A <: LiteralV2](key: IRI, propertiesMap: ConstructPredicateObjects): Task[List[A]] =
getListOption[A](key, propertiesMap)
.flatMap(ZIO.fromOption(_))
getListOption[A](key, propertiesMap).some
.orElseFail(InconsistentRepositoryDataException(s"PropertiesMap has no $key defined."))

/**
* Returns a [[NonEmptyChunk]] of values for the given key.
* Fails during runtime if the value could not be cast to the given type.
*
* @param key the key to look for.
* @param propertiesMap the map to look in.
* @tparam A the type of the values.
* @return A [[NonEmptyChunk]] of values,
* Fails with an [[InconsistentRepositoryDataException]] if key was not present.
* Fails with an [[InconsistentRepositoryDataException]] if the list of values was empty.
*/
def getNonEmptyChunkOrFail[A <: LiteralV2](
key: IRI,
propertiesMap: ConstructPredicateObjects
): Task[NonEmptyChunk[A]] =
getListOption[A](key, propertiesMap).some
.orElseFail(InconsistentRepositoryDataException(s"PropertiesMap has no $key defined."))
.flatMap(list =>
ZIO
.fail(InconsistentRepositoryDataException(s"PropertiesMap has $key defined but list of values is empty."))
.when(list.isEmpty)
.as(NonEmptyChunk.fromIterable(list.head, list.tail))
)

/**
* Returns an optional single value for the given key.
Expand Down
Expand Up @@ -5,6 +5,7 @@

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

import zio.NonEmptyChunk
import zio.test.Spec
import zio.test.ZIOSpecDefault
import zio.test.assertTrue
Expand Down Expand Up @@ -46,7 +47,8 @@ object ProjectADMServiceSpec extends ZIOSpecDefault {
shortname = shortname,
shortcode = shortcode,
longname = None,
description = List(StringLiteralV2("description not used in test but is required by constructor", None)),
description =
NonEmptyChunk(StringLiteralV2("description not used in test but is required by constructor", None)),
keywords = List.empty,
logo = None,
status = true,
Expand Down

0 comments on commit 8d08abd

Please sign in to comment.