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

Use toLiterals & toLangLiterals utility functions.

parent c2affc40
Pipeline #8915 passed with stages
in 7 minutes and 20 seconds
......@@ -67,10 +67,17 @@ sealed class ConfigField {
sealed class ComplexAnnotationField : AnnotationField() {
data class ListField(val key: String, val fields: List<SimpleAnnotationField>) : ComplexAnnotationField() {
fun toLiterals(value: String): List<Literal> {
return fields.map {
fun toLiterals(source: Map<String, String>): List<Literal> {
return fields.mapNotNull {
when (it) {
is MappedAnnotationField -> it.toLiteral(value)
is MappedAnnotationField ->
source[it.field].let { value ->
if (value != null) {
it.toLiteral(value)
} else {
null
}
}
is ConstantField -> it.toLiteral()
}
}
......@@ -79,11 +86,18 @@ sealed class ConfigField {
data class LanguageField(val key: String, val fields: List<Pair<String, List<SimpleAnnotationField>>>) :
ComplexAnnotationField() {
fun toLangLiterals(value: String): List<Literal> {
return fields.map { pairs ->
return pairs.second.map {
fun toLangLiterals(source: Map<String, String>): List<Literal> {
return fields.flatMap { pairs ->
pairs.second.mapNotNull {
when (it) {
is MappedAnnotationField -> it.toLangLiteral(value, pairs.first)
is MappedAnnotationField ->
source[it.field].let { value ->
if (value != null) {
it.toLangLiteral(value, pairs.first)
} else {
null
}
}
is ConstantField -> it.toLangLiteral(pairs.first)
}
}
......
......@@ -19,23 +19,12 @@
package org.memobase.mapping.mappers
import org.memobase.builder.IResource
import org.memobase.mapping.fields.ConstantField
import org.memobase.mapping.fields.LanguageField
import org.memobase.mapping.fields.MappedAnnotationField
class LanguageFieldMapper(private val languageField: LanguageField) : IFieldMapper {
override fun apply(source: Map<String, String>, subject: IResource) {
languageField.fields.forEach { pairs ->
pairs.second.forEach { field ->
when (field) {
is ConstantField -> subject.addLiteral(field.key, field.toLangLiteral(pairs.first))
is MappedAnnotationField -> source[field.field].let {
if (it != null) {
subject.addLiteral(field.key, field.toLangLiteral(it, pairs.first))
}
}
}
}
languageField.toLangLiterals(source).forEach {
subject.addLiteral(languageField.key, it)
}
}
}
\ No newline at end of file
......@@ -19,22 +19,13 @@
package org.memobase.mapping.mappers
import org.memobase.builder.IResource
import org.memobase.mapping.fields.ConstantField
import org.memobase.mapping.fields.ListField
import org.memobase.mapping.fields.MappedAnnotationField
class ListFieldMapper(private val listField: ListField) :
IFieldMapper {
override fun apply(source: Map<String, String>, subject: IResource) {
listField.fields.forEach { field ->
when (field) {
is ConstantField -> subject.addLiteral(field.key, field.toLiteral())
is MappedAnnotationField -> source[field.field].let {
if (it != null) {
subject.addLiteral(field.key, field.toLiteral(it))
}
}
}
listField.toLiterals(source).forEach {
subject.addLiteral(listField.key, it)
}
}
}
\ No newline at end of file
......@@ -37,38 +37,10 @@ class RuleFieldMapper(private val configField: ConfigField) : IFieldMapper {
}
is ConstantField ->
subject.addRule(configField.key, listOf(configField.toLiteral()))
is LanguageField -> {
val literals = configField.fields.map {
it.second.map innerMap@{ field ->
when (field) {
is ConstantField -> listOf(field.toLangLiteral(it.first))
is MappedAnnotationField -> source[field.key].let { sourceValue ->
if (sourceValue != null) {
listOf(field.toLangLiteral(sourceValue, it.first))
} else {
emptyList()
}
}
}
}.flatten()
}.flatten()
subject.addRule(configField.key, literals)
}
is ListField -> {
val literals = configField.fields.map { field ->
when(field) {
is ConstantField -> listOf(field.toLiteral())
is MappedAnnotationField -> source[field.key].let {
if (it != null) {
listOf(field.toLiteral(it))
} else {
emptyList()
}
}
}
}.flatten()
subject.addRule(configField.key, literals)
}
is LanguageField ->
subject.addRule(configField.key, configField.toLangLiterals(source))
is ListField ->
subject.addRule(configField.key, configField.toLiterals(source))
}
}
}
\ No newline at end of file
......@@ -56,25 +56,12 @@ abstract class TypeFieldMapper : IFieldMapper {
is ConstantField -> addConstantField(field)
is MappedAnnotationField -> addMappedField(field, source)
is LanguageField ->
for (item in field.fields) {
for (annotationField in item.second) {
when (annotationField) {
is ConstantField -> properties.add(Pair(annotationField.key, annotationField.toLangLiteral(item.first)))
is MappedAnnotationField ->
source[annotationField.field].let {
if (it != null) {
properties.add(Pair(field.key, annotationField.toLangLiteral(it, item.first)))
}
}
}
}
field.toLangLiterals(source).forEach {
properties.add(Pair(field.key, it))
}
is ListField ->
for (item in field.fields) {
when (item) {
is ConstantField -> addConstantField(item)
is MappedAnnotationField -> addMappedField(item, source)
}
field.toLiterals(source).forEach {
properties.add(Pair(field.key, it))
}
}
}
......
Supports Markdown
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