Skip to content

Commit

Permalink
Split number property into longOrNull and doubleOrNull
Browse files Browse the repository at this point in the history
  • Loading branch information
OptimumCode committed Sep 5, 2024
1 parent 2f973dd commit d67a084
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 9 deletions.
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 @@ 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
Expand Down Expand Up @@ -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()

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

0 comments on commit d67a084

Please sign in to comment.