From d67a084df70f4e0996f96cde2e1a02a52d0264c0 Mon Sep 17 00:00:00 2001 From: Oleg Smirnov Date: Thu, 5 Sep 2024 11:01:56 +0400 Subject: [PATCH] Split number property into longOrNull and doubleOrNull --- json-schema-validator/api/json-schema-validator.api | 3 ++- .../number/Draft4MaximumAssertionFactory.kt | 1 + .../number/Draft4MinimumAssertionFactory.kt | 1 + .../number/ExclusiveMaximumAssertionFactory.kt | 1 + .../number/ExclusiveMinimumAssertionFactory.kt | 1 + .../factories/number/MaximumAssertionFactory.kt | 1 + .../factories/number/MinimumAssertionFactory.kt | 1 + .../factories/number/MultipleOfAssertionFactory.kt | 1 + .../internal/factories/number/util/NumberUtil.kt | 1 + .../json/schema/internal/util/NumberParts.kt | 3 +++ .../json/schema/internal/wrapper/JsonWrappers.kt | 12 ++++++++---- .../github/optimumcode/json/schema/model/Model.kt | 13 +++++++++---- 12 files changed, 30 insertions(+), 9 deletions(-) diff --git a/json-schema-validator/api/json-schema-validator.api b/json-schema-validator/api/json-schema-validator.api index 842daa5..c931d08 100644 --- a/json-schema-validator/api/json-schema-validator.api +++ b/json-schema-validator/api/json-schema-validator.api @@ -380,7 +380,8 @@ public abstract interface class io/github/optimumcode/json/schema/model/ObjectEl public abstract interface class io/github/optimumcode/json/schema/model/PrimitiveElement : io/github/optimumcode/json/schema/model/AbstractElement { public abstract fun getContent ()Ljava/lang/String; - public abstract fun getNumber ()Ljava/lang/Number; + public abstract fun getDoubleOrNull ()Ljava/lang/Double; + public abstract fun getLongOrNull ()Ljava/lang/Long; public abstract fun isBoolean ()Z public abstract fun isNull ()Z public abstract fun isNumber ()Z diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/Draft4MaximumAssertionFactory.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/Draft4MaximumAssertionFactory.kt index ea76937..db830d5 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/Draft4MaximumAssertionFactory.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/Draft4MaximumAssertionFactory.kt @@ -5,6 +5,7 @@ import io.github.optimumcode.json.schema.internal.JsonSchemaAssertion import io.github.optimumcode.json.schema.internal.LoadingContext import io.github.optimumcode.json.schema.internal.factories.number.util.NumberComparisonAssertion import io.github.optimumcode.json.schema.internal.factories.number.util.compareTo +import io.github.optimumcode.json.schema.internal.util.number import io.github.optimumcode.json.schema.internal.wrapper.JsonPrimitiveWrapper import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonObject diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/Draft4MinimumAssertionFactory.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/Draft4MinimumAssertionFactory.kt index d6a63e5..bf9e642 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/Draft4MinimumAssertionFactory.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/Draft4MinimumAssertionFactory.kt @@ -5,6 +5,7 @@ import io.github.optimumcode.json.schema.internal.JsonSchemaAssertion import io.github.optimumcode.json.schema.internal.LoadingContext import io.github.optimumcode.json.schema.internal.factories.number.util.NumberComparisonAssertion import io.github.optimumcode.json.schema.internal.factories.number.util.compareTo +import io.github.optimumcode.json.schema.internal.util.number import io.github.optimumcode.json.schema.internal.wrapper.JsonPrimitiveWrapper import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonObject diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/ExclusiveMaximumAssertionFactory.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/ExclusiveMaximumAssertionFactory.kt index 86685b3..2f21e00 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/ExclusiveMaximumAssertionFactory.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/ExclusiveMaximumAssertionFactory.kt @@ -5,6 +5,7 @@ import io.github.optimumcode.json.schema.internal.LoadingContext import io.github.optimumcode.json.schema.internal.factories.AbstractAssertionFactory import io.github.optimumcode.json.schema.internal.factories.number.util.NumberComparisonAssertion import io.github.optimumcode.json.schema.internal.factories.number.util.compareTo +import io.github.optimumcode.json.schema.internal.util.number import io.github.optimumcode.json.schema.internal.wrapper.JsonPrimitiveWrapper import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/ExclusiveMinimumAssertionFactory.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/ExclusiveMinimumAssertionFactory.kt index 7b1db2c..1450601 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/ExclusiveMinimumAssertionFactory.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/ExclusiveMinimumAssertionFactory.kt @@ -5,6 +5,7 @@ import io.github.optimumcode.json.schema.internal.LoadingContext import io.github.optimumcode.json.schema.internal.factories.AbstractAssertionFactory import io.github.optimumcode.json.schema.internal.factories.number.util.NumberComparisonAssertion import io.github.optimumcode.json.schema.internal.factories.number.util.compareTo +import io.github.optimumcode.json.schema.internal.util.number import io.github.optimumcode.json.schema.internal.wrapper.JsonPrimitiveWrapper import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MaximumAssertionFactory.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MaximumAssertionFactory.kt index b812116..ec617dd 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MaximumAssertionFactory.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MaximumAssertionFactory.kt @@ -5,6 +5,7 @@ import io.github.optimumcode.json.schema.internal.LoadingContext import io.github.optimumcode.json.schema.internal.factories.AbstractAssertionFactory import io.github.optimumcode.json.schema.internal.factories.number.util.NumberComparisonAssertion import io.github.optimumcode.json.schema.internal.factories.number.util.compareTo +import io.github.optimumcode.json.schema.internal.util.number import io.github.optimumcode.json.schema.internal.wrapper.JsonPrimitiveWrapper import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MinimumAssertionFactory.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MinimumAssertionFactory.kt index db0245a..cd8faa4 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MinimumAssertionFactory.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MinimumAssertionFactory.kt @@ -5,6 +5,7 @@ import io.github.optimumcode.json.schema.internal.LoadingContext import io.github.optimumcode.json.schema.internal.factories.AbstractAssertionFactory import io.github.optimumcode.json.schema.internal.factories.number.util.NumberComparisonAssertion import io.github.optimumcode.json.schema.internal.factories.number.util.compareTo +import io.github.optimumcode.json.schema.internal.util.number import io.github.optimumcode.json.schema.internal.wrapper.JsonPrimitiveWrapper import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MultipleOfAssertionFactory.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MultipleOfAssertionFactory.kt index 2c69054..20c0473 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MultipleOfAssertionFactory.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/MultipleOfAssertionFactory.kt @@ -4,6 +4,7 @@ import io.github.optimumcode.json.schema.internal.JsonSchemaAssertion import io.github.optimumcode.json.schema.internal.LoadingContext import io.github.optimumcode.json.schema.internal.factories.AbstractAssertionFactory import io.github.optimumcode.json.schema.internal.factories.number.util.NumberComparisonAssertion +import io.github.optimumcode.json.schema.internal.util.number import io.github.optimumcode.json.schema.internal.wrapper.JsonPrimitiveWrapper import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/util/NumberUtil.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/util/NumberUtil.kt index 1378965..f474906 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/util/NumberUtil.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/number/util/NumberUtil.kt @@ -5,6 +5,7 @@ import io.github.optimumcode.json.schema.OutputCollector import io.github.optimumcode.json.schema.ValidationError import io.github.optimumcode.json.schema.internal.AssertionContext import io.github.optimumcode.json.schema.internal.JsonSchemaAssertion +import io.github.optimumcode.json.schema.internal.util.number import io.github.optimumcode.json.schema.model.AbstractElement import io.github.optimumcode.json.schema.model.PrimitiveElement diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/util/NumberParts.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/util/NumberParts.kt index 27eff3c..7481b1a 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/util/NumberParts.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/util/NumberParts.kt @@ -21,6 +21,9 @@ private const val E_SMALL_CHAR: Char = 'e' private const val E_BIG_CHAR: Char = 'E' private const val TEN: Double = 10.0 +internal val PrimitiveElement.number: Number? + get() = longOrNull ?: doubleOrNull + /** * This function should be used only if you are certain that the [element] is a number */ diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/wrapper/JsonWrappers.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/wrapper/JsonWrappers.kt index 0e89022..ba5cb0b 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/wrapper/JsonWrappers.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/wrapper/JsonWrappers.kt @@ -68,8 +68,10 @@ internal value class JsonPrimitiveWrapper( get() = primitive is JsonNull override val isString: Boolean get() = primitive.isString - override val number: Number? - get() = primitive.run { longOrNull ?: doubleOrNull } + override val longOrNull: Long? + get() = primitive.longOrNull + override val doubleOrNull: Double? + get() = primitive.doubleOrNull override val isBoolean: Boolean get() = primitive.run { !isString && booleanOrNull != null } override val isNumber: Boolean @@ -100,8 +102,10 @@ internal value class StringWrapper( get() = false override val isString: Boolean get() = true - override val number: Number? - get() = value.toLongOrNull() ?: value.toDoubleOrNull() + override val longOrNull: Long? + get() = value.toLongOrNull() + override val doubleOrNull: Double? + get() = value.toDoubleOrNull() override val isBoolean: Boolean get() = false override val isNumber: Boolean diff --git a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/model/Model.kt b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/model/Model.kt index f789e23..f97478d 100644 --- a/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/model/Model.kt +++ b/json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/model/Model.kt @@ -43,11 +43,16 @@ public interface PrimitiveElement : AbstractElement { public val isNumber: Boolean /** - * Tries to parse the element as a [Number]. - * Must return either [Long] or [Double]. - * If the element cannot be interpreted as a number, returns `null`. + * Tries to represent the element as a [Long]. + * If the element cannot be interpreted as a [Long], returns `null`. */ - public val number: Number? + public val longOrNull: Long? + + /** + * Tries to represent the element as a [Double]. + * If the element cannot be interpreted as a [Double], returns `null`. + */ + public val doubleOrNull: Double? /** * Returns the content of the element as plain string