Skip to content

Commit

Permalink
Create TemplateResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNatan committed Nov 5, 2023
1 parent 7d518c2 commit 2c9fd44
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cafe.adriel.lyricist.processor.properties

internal fun createTemplate(className: String, fields: String): String =
"""
|public data class $className(
| $fields
|)
""".trimMargin()
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cafe.adriel.lyricist.processor.properties

import java.util.Properties

public fun resolveProperties(properties: Properties): String = properties.map { (key, value) ->
val name = removeDots(key as String)
val (type, fieldValue) = resolveType(value as String)

"val $name: $type = $fieldValue"
}.joinToString(separator = ",\n")

private fun removeDots(input: String): String {
var replaced = input
while (true) {
val dot = replaced.indexOf(".")
if (dot == -1) break

val first = replaced[dot + 1]

replaced = replaced.replaceRange(dot..dot + 1, first.uppercase())
}
return replaced
}

private fun resolveType(value: String): Pair<String, String> = when {
value.toBooleanStrictOrNull() != null -> "Boolean" to value
value.toIntOrNull() != null -> "Int" to value
else -> resolveComplexExpressionType(value) ?: ("String" to "\"$value\"")
}

private val ValueWithArgumentsExpr = Regex("\\{(.*?)}")

private fun resolveComplexExpressionType(value: String): Pair<String, String>? {
val matches = ValueWithArgumentsExpr.findAll(value)
val textArgs = matches.map(MatchResult::value).toList()
if (textArgs.isEmpty())
return null

val names = textArgs.map { name -> name.substring(1 until name.lastIndex) }
val fnArgs = names.joinToString(separator = ", ") { name -> "$name: String" }
val returnArgs = names.joinToString(separator = ", ")
var replacedValue = value
for (match in matches)
replacedValue = replacedValue.replace(match.value, "$${match.value}")

return "($fnArgs) -> String" to "{ $returnArgs -> \"$replacedValue\" }"
}

0 comments on commit 2c9fd44

Please sign in to comment.