-
-
Notifications
You must be signed in to change notification settings - Fork 755
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
Markus Schwarz
committed
Jul 3, 2021
1 parent
5048b8b
commit da46d4a
Showing
13 changed files
with
179 additions
and
129 deletions.
There are no files selected for viewing
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
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
38 changes: 38 additions & 0 deletions
38
...enerator/src/main/kotlin/io/gitlab/arturbosch/detekt/generator/collection/DefaultValue.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,38 @@ | ||
package io.gitlab.arturbosch.detekt.generator.collection | ||
|
||
sealed interface DefaultValue { | ||
fun isNonEmptyList(): Boolean = false | ||
fun getAsList(): List<String> = error("default value is not a list") | ||
fun getAsPlainString(): String | ||
fun getQuotedIfNecessary(): String = getAsPlainString() | ||
|
||
companion object { | ||
fun of(defaultValue: String): DefaultValue = StringDefault(defaultValue) | ||
fun of(defaultValue: Boolean): DefaultValue = BooleanDefault(defaultValue) | ||
fun of(defaultValue: Int): DefaultValue = IntegerDefault(defaultValue) | ||
fun of(defaultValue: List<String>): DefaultValue = ListDefault(defaultValue) | ||
} | ||
} | ||
|
||
private data class StringDefault(private val defaultValue: String) : DefaultValue { | ||
private val quoted = "'$defaultValue'" | ||
override fun getAsPlainString(): String = defaultValue | ||
override fun getQuotedIfNecessary(): String = quoted | ||
} | ||
|
||
private data class BooleanDefault(private val defaultValue: Boolean) : DefaultValue { | ||
override fun getAsPlainString(): String = defaultValue.toString() | ||
} | ||
|
||
private data class IntegerDefault(private val defaultValue: Int) : DefaultValue { | ||
override fun getAsPlainString(): String = defaultValue.toString() | ||
} | ||
|
||
private data class ListDefault(private val defaultValue: List<String>) : DefaultValue { | ||
private val quoted: String = defaultValue.map { "'$it'" }.toString() | ||
|
||
override fun isNonEmptyList(): Boolean = defaultValue.isNotEmpty() | ||
override fun getAsList(): List<String> = defaultValue.ifEmpty { error("default value is an empty list") } | ||
override fun getAsPlainString(): String = defaultValue.toString() | ||
override fun getQuotedIfNecessary(): String = quoted | ||
} |
22 changes: 22 additions & 0 deletions
22
...nerator/src/main/kotlin/io/gitlab/arturbosch/detekt/generator/collection/DefaultValues.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,22 @@ | ||
package io.gitlab.arturbosch.detekt.generator.collection | ||
|
||
import org.jetbrains.kotlin.lexer.KtTokens.FALSE_KEYWORD | ||
import org.jetbrains.kotlin.lexer.KtTokens.TRUE_KEYWORD | ||
|
||
fun createDefaultValueIfLiteral(maybeLiteral: String): DefaultValue? = maybeLiteral.toDefaultValueIfLiteral() | ||
|
||
private fun String.toDefaultValueIfLiteral(): DefaultValue? { | ||
return when { | ||
isStringLiteral() -> DefaultValue.of(withoutQuotes()) | ||
isBooleanLiteral() -> DefaultValue.of(toBoolean()) | ||
isIntegerLiteral() -> DefaultValue.of(withoutUnderscores().toInt()) | ||
else -> null | ||
} | ||
} | ||
|
||
private fun String.withoutUnderscores() = replace("_", "") | ||
private fun String.isStringLiteral() = length > 1 && startsWith(QUOTES) && endsWith(QUOTES) | ||
private fun String.isBooleanLiteral() = this == TRUE_KEYWORD.value || this == FALSE_KEYWORD.value | ||
private fun String.isIntegerLiteral() = withoutUnderscores().toIntOrNull() != null | ||
|
||
private const val QUOTES = "\"" |
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
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
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
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
Oops, something went wrong.