Commit d934954a authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Remove pair from language fields as it is not serializable.

parent a14ae1aa
Pipeline #15804 passed with stage
in 4 minutes and 3 seconds
......@@ -18,16 +18,19 @@
package ch.memobase.mapping.fields
import ch.memobase.mapping.fields.ConfigField.AnnotationField.SimpleAnnotationField.MappedAnnotationField.DirectMapField
import com.beust.klaxon.TypeFor
import mapping.fields.FieldAdapter
import mapping.fields.FieldTypes
import mapping.fields.LanguagePair
import org.apache.jena.rdf.model.Literal
import org.apache.jena.rdf.model.ResourceFactory
@TypeFor("fieldType", FieldAdapter::class)
sealed class ConfigField(val fieldType: String) {
sealed class AnnotationField(fieldType: String) : ConfigField(fieldType) {
sealed class SimpleAnnotationField(fieldType: String) : AnnotationField(fieldType) {
@TypeFor("simpleFieldType", FieldAdapter::class)
sealed class SimpleAnnotationField(val simpleFieldType: String) : AnnotationField(simpleFieldType) {
data class ConstantField(val key: String, val constant: String) : SimpleAnnotationField(FieldTypes.constant) {
fun toLiteral(): Literal {
return literal(constant)
......@@ -36,6 +39,15 @@ sealed class ConfigField(val fieldType: String) {
fun toLangLiteral(tag: String): Literal {
return langLiteral(constant, tag)
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
return when (other) {
null -> false
!is ConstantField -> false
else -> key == other.key && constant == other.constant
}
}
}
sealed class MappedAnnotationField(fieldType: String, val key: String, val field: String) : SimpleAnnotationField(fieldType) {
......@@ -51,6 +63,15 @@ sealed class ConfigField(val fieldType: String) {
override fun toLangLiteral(value: String, tag: String): Literal {
return langLiteral(prefix + value.trim(), tag)
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
return when (other) {
null -> false
!is PrefixField -> false
else -> key == other.key && prefix == other.prefix && field == other.field
}
}
}
data class DirectMapField(val directKey: String, val directField: String) :
......@@ -62,6 +83,15 @@ sealed class ConfigField(val fieldType: String) {
override fun toLangLiteral(value: String, tag: String): Literal {
return langLiteral(value, tag)
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
return when (other) {
null -> false
!is DirectMapField -> false
else -> key == other.key && field == other.field
}
}
}
abstract fun toLiteral(value: String): Literal
......@@ -86,27 +116,45 @@ sealed class ConfigField(val fieldType: String) {
}
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
return when (other) {
null -> false
!is ListField -> false
else -> key == other.key && fields == other.fields
}
}
}
data class LanguageField(val key: String, val fields: List<Pair<String, List<SimpleAnnotationField>>>) :
data class LanguageField(val key: String, val fields: List<LanguagePair>) :
ComplexAnnotationField(FieldTypes.language) {
fun toLangLiterals(source: Map<String, Any>): List<Literal> {
return fields.flatMap { pairs ->
pairs.second.mapNotNull {
return fields.flatMap { languagePair ->
languagePair.sources.mapNotNull {
when (it) {
is MappedAnnotationField ->
FieldParsers.unpackSource(it.field, source).let { value ->
if (value != null) {
it.toLangLiteral(value, pairs.first)
it.toLangLiteral(value, languagePair.tag)
} else {
null
}
}
is ConstantField -> it.toLangLiteral(pairs.first)
is ConstantField -> it.toLangLiteral(languagePair.tag)
}
}
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
return when (other) {
null -> false
!is LanguageField -> false
else -> key == other.key && fields == other.fields
}
}
}
}
}
......
......@@ -19,6 +19,7 @@
package ch.memobase.mapping.fields
import ch.memobase.mapping.exceptions.InvalidMappingException
import mapping.fields.LanguagePair
@Suppress("UNCHECKED_CAST")
object FieldParsers {
......@@ -81,7 +82,7 @@ object FieldParsers {
throw InvalidMappingException("Invalid structure for prefix field in section ${entry.key}.")
}
} else {
val mutableList = mutableListOf<Pair<String, List<SimpleAnnotationField>>>()
val mutableList = mutableListOf<LanguagePair>()
for (k in value.keys) {
if (k is String && listOf("de", "it", "fr").contains(k)) {
val resultAnnotations = when (val items = value[k]) {
......@@ -94,7 +95,7 @@ object FieldParsers {
if (resultAnnotations.isEmpty()) {
throw InvalidMappingException("No valid definitions found under language tag $k in section ${entry.key}")
}
mutableList.add(Pair(k, resultAnnotations))
mutableList.add(LanguagePair(k, resultAnnotations))
}
}
if (mutableList.isNotEmpty()) {
......
package mapping.fields
import ch.memobase.mapping.fields.SimpleAnnotationField
data class LanguagePair(
val tag: String,
val sources: List<SimpleAnnotationField>
)
......@@ -29,4 +29,13 @@ class LanguageFieldMapper(val languageField: LanguageField) : AbstractFieldMappe
subject.addLiteral(languageField.key, it)
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
return when (other) {
null -> false
!is LanguageFieldMapper -> false
else -> languageField == other.languageField
}
}
}
package ch.memobase.test
import ch.memobase.mapping.fields.ConstantField
import ch.memobase.mapping.fields.FieldParsers
import ch.memobase.mapping.fields.LanguageField
import ch.memobase.mapping.mappers.ConstantFieldMapper
import ch.memobase.mapping.mappers.LanguageFieldMapper
import ch.memobase.mapping.mappers.SkosConceptFieldMapper
import com.beust.klaxon.Klaxon
import mapping.MapperParsers
......@@ -36,4 +39,22 @@ class TestFieldMapperSerde {
assertThat(parsed)
.isEqualTo(mappers)
}
@Test
fun `test language field mapper`() {
val field = FieldParsers.parseAnnotationField(
mapOf(
Pair("key", mapOf(
Pair("de", "DirectField1"),
Pair("fr", "DirectField2"),
Pair("it", "DirectField3")
))
).entries.first()
)
val mapper = LanguageFieldMapper(field as LanguageField)
val values = klaxon.toJsonString(mapper)
val parsed = klaxon.parse<LanguageFieldMapper>(values)
assertThat(parsed)
.isEqualTo(mapper)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment