Skip to content

Commit

Permalink
Restore backward compatibility (for kotlin). Add deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
OptimumCode committed Sep 2, 2024
1 parent ec174d8 commit 0646c19
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
6 changes: 6 additions & 0 deletions json-schema-validator/api/json-schema-validator.api
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ public abstract interface class io/github/optimumcode/json/schema/extension/Exte

public abstract interface class io/github/optimumcode/json/schema/extension/ExternalAssertion {
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
public abstract fun validate (Lkotlinx/serialization/json/JsonElement;Lio/github/optimumcode/json/schema/extension/ExternalAssertionContext;Lio/github/optimumcode/json/schema/ErrorCollector;)Z
}

public final class io/github/optimumcode/json/schema/extension/ExternalAssertion$DefaultImpls {
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
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
}

public abstract interface class io/github/optimumcode/json/schema/extension/ExternalAssertionContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package io.github.optimumcode.json.schema.extension

import io.github.optimumcode.json.schema.ErrorCollector
import io.github.optimumcode.json.schema.ExperimentalApi
import io.github.optimumcode.json.schema.internal.wrapper.JsonWrapper
import io.github.optimumcode.json.schema.model.AbstractElement
import kotlinx.serialization.json.JsonElement

/**
* This interface allows you to implement your own schema assertion.
* This interface **does not** allow implementing custom applicators.
* Only simple assertions (like: _format_, _type_) can be implemented.
*/
@Suppress("detekt:ForbiddenComment")
@ExperimentalApi
public interface ExternalAssertion {
/**
Expand All @@ -30,5 +33,24 @@ public interface ExternalAssertion {
element: AbstractElement,
context: ExternalAssertionContext,
errorCollector: ErrorCollector,
): Boolean
}
): Boolean =
// TODO: remove it after two minor/major release
validate(element.unwrap(), context, errorCollector)

Check warning on line 38 in json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/extension/ExternalAssertion.kt

View check run for this annotation

Codecov / codecov/patch

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

Added line #L38 was not covered by tests

// TODO: increase level to error in the next release
@Deprecated(
message = "override validate(AbstractElement, ExternalAssertionContext, ErrorCollector) instead",
level = DeprecationLevel.WARNING,
)
public fun validate(
element: JsonElement,
context: ExternalAssertionContext,
errorCollector: ErrorCollector,
): Boolean = throw UnsupportedOperationException()

Check warning on line 49 in json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/extension/ExternalAssertion.kt

View check run for this annotation

Codecov / codecov/patch

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

Added line #L49 was not covered by tests
}

internal fun AbstractElement.unwrap(): JsonElement =
when (this) {

Check warning on line 53 in json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/extension/ExternalAssertion.kt

View check run for this annotation

Codecov / codecov/patch

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

Added line #L53 was not covered by tests
is JsonWrapper -> unwrap()
else -> error("unsupported element type: ${this::class.simpleName}")

Check warning on line 55 in json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/extension/ExternalAssertion.kt

View check run for this annotation

Codecov / codecov/patch

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

Added line #L55 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import kotlinx.serialization.json.doubleOrNull
import kotlinx.serialization.json.longOrNull
import kotlin.jvm.JvmInline

internal interface JsonWrapper {
fun unwrap(): JsonElement
}

@JvmInline
internal value class JsonObjectWrapper(
private val obj: JsonObject,
) : ObjectElement {
) : ObjectElement, JsonWrapper {
override val keys: Set<String>
get() = obj.keys

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

override fun unwrap(): JsonElement = obj

Check warning on line 42 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#L42

Added line #L42 was not covered by tests

override fun toString(): String = obj.toString()
}

@JvmInline
internal value class JsonArrayWrapper(
private val array: JsonArray,
) : ArrayElement {
) : ArrayElement, JsonWrapper {
override fun iterator(): Iterator<AbstractElement> = array.asSequence().map { it.wrap() }.iterator()

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

override fun toString(): String = array.toString()

override fun unwrap(): JsonElement = array

Check warning on line 60 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#L60

Added line #L60 was not covered by tests
}

@JvmInline
internal value class JsonPrimitiveWrapper(
private val primitive: JsonPrimitive,
) : PrimitiveElement {
) : PrimitiveElement, JsonWrapper {
override val isNull: Boolean
get() = primitive is JsonNull
override val isString: Boolean
Expand All @@ -73,6 +81,8 @@ internal value class JsonPrimitiveWrapper(
get() = primitive.content

override fun toString(): String = primitive.toString()

override fun unwrap(): JsonElement = primitive

Check warning on line 85 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#L85

Added line #L85 was not covered by tests
}

internal fun JsonElement.wrap(): AbstractElement =
Expand All @@ -85,7 +95,7 @@ internal fun JsonElement.wrap(): AbstractElement =
@JvmInline
internal value class StringWrapper(
private val value: String,
) : PrimitiveElement {
) : PrimitiveElement, JsonWrapper {
override val isNull: Boolean
get() = false
override val isString: Boolean
Expand All @@ -100,4 +110,6 @@ internal value class StringWrapper(
get() = value

override fun toString(): String = value

Check warning on line 112 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#L112

Added line #L112 was not covered by tests

override fun unwrap(): JsonElement = JsonPrimitive(value)

Check warning on line 114 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#L114

Added line #L114 was not covered by tests
}

0 comments on commit 0646c19

Please sign in to comment.