Skip to content

Commit 539f36a

Browse files
committed
Add comments for model classes. Simplify array interface
1 parent 8390772 commit 539f36a

File tree

7 files changed

+69
-12
lines changed

7 files changed

+69
-12
lines changed

json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/array/AdditionalItemsAssertion.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.github.optimumcode.json.schema.internal.AssertionContext
77
import io.github.optimumcode.json.schema.internal.JsonSchemaAssertion
88
import io.github.optimumcode.json.schema.model.AbstractElement
99
import io.github.optimumcode.json.schema.model.ArrayElement
10+
import io.github.optimumcode.json.schema.model.lastIndex
1011

1112
internal class AdditionalItemsAssertion(
1213
private val location: JsonPointer,

json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/array/AllItemsAssertion.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.github.optimumcode.json.schema.internal.AssertionContext
77
import io.github.optimumcode.json.schema.internal.JsonSchemaAssertion
88
import io.github.optimumcode.json.schema.model.AbstractElement
99
import io.github.optimumcode.json.schema.model.ArrayElement
10+
import io.github.optimumcode.json.schema.model.lastIndex
1011

1112
internal class AllItemsAssertion(
1213
private val location: JsonPointer,

json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/array/UnevaluatedItemsAssertion.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.github.optimumcode.json.schema.internal.AssertionContext
88
import io.github.optimumcode.json.schema.internal.JsonSchemaAssertion
99
import io.github.optimumcode.json.schema.model.AbstractElement
1010
import io.github.optimumcode.json.schema.model.ArrayElement
11+
import io.github.optimumcode.json.schema.model.lastIndex
1112

1213
internal class UnevaluatedItemsAssertion(
1314
private val location: JsonPointer,

json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/util/ElementEqualityUtil.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ internal fun areEqualPrimitives(
4545
} else {
4646
when {
4747
first.isNull || second.isNull -> false
48+
// probably content should be compared ignoring the case - YAML allows different values for boolean
4849
first.isBoolean || second.isBoolean -> first.content == second.content
4950
else -> compareAsNumbers(first, second)
5051
}

json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/util/NumberParts.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.optimumcode.json.schema.internal.util
22

33
import io.github.optimumcode.json.schema.model.PrimitiveElement
4-
import kotlinx.serialization.json.double
54
import kotlin.math.absoluteValue
65

76
internal data class NumberParts(
@@ -28,7 +27,8 @@ private const val TEN: Double = 10.0
2827
internal fun numberParts(element: PrimitiveElement): NumberParts {
2928
if (element.content.run { contains(E_SMALL_CHAR) || contains(E_BIG_CHAR) }) {
3029
// FIXME: if we add support for YAML then we should handle +Inf and -Inf values correctly
31-
return element.double.run {
30+
val number = requireNotNull(element.number) { "element '${element.content}' is not a number" }
31+
return number.toDouble().run {
3232
var precision = 0
3333
var fractionalPart = rem(1.0).absoluteValue
3434
while (fractionalPart % 1.0 > 0) {

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import kotlinx.serialization.json.JsonNull
1010
import kotlinx.serialization.json.JsonObject
1111
import kotlinx.serialization.json.JsonPrimitive
1212
import kotlinx.serialization.json.booleanOrNull
13-
import kotlinx.serialization.json.double
1413
import kotlinx.serialization.json.doubleOrNull
1514
import kotlinx.serialization.json.longOrNull
1615
import kotlin.jvm.JvmInline
@@ -70,8 +69,6 @@ internal value class JsonPrimitiveWrapper(
7069
primitive.run {
7170
!isString && (longOrNull ?: doubleOrNull) != null
7271
}
73-
override val double: Double
74-
get() = primitive.double
7572
override val content: String
7673
get() = primitive.content
7774

@@ -99,8 +96,6 @@ internal value class StringWrapper(
9996
get() = false
10097
override val isNumber: Boolean
10198
get() = false
102-
override val double: Double
103-
get() = value.toDouble()
10499
override val content: String
105100
get() = value
106101

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,96 @@
11
package io.github.optimumcode.json.schema.model
22

3+
/**
4+
* [AbstractElement] represents one of 3 elements that can be consumed and validated:
5+
* * [ObjectElement] - corresponds to a JSON/YAML object or [Map]
6+
* * [ArrayElement] - corresponds to a collection of [AbstractElement]s
7+
* * [PrimitiveElement] - corresponds to a scalar value (string, number, boolean)
8+
*/
39
public sealed interface AbstractElement {
10+
/**
11+
* Implement [toString] method to provide a useful view that can be used in error message
12+
*/
413
override fun toString(): String
514
}
615

716
public interface PrimitiveElement : AbstractElement {
17+
/**
18+
* Returns `true` if the element is `null`.
19+
* Otherwise, returns `false`
20+
*/
821
public val isNull: Boolean
22+
23+
/**
24+
* Returns `true` if the element can be interpreted as string.
25+
* Otherwise, returns `false`
26+
*/
927
public val isString: Boolean
10-
public val number: Number?
28+
29+
/**
30+
* Returns `true` if the element can be interpreted as a boolean.
31+
* Otherwise, returns `false`
32+
*/
1133
public val isBoolean: Boolean
34+
35+
/**
36+
* Returns `true` if the element can be interpreted as a number.
37+
* Otherwise, returns `false`
38+
*/
1239
public val isNumber: Boolean
13-
public val double: Double
40+
41+
/**
42+
* Tries to parse the element as a [Number].
43+
* Must return either [Long] or [Double].
44+
* If the element cannot be interpreted as a number, returns `null`.
45+
*/
46+
public val number: Number?
47+
48+
/**
49+
* Returns the content of the element as plain string
50+
*/
1451
public val content: String
1552
}
1653

1754
internal val PrimitiveElement.contentOrNull: String?
1855
get() = if (isNull) null else content
1956

2057
public interface ObjectElement : AbstractElement, Sequence<Pair<String, AbstractElement>> {
58+
/**
59+
* Returns the set of keys defined in the [ObjectElement]
60+
*/
2161
public val keys: Set<String>
2262

63+
/**
64+
* Returns an [AbstractElement] associated with the [key].
65+
* If the [key] does not exist returns `null`
66+
*/
2367
public operator fun get(key: String): AbstractElement?
2468

69+
/**
70+
* Returns `true` if the [key] exists in the [ObjectElement].
71+
* Otherwise, returns `false`
72+
*/
2573
public operator fun contains(key: String): Boolean
2674

75+
/**
76+
* Returns number of keys in the [ObjectElement]
77+
*/
2778
public val size: Int
2879
}
2980

3081
internal fun ObjectElement.isEmpty(): Boolean = size == 0
3182

3283
public interface ArrayElement : AbstractElement, Sequence<AbstractElement> {
84+
/**
85+
* Returns the [AbstractElement] associated with the [index].
86+
*/
3387
public operator fun get(index: Int): AbstractElement
3488

89+
/**
90+
* Returns the number of [AbstractElement]s in the collection
91+
*/
3592
public val size: Int
36-
public val lastIndex: Int
37-
get() = size - 1
38-
}
93+
}
94+
95+
internal val ArrayElement.lastIndex: Int
96+
get() = size - 1

0 commit comments

Comments
 (0)