-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #578 from NDLANO/optional-language-rework
Convert `disclaimer` to new `OptLanguageFields` type
- Loading branch information
Showing
35 changed files
with
560 additions
and
103 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
17 changes: 17 additions & 0 deletions
17
...-api/src/main/scala/no/ndla/articleapi/db/migration/V56__DisclaimerToLanguageFields.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,17 @@ | ||
/* | ||
* Part of NDLA article-api | ||
* Copyright (C) 2025 NDLA | ||
* | ||
* See LICENSE | ||
* | ||
*/ | ||
|
||
package no.ndla.articleapi.db.migration | ||
|
||
import no.ndla.database.LanguageFieldMigration | ||
|
||
class V56__DisclaimerToLanguageFields extends LanguageFieldMigration { | ||
override val columnName: String = "document" | ||
override val tableName: String = "contentdata" | ||
override val fieldName: String = "disclaimer" | ||
} |
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
37 changes: 37 additions & 0 deletions
37
.../src/test/scala/no/ndla/articleapi/db/migration/V55__DisclaimerToLanguageFieldsTest.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,37 @@ | ||
/* | ||
* Part of NDLA article-api | ||
* Copyright (C) 2024 NDLA | ||
* | ||
* See LICENSE | ||
*/ | ||
|
||
package no.ndla.articleapi.db.migration | ||
|
||
import no.ndla.articleapi.{TestEnvironment, UnitSuite} | ||
|
||
class V55__DisclaimerToLanguageFieldsTest extends UnitSuite with TestEnvironment { | ||
test("That old disclaimers are migrated to new language fields") { | ||
val oldDocument = | ||
"""{"disclaimer":[{"disclaimer":"Dette er bokmål","language":"nb"},{"disclaimer":"Dette er nynorsk","language":"nn"}]}""" | ||
val expectedResult = """{"disclaimer":{"nb":"Dette er bokmål","nn":"Dette er nynorsk"}}""" | ||
val migration = new V56__DisclaimerToLanguageFields | ||
val result = migration.convertColumn(oldDocument) | ||
result should be(expectedResult) | ||
} | ||
|
||
test("That no old disclaimers are migrated to new language fields") { | ||
val oldDocument = """{}""" | ||
val expectedResult = """{"disclaimer":{}}""" | ||
val migration = new V56__DisclaimerToLanguageFields | ||
val result = migration.convertColumn(oldDocument) | ||
result should be(expectedResult) | ||
} | ||
|
||
test("That null disclaimers are migrated to new language fields") { | ||
val oldDocument = """{"disclaimer":null}""" | ||
val expectedResult = """{"disclaimer":{}}""" | ||
val migration = new V56__DisclaimerToLanguageFields | ||
val result = migration.convertColumn(oldDocument) | ||
result should be(expectedResult) | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
common/src/main/scala/no/ndla/common/model/domain/language/LanguageFields.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,38 @@ | ||
/* | ||
* Part of NDLA common | ||
* Copyright (C) 2025 NDLA | ||
* | ||
* See LICENSE | ||
* | ||
*/ | ||
|
||
package no.ndla.common.model.domain.language | ||
|
||
import io.circe.* | ||
import io.circe.syntax.EncoderOps | ||
import no.ndla.language.Language | ||
import no.ndla.language.model.{BaseWithLanguageAndValue, WithLanguageAndValue} | ||
|
||
case class LanguageFields[T](internal: Map[String, T]) { | ||
def getWithLanguageFields: Seq[WithLanguageAndValue[T]] = internal.map { case (language, value) => | ||
BaseWithLanguageAndValue(language, value) | ||
}.toSeq | ||
def get(language: String): Option[WithLanguageAndValue[T]] = | ||
internal.get(language).map(BaseWithLanguageAndValue(language, _)) | ||
def findByLanguageOrBestEffort(language: String): Option[WithLanguageAndValue[T]] = | ||
Language.findByLanguageOrBestEffort(getWithLanguageFields, language) | ||
} | ||
|
||
object LanguageFields { | ||
def empty[T]: LanguageFields[T] = LanguageFields(Map.empty) | ||
def fromFields[T](fields: Seq[WithLanguageAndValue[T]]): LanguageFields[T] = { | ||
val underlyingMap = fields.map(f => f.language -> f.value).toMap | ||
LanguageFields(underlyingMap) | ||
} | ||
|
||
implicit def encoder[T: Encoder]: Encoder[LanguageFields[T]] = Encoder.instance { lf => lf.internal.asJson } | ||
implicit def decoder[T: Decoder]: Decoder[LanguageFields[T]] = Decoder.instance { json => | ||
json.as[Map[String, T]].map { m => LanguageFields(m) } | ||
|
||
} | ||
} |
Oops, something went wrong.