Skip to content

Split number property into longOrNull and doubleOrNull #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion json-schema-validator/api/json-schema-validator.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@
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
Expand Down Expand Up @@ -100,8 +102,10 @@
get() = false
override val isString: Boolean
get() = true
override val number: Number?
get() = value.toLongOrNull() ?: value.toDoubleOrNull()
override val longOrNull: Long?
get() = value.toLongOrNull()

Check warning on line 106 in json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/wrapper/JsonWrappers.kt

View check run for this annotation

Codecov / codecov/patch

json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/wrapper/JsonWrappers.kt#L106

Added line #L106 was not covered by tests
override val doubleOrNull: Double?
get() = value.toDoubleOrNull()

Check warning on line 108 in json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/wrapper/JsonWrappers.kt

View check run for this annotation

Codecov / codecov/patch

json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/wrapper/JsonWrappers.kt#L108

Added line #L108 was not covered by tests
override val isBoolean: Boolean
get() = false
override val isNumber: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading