Skip to content

Commit a66cf3b

Browse files
committed
More readable path formatter initialization
1 parent a5b1081 commit a66cf3b

File tree

3 files changed

+29
-32
lines changed

3 files changed

+29
-32
lines changed

src/main/java/org/radarbase/output/path/FormattedPathFactory.kt

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,16 @@ import java.time.Instant
2424
import kotlin.reflect.jvm.jvmName
2525

2626
open class FormattedPathFactory : RecordPathFactory() {
27-
private lateinit var format: String
28-
private lateinit var plugins: List<PathFormatterPlugin>
2927
private lateinit var formatter: PathFormatter
3028
private lateinit var properties: Map<String, String>
3129
private var topicFormatters: Map<String, PathFormatter> = emptyMap()
3230

3331
override fun init(properties: Map<String, String>) {
3432
super.init(properties)
3533

36-
format = properties["format"] ?: DEFAULT_FORMAT
37-
38-
plugins = instantiatePlugins(properties["plugins"] ?: DEFAULT_FORMAT_PLUGINS, properties)
39-
40-
logger.info("Path formatter uses format '{}' with plugins '{}'", format, plugins.map { it.name })
41-
42-
formatter = PathFormatter(format, plugins)
34+
this.properties = DEFAULTS + properties
35+
formatter = createFormatter(this.properties)
36+
logger.info("Formatting path with {}", formatter)
4337
}
4438

4539
private fun instantiatePlugins(
@@ -54,23 +48,20 @@ open class FormattedPathFactory : RecordPathFactory() {
5448
override fun addTopicConfiguration(topicConfig: Map<String, TopicConfig>) {
5549
topicFormatters = topicConfig
5650
.filter { (_, config) -> config.pathProperties.isNotEmpty() }
57-
.mapValues { (topic, config) ->
58-
val topicFormat = config.pathProperties.getOrDefault("format", format)
59-
val pluginClassNames = config.pathProperties["plugins"]
60-
61-
val topicPlugins = if (pluginClassNames != null) {
62-
instantiatePlugins(pluginClassNames, properties + config.pathProperties)
63-
} else plugins
51+
.mapValues { (_, config) ->
52+
createFormatter(properties + config.pathProperties)
53+
}
54+
.onEach { (topic, formatter) ->
55+
logger.info("Formatting path of topic {} with {}", topic, formatter)
56+
}
57+
}
6458

65-
logger.info(
66-
"Path formatter of topic {} uses format {} with plugins {}",
67-
topic,
68-
topicFormat,
69-
topicPlugins.map { it.name }
70-
)
59+
private fun createFormatter(properties: Map<String, String>): PathFormatter {
60+
val format = checkNotNull(properties["format"])
61+
val pluginClassNames = checkNotNull(properties["plugins"])
62+
val plugins = instantiatePlugins(pluginClassNames, properties)
7163

72-
PathFormatter(topicFormat, topicPlugins)
73-
}
64+
return PathFormatter(format, plugins)
7465
}
7566

7667
override fun getRelativePath(
@@ -88,8 +79,10 @@ open class FormattedPathFactory : RecordPathFactory() {
8879
): String = sanitizeId(key.get("sourceId"), "unknown-source")
8980

9081
companion object {
91-
internal const val DEFAULT_FORMAT = "\${projectId}/\${userId}/\${topic}/\${filename}"
92-
internal const val DEFAULT_FORMAT_PLUGINS = "fixed time key value"
82+
internal val DEFAULTS = mapOf(
83+
"format" to "\${projectId}/\${userId}/\${topic}/\${filename}",
84+
"plugins" to "fixed time key value",
85+
)
9386
private val logger = LoggerFactory.getLogger(FormattedPathFactory::class.java)
9487

9588
internal fun String.toPathFormatterPlugin(): PathFormatterPlugin? = when (this) {

src/main/java/org/radarbase/output/path/PathFormatter.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import java.nio.file.Paths
2222

2323
class PathFormatter(
2424
private val format: String,
25-
plugins: List<PathFormatterPlugin>
25+
private val plugins: List<PathFormatterPlugin>
2626
) {
2727
private val parameterLookups: Map<String, PathFormatParameters.() -> String>
2828

@@ -37,7 +37,7 @@ class PathFormatter(
3737
try {
3838
plugin.createLookupTable(foundParameters)
3939
} catch (ex: IllegalArgumentException) {
40-
logger.error("Cannot parse path format {}, illegal format parameter found by plugin {}", format, plugin.javaClass, ex)
40+
logger.error("Cannot parse path format {}, illegal format parameter found by plugin {}", format, plugin.name, ex)
4141
throw ex
4242
}
4343
)
@@ -69,6 +69,10 @@ class PathFormatter(
6969
return Paths.get(path)
7070
}
7171

72+
override fun toString(): String = "PathFormatter{" +
73+
"format=$format," +
74+
"plugins=${plugins.map { it.name }}}"
75+
7276
companion object {
7377
private val logger = LoggerFactory.getLogger(PathFormatter::class.java)
7478
}

src/test/java/org/radarbase/output/path/PathFormatterTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal class PathFormatterTest {
4343
@Test
4444
fun testDefaultPath() {
4545
val formatter = PathFormatter(
46-
format = FormattedPathFactory.Companion.DEFAULT_FORMAT,
46+
format = checkNotNull(FormattedPathFactory.Companion.DEFAULTS["format"]),
4747
plugins = listOf(
4848
FixedPathFormatterPlugin(),
4949
TimePathFormatterPlugin(),
@@ -57,7 +57,7 @@ internal class PathFormatterTest {
5757
@Test
5858
fun testDefaultPathFewerPlugins() {
5959
val formatter = PathFormatter(
60-
format = FormattedPathFactory.Companion.DEFAULT_FORMAT,
60+
format = checkNotNull(FormattedPathFactory.Companion.DEFAULTS["format"]),
6161
plugins = listOf(
6262
FixedPathFormatterPlugin(),
6363
)
@@ -68,7 +68,7 @@ internal class PathFormatterTest {
6868
@Test
6969
fun testDefaultPathNoTime() {
7070
val formatter = PathFormatter(
71-
format = FormattedPathFactory.Companion.DEFAULT_FORMAT,
71+
format = checkNotNull(FormattedPathFactory.Companion.DEFAULTS["format"]),
7272
plugins = listOf(
7373
FixedPathFormatterPlugin(),
7474
)
@@ -80,7 +80,7 @@ internal class PathFormatterTest {
8080
fun testDefaultPathWrongPlugins() {
8181
assertThrows(IllegalArgumentException::class.java) {
8282
PathFormatter(
83-
format = FormattedPathFactory.Companion.DEFAULT_FORMAT,
83+
format = checkNotNull(FormattedPathFactory.Companion.DEFAULTS["format"]),
8484
plugins = listOf(
8585
TimePathFormatterPlugin(),
8686
KeyPathFormatterPlugin(),

0 commit comments

Comments
 (0)