Skip to content

Commit

Permalink
improve java-friendlyness of the builder
Browse files Browse the repository at this point in the history
  • Loading branch information
erosb committed Aug 25, 2024
1 parent 94cc3f0 commit 5039923
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<description>JSON Schema Parser and Validator</description>
<url>https://github.com/erosb/json-sKema</url>

<packaging>jar</packaging>

<scm>
<connection>https://github.com/erosb/json-sKema</connection>
<url>https://github.com/erosb/json-sKema</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,26 @@ abstract class SchemaBuilder {
@JvmStatic
fun allOf(subschemas: List<SchemaBuilder>): CompositeSchemaBuilder = empty().allOf(subschemas)

@JvmStatic
fun allOf(vararg subschemas: SchemaBuilder): CompositeSchemaBuilder = empty().allOf(*subschemas)

@JvmStatic
fun oneOf(subschemas: List<SchemaBuilder>): CompositeSchemaBuilder = empty().oneOf(subschemas)

@JvmStatic
fun oneOf(vararg subschemas: SchemaBuilder): CompositeSchemaBuilder = empty().oneOf(*subschemas)

@JvmStatic
fun anyOf(subschemas: List<SchemaBuilder>): CompositeSchemaBuilder = empty().anyOf(subschemas)

@JvmStatic
fun anyOf(vararg subschemas: SchemaBuilder): CompositeSchemaBuilder = empty().anyOf(*subschemas)

@JvmStatic
fun not(notSchema: SchemaBuilder): CompositeSchemaBuilder = empty().not(notSchema)

@JvmStatic
fun const(constant: IJsonValue): CompositeSchemaBuilder = empty().const(constant)
fun constSchema(constant: IJsonValue): CompositeSchemaBuilder = empty().constSchema(constant)

@JvmStatic
fun enumSchema(vararg enumValues: IJsonValue) = enumSchema(enumValues.toList())
Expand Down Expand Up @@ -299,22 +308,28 @@ class CompositeSchemaBuilder internal constructor() : SchemaBuilder() {
AllOfSchema(subschemas.map { it.buildAt(loc) }, loc)
}

fun allOf(vararg subschemas: SchemaBuilder) = allOf(subschemas.toList())

fun oneOf(subschemas: List<SchemaBuilder>) =
appendSupplier(Keyword.ONE_OF) { loc ->
OneOfSchema(subschemas.map { it.buildAt(loc) }, loc)
}

fun oneOf(vararg subschemas: SchemaBuilder) = oneOf(subschemas.toList())

fun anyOf(subschemas: List<SchemaBuilder>) =
appendSupplier(Keyword.ANY_OF) { loc ->
AnyOfSchema(subschemas.map { it.buildAt(loc) }, loc)
}

fun anyOf(vararg subschemas: SchemaBuilder) = anyOf(subschemas.toList())

fun not(negatedSchema: SchemaBuilder) =
appendSupplier(Keyword.NOT) { loc ->
NotSchema(negatedSchema.buildAt(loc), loc)
}

fun const(constant: IJsonValue) =
fun constSchema(constant: IJsonValue) =
appendSupplier(Keyword.CONST) { loc ->
ConstSchema(constant, loc)
}
Expand Down
16 changes: 9 additions & 7 deletions src/main/kotlin/com/github/erosb/jsonsKema/JsonValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,20 @@ abstract class JsonValue(override val location: SourceLocation = UnknownSource)
final override fun toString(): String = accept(JsonPrintingVisitor())!!
}

data class JsonNull(override val location: SourceLocation = UnknownSource) : IJsonNull, JsonValue(location) {
data class JsonNull @JvmOverloads constructor(override val location: SourceLocation = UnknownSource) : IJsonNull, JsonValue(location) {
override fun unwrap(): Any? = null
override fun equals(other: Any?) = super.equals(other)
}

data class JsonBoolean(
data class JsonBoolean @JvmOverloads constructor(
override val value: Boolean,
override val location: SourceLocation = UnknownSource
) : JsonValue(location), IJsonBoolean {
override fun unwrap(): Any = value
override fun equals(other: Any?) = super.equals(other)
}

data class JsonNumber(
data class JsonNumber @JvmOverloads constructor(
override val value: Number,
override val location: SourceLocation = UnknownSource
) : JsonValue(location), IJsonNumber {
Expand All @@ -247,15 +247,17 @@ data class JsonNumber(
}
}

data class JsonString(override val value: String, override val location: SourceLocation = UnknownSource) :
JsonValue(location), IJsonString {
data class JsonString @JvmOverloads constructor(
override val value: String,
override val location: SourceLocation = UnknownSource
) : JsonValue(location), IJsonString {

override fun equals(other: Any?) = super.equals(other)

override fun unwrap() = value
}

data class JsonArray(
data class JsonArray @JvmOverloads constructor(
override val elements: List<JsonValue>,
override val location: SourceLocation = UnknownSource
) : JsonValue(location), IJsonArray<JsonValue> {
Expand All @@ -267,7 +269,7 @@ data class JsonArray(
override fun equals(other: Any?) = super.equals(other)
}

data class JsonObject(
data class JsonObject @JvmOverloads constructor(
override val properties: Map<JsonString, JsonValue>,
override val location: SourceLocation = UnknownSource
) : JsonValue(location), IJsonObject<JsonString, JsonValue> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class SchemaBuilderTest {
fun not() {
val schema = SchemaBuilder.not(SchemaBuilder.typeObject()
.property("propA", SchemaBuilder.typeNumber().not(
SchemaBuilder.const(JsonNumber(2))
SchemaBuilder.constSchema(JsonNumber(2))
))
).build()

Expand Down

0 comments on commit 5039923

Please sign in to comment.