From eead3c88390eee74307f1177df6c641d37943cc7 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Mon, 4 Sep 2023 02:01:08 +0800 Subject: [PATCH] feat: add use '' or "" to force a string value --- .../dsl/configure/SweetPropertyConfigureExtension.kt | 5 ++++- .../plugin/generator/factory/GeneratorFactory.kt | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt index 828cc8d..b7f0914 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt @@ -244,8 +244,11 @@ open class SweetPropertyConfigureExtension internal constructor() { /** * 是否启用类型自动转换功能 * - * * 默认启用 - 启用后将自动识别属性键值中的类型并转换为对应的类型 + * + * 在启用后如果你想要强制设置一个键值内容为字符串类型 - 你可以使用单引号或双引号包裹整个字符串 + * + * 注意:在关闭此功能后如上所述的功能也将同时失效 */ var isEnableTypeAutoConversion: Boolean? = null @JvmName("enableTypeAutoConversion") set diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/generator/factory/GeneratorFactory.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/generator/factory/GeneratorFactory.kt index 95c9533..cfc0774 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/generator/factory/GeneratorFactory.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/generator/factory/GeneratorFactory.kt @@ -32,13 +32,20 @@ internal typealias PropertyMap = MutableMap * @return [Pair]<[KClass], [String]> */ internal fun Any.parseTypedValue(isAutoConversion: Boolean): Pair, 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