deprecate String.parseValue

This commit is contained in:
Alexander Nozik 2023-11-12 14:26:28 +03:00
parent 261c415d3d
commit 2634a19285
9 changed files with 66 additions and 48 deletions

View File

@ -1,4 +0,0 @@
job("Build") {
gradlew("openjdk:11", "build")
}

View File

@ -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

View File

@ -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<NameToken, Meta> {
val map = LinkedHashMap<NameToken, Meta>()
@ -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)

View File

@ -88,10 +88,25 @@ public interface IOFormatFactory<T : Any> : Factory<IOFormat<T>>, Named {
public fun <T : Any> Binary(obj: T, format: IOWriter<T>): Binary = Binary { format.writeTo(this, obj) }
public object FloatIOFormat : IOFormat<Float>, IOFormatFactory<Float> {
override fun build(context: Context, meta: Meta): IOFormat<Float> = this
override val name: Name = "float32".asName()
override val type: KType get() = typeOf<Float>()
override fun writeTo(sink: Sink, obj: Float) {
sink.writeFloat(obj)
}
override fun readFrom(source: Source): Float = source.readFloat()
}
public object DoubleIOFormat : IOFormat<Double>, IOFormatFactory<Double> {
override fun build(context: Context, meta: Meta): IOFormat<Double> = this
override val name: Name = "double".asName()
override val name: Name = "float64".asName()
override val type: KType get() = typeOf<Double>()
@ -99,5 +114,5 @@ public object DoubleIOFormat : IOFormat<Double>, IOFormatFactory<Double> {
sink.writeLong(obj.toBits())
}
override fun readFrom(source: Source): Double = Double.fromBits(source.readLong())
override fun readFrom(source: Source): Double = source.readDouble()
}

View File

@ -81,7 +81,7 @@ public fun JsonPrimitive.toValue(descriptor: MetaDescriptor?): Value = when (thi
content.asValue()
} else {
//consider using LazyParse
content.parseValue()
Value.parse(content)
}
}
}

View File

@ -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 : Enum<E>> 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)
}
@Deprecated("Use Value.parse(this) instead", ReplaceWith("Value.parse(this)"))
public fun String.parseValue(): Value = Value.parse(this)

View File

@ -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

View File

@ -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

View File

@ -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