Commit 4ec593d8 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Remove obsolete serialization code from config fields.

Update tests to reflect changes.
parent 265d1a99
Pipeline #21259 passed with stage
in 2 minutes and 16 seconds
......@@ -18,20 +18,15 @@
package ch.memobase.mapping.fields
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) {
@TypeFor("annotationFieldType", FieldAdapter::class)
sealed class AnnotationField(val annotationFieldType: String) : ConfigField(annotationFieldType) {
@TypeFor("simpleFieldType", FieldAdapter::class)
sealed class SimpleAnnotationField(val simpleFieldType: String) : AnnotationField(simpleFieldType) {
data class ConstantField(val key: String, val constant: String) : SimpleAnnotationField(FieldTypes.constant) {
sealed class ConfigField {
sealed class AnnotationField : ConfigField() {
sealed class SimpleAnnotationField : AnnotationField() {
data class ConstantField(val key: String, val constant: String) : SimpleAnnotationField() {
fun toLiteral(): Literal {
return literal(constant)
}
......@@ -39,23 +34,14 @@ 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) {
sealed class MappedAnnotationField(val key: String, val field: String) : SimpleAnnotationField() {
data class PrefixField(
val prefixKey: String,
val prefix: String,
val prefixField: String
) : MappedAnnotationField(FieldTypes.prefix, prefixKey, prefixField) {
) : MappedAnnotationField(prefixKey, prefixField) {
override fun toLiteral(value: String): Literal {
return literal(prefix + value.trim())
}
......@@ -63,19 +49,10 @@ 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) :
MappedAnnotationField(FieldTypes.direct, directKey, directField) {
MappedAnnotationField(directKey, directField) {
override fun toLiteral(value: String): Literal {
return literal(value)
}
......@@ -83,15 +60,6 @@ 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
......@@ -99,9 +67,8 @@ sealed class ConfigField(val fieldType: String) {
}
}
@TypeFor("complexAnnotationFieldType", FieldAdapter::class)
sealed class ComplexAnnotationField(val complexAnnotationFieldType: String) : AnnotationField(complexAnnotationFieldType) {
data class ListField(val key: String, val fields: List<SimpleAnnotationField>) : ComplexAnnotationField(FieldTypes.list) {
sealed class ComplexAnnotationField() : AnnotationField() {
data class ListField(val key: String, val fields: List<SimpleAnnotationField>) : ComplexAnnotationField() {
fun toLiterals(source: Map<String, Any>): List<Literal> {
return fields.mapNotNull {
when (it) {
......@@ -117,19 +84,10 @@ 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<LanguagePair>) :
ComplexAnnotationField(FieldTypes.language) {
ComplexAnnotationField() {
fun toLangLiterals(source: Map<String, Any>): List<Literal> {
return fields.flatMap { languagePair ->
languagePair.sources.mapNotNull {
......@@ -147,21 +105,12 @@ sealed class ConfigField(val fieldType: String) {
}
}
}
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
}
}
}
}
}
protected fun langLiteral(text: String, language: String): Literal =
ResourceFactory.createLangLiteral(text.trim(), language)
ResourceFactory.createLangLiteral(text.trim(), language)
protected fun literal(text: String): Literal = ResourceFactory.createStringLiteral(text.trim())
}
......
package mapping.fields
import ch.memobase.mapping.fields.ConfigField
import ch.memobase.mapping.fields.ConstantField
import ch.memobase.mapping.fields.DirectMapField
import ch.memobase.mapping.fields.LanguageField
import ch.memobase.mapping.fields.ListField
import ch.memobase.mapping.fields.PrefixField
import com.beust.klaxon.TypeAdapter
import kotlin.reflect.KClass
class FieldAdapter : TypeAdapter<ConfigField> {
override fun classFor(type: Any): KClass<out ConfigField> {
return when (type) {
FieldTypes.constant -> ConstantField::class
FieldTypes.direct -> DirectMapField::class
FieldTypes.prefix -> PrefixField::class
FieldTypes.list -> ListField::class
FieldTypes.language -> LanguageField::class
else -> throw Exception("Could not parse config field instance with type $type.")
}
}
}
package mapping.fields
object FieldTypes {
const val constant = "constant"
const val direct = "direct"
const val prefix = "prefix"
const val language = "language"
const val list = "list"
}
package ch.memobase.test
import ch.memobase.mapping.MapperParsers
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 org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestFieldMapperSerde {
private val klaxon = Klaxon()
@Test
fun `test constant field mapper serialization`() {
val json = klaxon.toJsonString(ConstantFieldMapper(ConstantField("key", "value")))
val parsed = klaxon.parse<ConstantFieldMapper>(json)
assertThat(parsed)
.isNotNull
.isEqualTo(ConstantFieldMapper(ConstantField("key", "value")))
}
@Test
fun `test skos concept field mapper`() {
val mappers = MapperParsers.buildSkosConceptMappers(
"subject", mapOf(
Pair("prefLabel", "TestField"),
Pair("note", mapOf(Pair("const", "ConstValue")))
)
)[0]
val values = klaxon.toJsonString(mappers)
val parsed = klaxon.parse<SkosConceptFieldMapper>(values)
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)
}
}
{"digitalFieldMappers" : [{"field" : {"directField" : "MainID", "directKey" : "main", "annotationFieldType" : "direct", "field" : "MainID", "fieldType" : "direct", "key" : "main", "simpleFieldType" : "direct"}, "rdfType" : "identifiers", "type" : "rico-concept"}], "physicalFieldMappers" : [{"field" : {"directField" : "MainID", "directKey" : "main", "annotationFieldType" : "direct", "field" : "MainID", "fieldType" : "direct", "key" : "main", "simpleFieldType" : "direct"}, "rdfType" : "identifiers", "type" : "rico-concept"}], "recordFieldMappers" : [{"field" : {"directField" : "MainID", "directKey" : "main", "annotationFieldType" : "direct", "field" : "MainID", "fieldType" : "direct", "key" : "main", "simpleFieldType" : "direct"}, "rdfType" : "identifiers", "type" : "rico-concept"}], "recordType" : {"constant" : "Foto", "key" : "type", "annotationFieldType" : "constant", "fieldType" : "constant", "simpleFieldType" : "constant"}, "sponsoredByMemoriav" : true, "uri" : "TestField"}
\ No newline at end of file
{"digitalFieldMappers" : [{"field" : {"directField" : "MainID", "directKey" : "main", "field" : "MainID", "key" : "main"}, "rdfType" : "identifiers", "type" : "rico-concept"}], "physicalFieldMappers" : [{"field" : {"directField" : "MainID", "directKey" : "main", "field" : "MainID", "key" : "main"}, "rdfType" : "identifiers", "type" : "rico-concept"}], "recordFieldMappers" : [{"field" : {"directField" : "MainID", "directKey" : "main", "field" : "MainID", "key" : "main"}, "rdfType" : "identifiers", "type" : "rico-concept"}], "recordType" : {"constant" : "Foto", "key" : "type"}, "sponsoredByMemoriav" : true, "uri" : "TestField"}
\ No newline at end of file
{"digitalFieldMappers" : [], "physicalFieldMappers" : [], "recordFieldMappers" : [{"agentClassType" : "subject", "fields" : [{"fields" : [{"sources" : [{"directField" : "Schlagworte", "directKey" : "prefLabel", "annotationFieldType" : "direct", "field" : "Schlagworte", "fieldType" : "direct", "key" : "prefLabel", "simpleFieldType" : "direct"}], "tag" : "fr"}], "key" : "prefLabel", "annotationFieldType" : "language", "complexAnnotationFieldType" : "language", "fieldType" : "language"}, {"constant" : "Ursprungsfeld: Mots clés", "key" : "editorialNote", "annotationFieldType" : "constant", "fieldType" : "constant", "simpleFieldType" : "constant"}], "type" : "skos-concept"}, {"agentClassType" : "genre", "fields" : [{"fields" : [{"sources" : [{"directField" : "Genre", "directKey" : "prefLabel", "annotationFieldType" : "direct", "field" : "Genre", "fieldType" : "direct", "key" : "prefLabel", "simpleFieldType" : "direct"}], "tag" : "fr"}], "key" : "prefLabel", "annotationFieldType" : "language", "complexAnnotationFieldType" : "language", "fieldType" : "language"}, {"constant" : "Ursprungsfeld: Genre", "key" : "editorialNote", "annotationFieldType" : "constant", "fieldType" : "constant", "simpleFieldType" : "constant"}], "type" : "skos-concept"}], "recordType" : {"constant" : "Foto", "key" : "type", "annotationFieldType" : "constant", "fieldType" : "constant", "simpleFieldType" : "constant"}, "sponsoredByMemoriav" : false, "uri" : "id"}
\ No newline at end of file
{"digitalFieldMappers" : [], "physicalFieldMappers" : [], "recordFieldMappers" : [{"agentClassType" : "subject", "fields" : [{"fields" : [{"sources" : [{"directField" : "Schlagworte", "directKey" : "prefLabel", "field" : "Schlagworte", "key" : "prefLabel"}], "tag" : "fr"}], "key" : "prefLabel"}, {"constant" : "Ursprungsfeld: Mots clés", "key" : "editorialNote"}], "type" : "skos-concept"}, {"agentClassType" : "genre", "fields" : [{"fields" : [{"sources" : [{"directField" : "Genre", "directKey" : "prefLabel", "field" : "Genre", "key" : "prefLabel"}], "tag" : "fr"}], "key" : "prefLabel"}, {"constant" : "Ursprungsfeld: Genre", "key" : "editorialNote"}], "type" : "skos-concept"}], "recordType" : {"constant" : "Foto", "key" : "type"}, "sponsoredByMemoriav" : false, "uri" : "id"}
\ No newline at end of file
......@@ -5,16 +5,6 @@
@prefix ebucore: <http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
_:b0 a rico:CreationRelation ;
rico:creationRelationHasSource <https://memobase.ch/record/rs1-1> ;
rico:creationRelationHasTarget [ a rico:Person ;
rico:agentIsTargetOfCreationRelation
_:b0 ;
rico:name "Hans Zimmer"
] ;
rico:name "Author" ;
rico:type "creator" .
<https://memobase.ch/record/rs1-1>
a rico:Record ;
rdau:P60451 <https://memobase.ch/institution/mrv> ;
......@@ -29,3 +19,13 @@ _:b0 a rico:CreationRelation ;
rico:recordResourceOrInstantiationIsSourceOfCreationRelation
_:b0 ;
rico:type "Foto" .
_:b0 a rico:CreationRelation ;
rico:creationRelationHasSource <https://memobase.ch/record/rs1-1> ;
rico:creationRelationHasTarget [ a rico:Person ;
rico:agentIsTargetOfCreationRelation
_:b0 ;
rico:name "Hans Zimmer"
] ;
rico:name "Author" ;
rico:type "creator" .
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