Skip to content

Commit

Permalink
Define default string and regex as constant
Browse files Browse the repository at this point in the history
  • Loading branch information
v0idness committed Aug 14, 2024
1 parent 6b36992 commit 23522da
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import org.vitrivr.engine.core.operators.general.TransformerFactory

private val logger = KotlinLogging.logger {}

private val TEMPLATE_REGEX = "\\$\\{([^}]+)\\}".toRegex()
private const val DEFAULT_VALUE = "No content available."

/**
* A [Transformer] that takes an input template with placeholders and inserts content from fields in their place.
*
Expand All @@ -25,9 +28,8 @@ private val logger = KotlinLogging.logger {}
class TemplateTextTransformer : TransformerFactory {
override fun newTransformer(name: String, input: Operator<out Retrievable>, context: Context): Transformer {
val template = context[name, "template"] ?: throw IllegalArgumentException("The template text transformer requires a template.")
val regex = "\\$\\{([^}]+)\\}".toRegex()
val contentFields = regex.findAll(template).map { it.groupValues[1] }.toList()
val defaultValue = context[name, "defaultValue"] ?: ""
val contentFields = TEMPLATE_REGEX.findAll(template).map { it.groupValues[1] }.toList()
val defaultValue = context[name, "defaultValue"] ?: DEFAULT_VALUE
return Instance(
input = input,
contentFactory = (context as IndexContext).contentFactory,
Expand Down

0 comments on commit 23522da

Please sign in to comment.