Changed the logic of Value::isList for serialization

This commit is contained in:
Alexander Nozik 2020-11-29 10:18:52 +03:00
parent 2315fb963b
commit 1b46d00a91
6 changed files with 24 additions and 14 deletions

View File

@ -4,6 +4,8 @@
### Added ### Added
### Changed ### Changed
- `ListValue` and `DoubleArrayValue` implement `Iterable`.
- Changed the logic of `Value::isList` to check for type instead of size
### Deprecated ### Deprecated
@ -12,6 +14,7 @@
### Fixed ### Fixed
### Security ### Security
## [0.2.0] ## [0.2.0]
### Added ### Added

View File

@ -2,7 +2,7 @@ plugins {
id("ru.mipt.npm.project") id("ru.mipt.npm.project")
} }
val dataforgeVersion by extra("0.2.0") val dataforgeVersion by extra("0.2.1-dev-1")
val bintrayRepo by extra("dataforge") val bintrayRepo by extra("dataforge")
val githubProject by extra("dataforge-core") val githubProject by extra("dataforge-core")

View File

@ -813,7 +813,7 @@ public final class hep/dataforge/names/NameTokenKt {
public static final fun withIndex (Lhep/dataforge/names/NameToken;Ljava/lang/String;)Lhep/dataforge/names/NameToken; public static final fun withIndex (Lhep/dataforge/names/NameToken;Ljava/lang/String;)Lhep/dataforge/names/NameToken;
} }
public final class hep/dataforge/values/DoubleArrayValue : hep/dataforge/values/Value { public final class hep/dataforge/values/DoubleArrayValue : hep/dataforge/values/Value, java/lang/Iterable, kotlin/jvm/internal/markers/KMappedMarker {
public fun <init> ([D)V public fun <init> ([D)V
public fun equals (Ljava/lang/Object;)Z public fun equals (Ljava/lang/Object;)Z
public fun getList ()Ljava/util/List; public fun getList ()Ljava/util/List;
@ -824,6 +824,7 @@ public final class hep/dataforge/values/DoubleArrayValue : hep/dataforge/values/
public synthetic fun getValue ()Ljava/lang/Object; public synthetic fun getValue ()Ljava/lang/Object;
public fun getValue ()[D public fun getValue ()[D
public fun hashCode ()I public fun hashCode ()I
public fun iterator ()Ljava/util/Iterator;
public fun toString ()Ljava/lang/String; public fun toString ()Ljava/lang/String;
} }
@ -869,7 +870,7 @@ public final class hep/dataforge/values/LazyParsedValue : hep/dataforge/values/V
public fun toString ()Ljava/lang/String; public fun toString ()Ljava/lang/String;
} }
public final class hep/dataforge/values/ListValue : hep/dataforge/values/Value { public final class hep/dataforge/values/ListValue : hep/dataforge/values/Value, java/lang/Iterable, kotlin/jvm/internal/markers/KMappedMarker {
public fun <init> (Ljava/util/List;)V public fun <init> (Ljava/util/List;)V
public fun equals (Ljava/lang/Object;)Z public fun equals (Ljava/lang/Object;)Z
public fun getList ()Ljava/util/List; public fun getList ()Ljava/util/List;
@ -879,6 +880,7 @@ public final class hep/dataforge/values/ListValue : hep/dataforge/values/Value {
public synthetic fun getValue ()Ljava/lang/Object; public synthetic fun getValue ()Ljava/lang/Object;
public fun getValue ()Ljava/util/List; public fun getValue ()Ljava/util/List;
public fun hashCode ()I public fun hashCode ()I
public fun iterator ()Ljava/util/Iterator;
public fun toString ()Ljava/lang/String; public fun toString ()Ljava/lang/String;
} }

View File

@ -105,7 +105,7 @@ public object Null : Value {
* Singleton true value * Singleton true value
*/ */
public object True : Value { public object True : Value {
override val value: Any? get() = true override val value: Any get() = true
override val type: ValueType get() = ValueType.BOOLEAN override val type: ValueType get() = ValueType.BOOLEAN
override val number: Number get() = 1.0 override val number: Number get() = 1.0
override val string: String get() = "true" override val string: String get() = "true"
@ -120,7 +120,7 @@ public object True : Value {
* Singleton false value * Singleton false value
*/ */
public object False : Value { public object False : Value {
override val value: Any? get() = false override val value: Any get() = false
override val type: ValueType get() = ValueType.BOOLEAN override val type: ValueType get() = ValueType.BOOLEAN
override val number: Number get() = -1.0 override val number: Number get() = -1.0
override val string: String get() = "false" override val string: String get() = "false"
@ -132,7 +132,7 @@ public object False : Value {
} }
public class NumberValue(override val number: Number) : Value { public class NumberValue(override val number: Number) : Value {
override val value: Any? get() = number override val value: Any get() = number
override val type: ValueType get() = ValueType.NUMBER override val type: ValueType get() = ValueType.NUMBER
override val string: String get() = number.toString() override val string: String get() = number.toString()
@ -155,7 +155,7 @@ public class NumberValue(override val number: Number) : Value {
} }
public class StringValue(override val string: String) : Value { public class StringValue(override val string: String) : Value {
override val value: Any? get() = string override val value: Any get() = string
override val type: ValueType get() = ValueType.STRING override val type: ValueType get() = ValueType.STRING
override val number: Number get() = string.toDouble() override val number: Number get() = string.toDouble()
@ -182,7 +182,7 @@ public class EnumValue<E : Enum<*>>(override val value: E) : Value {
override fun toString(): String = value.toString() override fun toString(): String = value.toString()
} }
public class ListValue(override val list: List<Value>) : Value { public class ListValue(override val list: List<Value>) : Value, Iterable<Value> {
init { init {
require(list.isNotEmpty()) { "Can't create list value from empty list" } require(list.isNotEmpty()) { "Can't create list value from empty list" }
} }
@ -194,6 +194,8 @@ public class ListValue(override val list: List<Value>) : Value {
override fun toString(): String = list.joinToString(prefix = "[", postfix = "]") override fun toString(): String = list.joinToString(prefix = "[", postfix = "]")
override fun iterator(): Iterator<Value> = list.iterator()
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (other !is Value) return false if (other !is Value) return false

View File

@ -23,7 +23,7 @@ public fun String.lazyParseValue(): LazyParsedValue = LazyParsedValue(this)
/** /**
* A performance optimized version of list value for doubles * A performance optimized version of list value for doubles
*/ */
public class DoubleArrayValue(override val value: DoubleArray) : Value { public class DoubleArrayValue(override val value: DoubleArray) : Value, Iterable<Double> {
override val type: ValueType get() = ValueType.NUMBER override val type: ValueType get() = ValueType.NUMBER
override val number: Double get() = value.first() override val number: Double get() = value.first()
override val string: String get() = value.first().toString() override val string: String get() = value.first().toString()
@ -44,6 +44,8 @@ public class DoubleArrayValue(override val value: DoubleArray) : Value {
} }
override fun toString(): String = list.joinToString(prefix = "[", postfix = "]") override fun toString(): String = list.joinToString(prefix = "[", postfix = "]")
override fun iterator(): Iterator<Double> = value.iterator()
} }
public fun DoubleArray.asValue(): Value = if (isEmpty()) Null else DoubleArrayValue(this) public fun DoubleArray.asValue(): Value = if (isEmpty()) Null else DoubleArrayValue(this)

View File

@ -9,9 +9,10 @@ import hep.dataforge.meta.MetaBuilder
public fun Value.isNull(): Boolean = this == Null public fun Value.isNull(): Boolean = this == Null
/** /**
* Check if value is list * Check if value is list. This method checks the type of the value, not the number of the elements.
* So it will return `true` for empty lists and lists of one elements.
*/ */
public fun Value.isList(): Boolean = this.list.size > 1 public fun Value.isList(): Boolean = this is Iterable<*>
public val Value.boolean: Boolean public val Value.boolean: Boolean
get() = this == True get() = this == True