From 2634a192858b7048a038bdb77661f0ae1b5ce8b2 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 12 Nov 2023 14:26:28 +0300 Subject: [PATCH] deprecate String.parseValue --- .space.kts | 4 -- CHANGELOG.md | 4 +- .../dataforge/io/yaml/YamlMetaFormat.kt | 8 +-- .../space/kscience/dataforge/io/IOFormat.kt | 19 ++++- .../space/kscience/dataforge/meta/JsonMeta.kt | 2 +- .../space/kscience/dataforge/meta/Value.kt | 71 ++++++++++--------- .../kscience/dataforge/meta/exoticValues.kt | 2 +- gradle.properties | 2 +- settings.gradle.kts | 2 +- 9 files changed, 66 insertions(+), 48 deletions(-) delete mode 100644 .space.kts diff --git a/.space.kts b/.space.kts deleted file mode 100644 index 45b2ed53..00000000 --- a/.space.kts +++ /dev/null @@ -1,4 +0,0 @@ -job("Build") { - gradlew("openjdk:11", "build") -} - diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a451615..4bcc9cef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,11 +9,11 @@ - Meta converter `metaToObject` returns a non-nullable type. Additional method `metaToObjectOrNull` for nullable return. - Kotlin 1.9.20. - Migrated from ktor-io to kotlinx-io. -- `MutableMeta` builder now returns a simplified version of meta that does not hold listeners. -- Ktor-io is replaced with kotlinx-io. +- `MutableMeta` builder now returns a simplified version of meta that does not hold listeners. - More concise names for read/write methods in IO. ### Deprecated +- `String.parseValue` is replaced with `Value.parse` ### Removed diff --git a/dataforge-io/dataforge-io-yaml/src/commonMain/kotlin/space/kscience/dataforge/io/yaml/YamlMetaFormat.kt b/dataforge-io/dataforge-io-yaml/src/commonMain/kotlin/space/kscience/dataforge/io/yaml/YamlMetaFormat.kt index 589b7e66..7c4c1246 100644 --- a/dataforge-io/dataforge-io-yaml/src/commonMain/kotlin/space/kscience/dataforge/io/yaml/YamlMetaFormat.kt +++ b/dataforge-io/dataforge-io-yaml/src/commonMain/kotlin/space/kscience/dataforge/io/yaml/YamlMetaFormat.kt @@ -32,7 +32,7 @@ public fun Meta.toYaml(): YamlMap { private class YamlMeta(private val yamlMap: YamlMap, private val descriptor: MetaDescriptor? = null) : Meta { override val value: Value? - get() = yamlMap.getStringOrNull(null)?.parseValue() + get() = yamlMap.getStringOrNull(null)?.let { Value.parse(it) } private fun buildItems(): Map { val map = LinkedHashMap() @@ -43,13 +43,13 @@ private class YamlMeta(private val yamlMap: YamlMap, private val descriptor: Met val token = NameToken(stringKey) when (value) { YamlNull -> Meta(Null) - is YamlLiteral -> map[token] = Meta(value.content.parseValue()) + is YamlLiteral -> map[token] = Meta(Value.parse(value.content)) is YamlMap -> map[token] = value.toMeta() is YamlList -> if (value.all { it is YamlLiteral }) { val listValue = ListValue( value.map { //We already checked that all values are primitives - (it as YamlLiteral).content.parseValue() + Value.parse((it as YamlLiteral).content) } ) map[token] = Meta(listValue) @@ -75,7 +75,7 @@ private class YamlMeta(private val yamlMap: YamlMap, private val descriptor: Met public fun YamlElement.toMeta(descriptor: MetaDescriptor? = null): Meta = when (this) { YamlNull -> Meta(Null) - is YamlLiteral -> Meta(content.parseValue()) + is YamlLiteral -> Meta(Value.parse(content)) is YamlMap -> toMeta() //We can't return multiple items therefore we create top level node is YamlList -> YamlMap(mapOf("@yamlArray" to this)).toMeta(descriptor) diff --git a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt index e51aa40a..36d28d17 100644 --- a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt @@ -88,10 +88,25 @@ public interface IOFormatFactory : Factory>, Named { public fun Binary(obj: T, format: IOWriter): Binary = Binary { format.writeTo(this, obj) } +public object FloatIOFormat : IOFormat, IOFormatFactory { + override fun build(context: Context, meta: Meta): IOFormat = this + + override val name: Name = "float32".asName() + + override val type: KType get() = typeOf() + + override fun writeTo(sink: Sink, obj: Float) { + sink.writeFloat(obj) + } + + override fun readFrom(source: Source): Float = source.readFloat() +} + + public object DoubleIOFormat : IOFormat, IOFormatFactory { override fun build(context: Context, meta: Meta): IOFormat = this - override val name: Name = "double".asName() + override val name: Name = "float64".asName() override val type: KType get() = typeOf() @@ -99,5 +114,5 @@ public object DoubleIOFormat : IOFormat, IOFormatFactory { sink.writeLong(obj.toBits()) } - override fun readFrom(source: Source): Double = Double.fromBits(source.readLong()) + override fun readFrom(source: Source): Double = source.readDouble() } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt index 7dc44785..36373582 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt @@ -81,7 +81,7 @@ public fun JsonPrimitive.toValue(descriptor: MetaDescriptor?): Value = when (thi content.asValue() } else { //consider using LazyParse - content.parseValue() + Value.parse(content) } } } diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Value.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Value.kt index 73950f53..66e14c86 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Value.kt @@ -64,6 +64,7 @@ public interface Value { ListValue(list) } } + is DoubleArray -> value.asValue() is IntArray -> value.asValue() is FloatArray -> value.asValue() @@ -76,6 +77,41 @@ public interface Value { else -> throw IllegalArgumentException("Unrecognized type of the object (${value::class}) converted to Value") } } + + /** + * Parse value from string. Double-quoted strings are parsed literally. true/false are parsed as booleans + */ + public fun parse(string: String): Value { + + //Trying to get integer + if (string.isEmpty() || string == Null.string) { + return Null + } + + //string constants + if (string.startsWith("\"") && string.endsWith("\"")) { + return StringValue(string.substring(1, string.length - 2)) + } + + string.toIntOrNull()?.let { + return NumberValue(it) + } + + string.toDoubleOrNull()?.let { + return NumberValue(it) + } + + if ("true" == string) { + return True + } + + if ("false" == string) { + return False + } + + //Give up and return a StringValue + return StringValue(string) + } } } @@ -140,7 +176,7 @@ public class NumberValue(public val number: Number) : Value { val otherNumber = other.numberOrNull ?: return false - if(number == otherNumber) return true + if (number == otherNumber) return true //Do not change the order of comparison. On JS number is the instance of all types return when (numberOrNull) { @@ -228,34 +264,5 @@ public fun > E.asValue(): Value = EnumValue(this) /** * Create Value from String using the closest match conversion */ -public fun String.parseValue(): Value { - - //Trying to get integer - if (isEmpty() || this == Null.string) { - return Null - } - - //string constants - if (startsWith("\"") && endsWith("\"")) { - return StringValue(substring(1, length - 2)) - } - - toIntOrNull()?.let { - return NumberValue(it) - } - - toDoubleOrNull()?.let { - return NumberValue(it) - } - - if ("true" == this) { - return True - } - - if ("false" == this) { - return False - } - - //Give up and return a StringValue - return StringValue(this) -} \ No newline at end of file +@Deprecated("Use Value.parse(this) instead", ReplaceWith("Value.parse(this)")) +public fun String.parseValue(): Value = Value.parse(this) \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/exoticValues.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/exoticValues.kt index 072cffdb..74952053 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/exoticValues.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/exoticValues.kt @@ -5,7 +5,7 @@ package space.kscience.dataforge.meta * A value built from string which content and type are parsed on-demand */ public class LazyParsedValue(public val string: String) : Value { - private val parsedValue by lazy { string.parseValue() } + private val parsedValue by lazy { Value.parse(string) } override val value: Any? get() = parsedValue.value override val type: ValueType get() = parsedValue.type diff --git a/gradle.properties b/gradle.properties index c3c5b2a3..97bb841d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,5 +6,5 @@ kotlin.mpp.stability.nowarn=true kotlin.incremental.js.ir=true kotlin.native.ignoreDisabledTargets=true -toolsVersion=0.15.0-kotlin-1.9.20-RC2 +toolsVersion=0.15.0-kotlin-1.9.20 #kotlin.experimental.tryK2=true \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index e9fb1b81..ca872038 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,7 +1,6 @@ rootProject.name = "dataforge-core" enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") -//enableFeaturePreview("VERSION_CATALOGS") pluginManagement { @@ -15,6 +14,7 @@ pluginManagement { } plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0" id("space.kscience.gradle.project") version toolsVersion id("space.kscience.gradle.mpp") version toolsVersion id("space.kscience.gradle.jvm") version toolsVersion