From 5039923b93c178c32db1cd43e4ab380b09e84491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Sun, 25 Aug 2024 08:27:10 +0200 Subject: [PATCH] improve java-friendlyness of the builder --- pom.xml | 2 ++ .../erosb/jsonsKema/CompositeSchemaBuilder.kt | 19 +++++++++++++++++-- .../com/github/erosb/jsonsKema/JsonValue.kt | 16 +++++++++------- .../jsonsKema_fluent/SchemaBuilderTest.kt | 2 +- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 10b6019..a6b5941 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,8 @@ JSON Schema Parser and Validator https://github.com/erosb/json-sKema + jar + https://github.com/erosb/json-sKema https://github.com/erosb/json-sKema diff --git a/src/main/kotlin/com/github/erosb/jsonsKema/CompositeSchemaBuilder.kt b/src/main/kotlin/com/github/erosb/jsonsKema/CompositeSchemaBuilder.kt index b590d1c..2fdb72c 100644 --- a/src/main/kotlin/com/github/erosb/jsonsKema/CompositeSchemaBuilder.kt +++ b/src/main/kotlin/com/github/erosb/jsonsKema/CompositeSchemaBuilder.kt @@ -66,17 +66,26 @@ abstract class SchemaBuilder { @JvmStatic fun allOf(subschemas: List): CompositeSchemaBuilder = empty().allOf(subschemas) + @JvmStatic + fun allOf(vararg subschemas: SchemaBuilder): CompositeSchemaBuilder = empty().allOf(*subschemas) + @JvmStatic fun oneOf(subschemas: List): CompositeSchemaBuilder = empty().oneOf(subschemas) + @JvmStatic + fun oneOf(vararg subschemas: SchemaBuilder): CompositeSchemaBuilder = empty().oneOf(*subschemas) + @JvmStatic fun anyOf(subschemas: List): 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()) @@ -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) = appendSupplier(Keyword.ONE_OF) { loc -> OneOfSchema(subschemas.map { it.buildAt(loc) }, loc) } + fun oneOf(vararg subschemas: SchemaBuilder) = oneOf(subschemas.toList()) + fun anyOf(subschemas: List) = 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) } diff --git a/src/main/kotlin/com/github/erosb/jsonsKema/JsonValue.kt b/src/main/kotlin/com/github/erosb/jsonsKema/JsonValue.kt index f4d1433..ba0acce 100644 --- a/src/main/kotlin/com/github/erosb/jsonsKema/JsonValue.kt +++ b/src/main/kotlin/com/github/erosb/jsonsKema/JsonValue.kt @@ -221,12 +221,12 @@ 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 { @@ -234,7 +234,7 @@ data class JsonBoolean( 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 { @@ -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, override val location: SourceLocation = UnknownSource ) : JsonValue(location), IJsonArray { @@ -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, override val location: SourceLocation = UnknownSource ) : JsonValue(location), IJsonObject { diff --git a/src/test/kotlin/com/github/erosb/jsonsKema_fluent/SchemaBuilderTest.kt b/src/test/kotlin/com/github/erosb/jsonsKema_fluent/SchemaBuilderTest.kt index aacbfb3..c9af4b4 100644 --- a/src/test/kotlin/com/github/erosb/jsonsKema_fluent/SchemaBuilderTest.kt +++ b/src/test/kotlin/com/github/erosb/jsonsKema_fluent/SchemaBuilderTest.kt @@ -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()