-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...-processor-properties/src/main/java/cafe/adriel/lyricist/processor/properties/Template.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
47 changes: 47 additions & 0 deletions
47
...or-properties/src/main/java/cafe/adriel/lyricist/processor/properties/TemplateResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\" }" | ||
} |