mirror of
https://github.com/HighCapable/SweetProperty.git
synced 2025-09-04 01:35:37 +08:00
feat: add use '' or "" to force a string value
This commit is contained in:
@@ -244,8 +244,11 @@ open class SweetPropertyConfigureExtension internal constructor() {
|
||||
/**
|
||||
* 是否启用类型自动转换功能
|
||||
*
|
||||
*
|
||||
* 默认启用 - 启用后将自动识别属性键值中的类型并转换为对应的类型
|
||||
*
|
||||
* 在启用后如果你想要强制设置一个键值内容为字符串类型 - 你可以使用单引号或双引号包裹整个字符串
|
||||
*
|
||||
* 注意:在关闭此功能后如上所述的功能也将同时失效
|
||||
*/
|
||||
var isEnableTypeAutoConversion: Boolean? = null
|
||||
@JvmName("enableTypeAutoConversion") set
|
||||
|
@@ -32,13 +32,20 @@ internal typealias PropertyMap = MutableMap<String, Any>
|
||||
* @return [Pair]<[KClass], [String]>
|
||||
*/
|
||||
internal fun Any.parseTypedValue(isAutoConversion: Boolean): Pair<KClass<*>, String> {
|
||||
var isStringType = false
|
||||
val valueString = toString()
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r")
|
||||
.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"")
|
||||
.let {
|
||||
if (isAutoConversion && (it.startsWith("\"") && it.endsWith("\"") || it.startsWith("'") && it.endsWith("'"))) {
|
||||
isStringType = true
|
||||
it.drop(1).dropLast(1)
|
||||
} else it.replace("\"", "\\\"")
|
||||
}
|
||||
if (isAutoConversion.not()) return Pair(String::class, "\"$valueString\"")
|
||||
val typeSpec = when {
|
||||
isStringType -> String::class
|
||||
valueString.trim().toIntOrNull() != null -> Int::class
|
||||
valueString.trim().toLongOrNull() != null -> Long::class
|
||||
valueString.trim().toDoubleOrNull() != null -> Double::class
|
||||
|
Reference in New Issue
Block a user