Skip to content

Commit

Permalink
refactor: reorder
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Nov 21, 2024
1 parent ad1ad90 commit ffc3e0b
Showing 1 changed file with 53 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,38 +580,35 @@ case class ReadResourceV2(

object ReadResourceV2 {

private def setCopyrightAndLicenceIfMissingOnLinkedResources(
def setCopyrightAndLicenceIfMissing(
copyright: Option[CopyrightAttribution],
license: Option[License],
): ReadResourceV2 => ReadResourceV2 =
rr => {
val newValues: Map[SmartIri, Seq[ReadValueV2]] = rr.values.map((iri: SmartIri, seq: Seq[ReadValueV2]) =>
(
iri,
seq.map {
case lv: ReadLinkValueV2 =>
linkValueFromReadValue
.andThen(nestedResourceFromLinkValueContent)
.modifyOption(setCopyrightAndLicenceIfMissingResourceValues(copyright, license))(lv)
.getOrElse(lv)
case other => other
},
),
)
rr.copy(values = newValues)
}
setCopyrightAndLicenceIfMissingResourceValues(copyright, license)
.andThen(setCopyrightAndLicenceIfMissingOnLinkedResources(copyright, license))

private val linkValueFromReadValue = Optional[ReadValueV2, LinkValueContentV2] {
case lv: ReadLinkValueV2 => Some(lv.valueContent)
case _ => None
}(lv => {
case rv: ReadLinkValueV2 => rv.copy(valueContent = lv)
case rv: ReadOtherValueV2 => rv.copy(valueContent = lv)
case rv: ReadTextValueV2 => rv
})
private def setCopyrightAndLicenceIfMissingResourceValues(
copyright: Option[CopyrightAttribution],
license: Option[License],
): ReadResourceV2 => ReadResourceV2 =
setIfMissing(licenseLens)(license).andThen(setIfMissing(copyrightAttributionLens)(copyright))

private val copyrightAttributionLens = GenLens[FileValueV2](_.copyrightAttribution)
private val licenseLens = GenLens[FileValueV2](_.license)

private def ifMissingMapper[T](optional: Optional[ReadValueV2, Option[T]], newValue: Option[T]) =
(smartIri: SmartIri, seq: Seq[ReadValueV2]) =>
(
smartIri,
seq.map { readValue =>
optional.getOption(readValue).flatten match
case Some(_) => readValue
case None => optional.replace(newValue)(readValue)
},
)

private val nestedResourceFromLinkValueContent =
Optional[LinkValueContentV2, ReadResourceV2](_.nestedResource)(rr => lv => lv.copy(nestedResource = Some(rr)))
private def setIfMissing[T](opt: Optional[FileValueV2, Option[T]]): Option[T] => ReadResourceV2 => ReadResourceV2 =
value => rr => rr.copy(values = rr.values.map(ifMissingMapper(fileValueFromReadValue.andThen(opt), value)(_, _)))

private val fileValueContentLens: Lens[ReadValueV2, ValueContentV2] =
Lens[ReadValueV2, ValueContentV2](_.valueContent)(fc => {
Expand Down Expand Up @@ -642,52 +639,41 @@ object ReadResourceV2 {
case vc: TextFileValueContentV2 => vc.copy(fileValue = fv)
})

private val copyrightAttributionLens: Lens[FileValueV2, Option[CopyrightAttribution]] =
GenLens[FileValueV2](_.copyrightAttribution)

private val licenseLens: Lens[FileValueV2, Option[License]] =
GenLens[FileValueV2](_.license)

private val fileValueFromReadValue: Optional[ReadValueV2, FileValueV2] =
fileValueContentLens.andThen(fileValueContentPrism).andThen(fileValueLens)

private def setIfMissing[T](
optional: Optional[ReadValueV2, Option[T]],
): Option[T] => ReadResourceV2 => ReadResourceV2 =
newValue =>
readResource => {
val newValues: Map[SmartIri, Seq[ReadValueV2]] =
readResource.values.map((iri: SmartIri, seq: Seq[ReadValueV2]) =>
(
iri,
seq.map { readValue =>
optional.getOption(readValue).flatten match {
case Some(_) => readValue
case None => optional.replace(newValue)(readValue)
}
},
),
)
readResource.copy(values = newValues)
}

private def setCopyrightAndLicenceIfMissingResourceValues(
copyright: Option[CopyrightAttribution],
license: Option[License],
): ReadResourceV2 => ReadResourceV2 = {
val licenseOptional: Optional[ReadValueV2, Option[License]] =
fileValueFromReadValue.andThen(licenseLens)
val copyrightAttributionOptional: Optional[ReadValueV2, Option[CopyrightAttribution]] =
fileValueFromReadValue.andThen(copyrightAttributionLens)
setIfMissing(licenseOptional)(license).andThen(setIfMissing(copyrightAttributionOptional)(copyright))
}

def setCopyrightAndLicenceIfMissing(
private def setCopyrightAndLicenceIfMissingOnLinkedResources(
copyright: Option[CopyrightAttribution],
license: Option[License],
): ReadResourceV2 => ReadResourceV2 =
setCopyrightAndLicenceIfMissingResourceValues(copyright, license)
.andThen(setCopyrightAndLicenceIfMissingOnLinkedResources(copyright, license))
rr => rr.copy(values = rr.values.map(linkValueIfMissingMapper(copyright, license)(_, _)))

private def linkValueIfMissingMapper(copyright: Option[CopyrightAttribution], license: Option[License]) =
(iri: SmartIri, seq: Seq[ReadValueV2]) =>
(
iri,
seq.map {
case lv: ReadLinkValueV2 =>
linkValueFromReadValue
.andThen(nestedResourceFromLinkValueContent)
.modifyOption(setCopyrightAndLicenceIfMissingResourceValues(copyright, license))(lv)
.getOrElse(lv)
case other => other
},
)

private val linkValueFromReadValue = Optional[ReadValueV2, LinkValueContentV2] {
case lv: ReadLinkValueV2 => Some(lv.valueContent)
case _ => None
}(lv => {
case rv: ReadLinkValueV2 => rv.copy(valueContent = lv)
case rv: ReadOtherValueV2 => rv.copy(valueContent = lv)
case rv: ReadTextValueV2 => rv
})

private val nestedResourceFromLinkValueContent =
Optional[LinkValueContentV2, ReadResourceV2](_.nestedResource)(rr => lv => lv.copy(nestedResource = Some(rr)))

}

/**
Expand Down

0 comments on commit ffc3e0b

Please sign in to comment.