Skip to content

Commit 0646c19

Browse files
committed
Restore backward compatibility (for kotlin). Add deprecations
1 parent ec174d8 commit 0646c19

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

json-schema-validator/api/json-schema-validator.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,12 @@ public abstract interface class io/github/optimumcode/json/schema/extension/Exte
340340

341341
public abstract interface class io/github/optimumcode/json/schema/extension/ExternalAssertion {
342342
public abstract fun validate (Lio/github/optimumcode/json/schema/model/AbstractElement;Lio/github/optimumcode/json/schema/extension/ExternalAssertionContext;Lio/github/optimumcode/json/schema/ErrorCollector;)Z
343+
public abstract fun validate (Lkotlinx/serialization/json/JsonElement;Lio/github/optimumcode/json/schema/extension/ExternalAssertionContext;Lio/github/optimumcode/json/schema/ErrorCollector;)Z
344+
}
345+
346+
public final class io/github/optimumcode/json/schema/extension/ExternalAssertion$DefaultImpls {
347+
public static fun validate (Lio/github/optimumcode/json/schema/extension/ExternalAssertion;Lio/github/optimumcode/json/schema/model/AbstractElement;Lio/github/optimumcode/json/schema/extension/ExternalAssertionContext;Lio/github/optimumcode/json/schema/ErrorCollector;)Z
348+
public static fun validate (Lio/github/optimumcode/json/schema/extension/ExternalAssertion;Lkotlinx/serialization/json/JsonElement;Lio/github/optimumcode/json/schema/extension/ExternalAssertionContext;Lio/github/optimumcode/json/schema/ErrorCollector;)Z
343349
}
344350

345351
public abstract interface class io/github/optimumcode/json/schema/extension/ExternalAssertionContext {

json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/extension/ExternalAssertion.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package io.github.optimumcode.json.schema.extension
22

33
import io.github.optimumcode.json.schema.ErrorCollector
44
import io.github.optimumcode.json.schema.ExperimentalApi
5+
import io.github.optimumcode.json.schema.internal.wrapper.JsonWrapper
56
import io.github.optimumcode.json.schema.model.AbstractElement
7+
import kotlinx.serialization.json.JsonElement
68

79
/**
810
* This interface allows you to implement your own schema assertion.
911
* This interface **does not** allow implementing custom applicators.
1012
* Only simple assertions (like: _format_, _type_) can be implemented.
1113
*/
14+
@Suppress("detekt:ForbiddenComment")
1215
@ExperimentalApi
1316
public interface ExternalAssertion {
1417
/**
@@ -30,5 +33,24 @@ public interface ExternalAssertion {
3033
element: AbstractElement,
3134
context: ExternalAssertionContext,
3235
errorCollector: ErrorCollector,
33-
): Boolean
34-
}
36+
): Boolean =
37+
// TODO: remove it after two minor/major release
38+
validate(element.unwrap(), context, errorCollector)
39+
40+
// TODO: increase level to error in the next release
41+
@Deprecated(
42+
message = "override validate(AbstractElement, ExternalAssertionContext, ErrorCollector) instead",
43+
level = DeprecationLevel.WARNING,
44+
)
45+
public fun validate(
46+
element: JsonElement,
47+
context: ExternalAssertionContext,
48+
errorCollector: ErrorCollector,
49+
): Boolean = throw UnsupportedOperationException()
50+
}
51+
52+
internal fun AbstractElement.unwrap(): JsonElement =
53+
when (this) {
54+
is JsonWrapper -> unwrap()
55+
else -> error("unsupported element type: ${this::class.simpleName}")
56+
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ import kotlinx.serialization.json.doubleOrNull
1414
import kotlinx.serialization.json.longOrNull
1515
import kotlin.jvm.JvmInline
1616

17+
internal interface JsonWrapper {
18+
fun unwrap(): JsonElement
19+
}
20+
1721
@JvmInline
1822
internal value class JsonObjectWrapper(
1923
private val obj: JsonObject,
20-
) : ObjectElement {
24+
) : ObjectElement, JsonWrapper {
2125
override val keys: Set<String>
2226
get() = obj.keys
2327

@@ -35,13 +39,15 @@ internal value class JsonObjectWrapper(
3539
it.key to it.value.wrap()
3640
}.iterator()
3741

42+
override fun unwrap(): JsonElement = obj
43+
3844
override fun toString(): String = obj.toString()
3945
}
4046

4147
@JvmInline
4248
internal value class JsonArrayWrapper(
4349
private val array: JsonArray,
44-
) : ArrayElement {
50+
) : ArrayElement, JsonWrapper {
4551
override fun iterator(): Iterator<AbstractElement> = array.asSequence().map { it.wrap() }.iterator()
4652

4753
override fun get(index: Int): AbstractElement = array[index].wrap()
@@ -50,12 +56,14 @@ internal value class JsonArrayWrapper(
5056
get() = array.size
5157

5258
override fun toString(): String = array.toString()
59+
60+
override fun unwrap(): JsonElement = array
5361
}
5462

5563
@JvmInline
5664
internal value class JsonPrimitiveWrapper(
5765
private val primitive: JsonPrimitive,
58-
) : PrimitiveElement {
66+
) : PrimitiveElement, JsonWrapper {
5967
override val isNull: Boolean
6068
get() = primitive is JsonNull
6169
override val isString: Boolean
@@ -73,6 +81,8 @@ internal value class JsonPrimitiveWrapper(
7381
get() = primitive.content
7482

7583
override fun toString(): String = primitive.toString()
84+
85+
override fun unwrap(): JsonElement = primitive
7686
}
7787

7888
internal fun JsonElement.wrap(): AbstractElement =
@@ -85,7 +95,7 @@ internal fun JsonElement.wrap(): AbstractElement =
8595
@JvmInline
8696
internal value class StringWrapper(
8797
private val value: String,
88-
) : PrimitiveElement {
98+
) : PrimitiveElement, JsonWrapper {
8999
override val isNull: Boolean
90100
get() = false
91101
override val isString: Boolean
@@ -100,4 +110,6 @@ internal value class StringWrapper(
100110
get() = value
101111

102112
override fun toString(): String = value
113+
114+
override fun unwrap(): JsonElement = JsonPrimitive(value)
103115
}

0 commit comments

Comments
 (0)