![JAR search and dependency download from the Maven repository](/logo.png)
net.pwall.json.schema.codegen.Configurator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-kotlin-schema-codegen Show documentation
Show all versions of json-kotlin-schema-codegen Show documentation
Code generation from JSON Schema to Kotlin or Java
/*
* @(#) Configurator.kt
*
* json-kotlin-schema-codegen JSON Schema Code Generation
* Copyright (c) 2021, 2022, 2023, 2024 Peter Wall
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.pwall.json.schema.codegen
import java.net.URI
import io.kjson.JSON.displayValue
import io.kjson.JSON.typeError
import io.kjson.JSONArray
import io.kjson.JSONBoolean
import io.kjson.JSONObject
import io.kjson.JSONString
import io.kjson.JSONValue
import io.kjson.mustache.Template
import io.kjson.pointer.JSONPointer
import io.kjson.pointer.JSONRef
import io.kjson.pointer.forEachKey
import io.kjson.pointer.optionalChild
import io.kjson.pointer.withOptionalChild
import net.pwall.json.schema.JSONSchema
import net.pwall.json.schema.codegen.CodeGenerator.Companion.fatal
import net.pwall.json.schema.output.BasicErrorEntry
import net.pwall.json.schema.validation.FormatValidator
object Configurator {
val extensionKeywordPattern = Regex("^x(-[A-Za-z0-9]+)+$")
fun configure(generator: CodeGenerator, ref: JSONRef, uri: URI? = null) {
// TODO validate against schema?
val extensionValidators = mutableMapOf>()
val nonStandardFormats = mutableMapOf()
val parser = generator.schemaParser
ref.optionalChild("title")?.nonEmptyString()
ref.optionalChild("version")?.nonEmptyString()
ref.optionalChild("description")?.nonEmptyString()
ref.withOptionalChild("packageName") {
generator.basePackageName = optionalNonEmptyString()
}
ref.withOptionalChild("markerInterface") {
generator.markerInterface = optionalNonEmptyString()?.let { ClassName.of(it) }
}
ref.withOptionalChild("generatorComment") {
generator.generatorComment = optionalNonEmptyString()
}
ref.withOptionalChild("commentTemplate") {
generator.commentTemplate = optionalNonEmptyString()?.let { Template.parse(it) }
}
ref.withOptionalChild("targetLanguage") {
generator.targetLanguage = when (it.value) {
"kotlin" -> TargetLanguage.KOTLIN
"java" -> TargetLanguage.JAVA
"typescript" -> TargetLanguage.TYPESCRIPT
else -> invalid()
}
}
ref.withOptionalChild("nestedClassNameOption") {
generator.nestedClassNameOption = when (it.value) {
"property" -> CodeGenerator.NestedClassNameOption.USE_NAME_FROM_PROPERTY
"refSchema" -> CodeGenerator.NestedClassNameOption.USE_NAME_FROM_REF_SCHEMA
else -> invalid()
}
}
ref.withOptionalChild("additionalPropertiesOption") {
generator.additionalPropertiesOption = when (it.value) {
"ignore" -> CodeGenerator.AdditionalPropertiesOption.IGNORE
"strict" -> CodeGenerator.AdditionalPropertiesOption.STRICT
else -> invalid()
}
}
ref.withOptionalChild("examplesValidationOption") {
generator.examplesValidationOption = when (it.value) {
"none" -> {
parser.options.validateExamples = false
CodeGenerator.ValidationOption.NONE
}
"warn" -> {
parser.options.validateExamples = true
CodeGenerator.ValidationOption.WARN
}
"block" -> {
parser.options.validateExamples = true
CodeGenerator.ValidationOption.BLOCK
}
else -> invalid()
}
}
ref.withOptionalChild("defaultValidationOption") {
generator.defaultValidationOption = when (it.value) {
"none" -> {
parser.options.validateDefault = false
CodeGenerator.ValidationOption.NONE
}
"warn" -> {
parser.options.validateDefault = true
CodeGenerator.ValidationOption.WARN
}
"block" -> {
CodeGenerator.ValidationOption.BLOCK
}
else -> {
parser.options.validateDefault = true
invalid()
}
}
}
ref.withOptionalChild("extensibleEnumKeyword") {
it.value.let { keyword ->
if (!extensionKeywordPattern.containsMatchIn(keyword))
fatal("Illegal extension keyword at $pointer")
generator.extensibleEnumKeyword = keyword
}
}
ref.withOptionalChild("derivePackageFromStructure") {
generator.derivePackageFromStructure = it.value
}
ref.withOptionalChild("extensionValidations") {
forEachKey { ext ->
forEachKey {
extensionValidators.getOrPut(ext) { mutableMapOf() }[it] =
CustomValidator(uri, pointer, it, parser.parseSchema(base!!, pointer, uri))
}
}
}
ref.withOptionalChild("nonStandardFormat") {
forEachKey {
nonStandardFormats[it] = CustomFormat(uri, pointer, it, parser.parseSchema(base!!, pointer, uri))
}
}
ref.withOptionalChild("customClasses") {
withOptionalChild("format") {
forEachKey {
generator.addCustomClassByFormat(it, nonEmptyString())
}
}
withOptionalChild("uri") {
forEachKey {
generator.addCustomClassByURI(URI(it), nonEmptyString())
}
}
withOptionalChild("extension") {
forEachKey { ext ->
forEachKey {
generator.addCustomClassByExtension(ext, it, nonEmptyString())
}
}
}
forEachKey {
if (it !in listOf("format", "uri", "extension"))
fatal("Config item $pointer unrecognised mapping type")
}
}
ref.withOptionalChild("decimalClassName") {
generator.decimalClass = ClassName.of(nonEmptyString())
}
ref.withOptionalChild("classNames") {
forEachKey {
generator.addClassNameMapping(URI(it), nonEmptyString())
}
}
ref.withOptionalChild("annotations") {
withOptionalChild("classes") {
forEachKey { entry ->
if (node != null)
generator.addClassAnnotation(entry, Template.parse(asRef().nonEmptyString()))
else
generator.addClassAnnotation(entry)
}
}
withOptionalChild("fields") {
forEachKey { entry ->
if (node != null)
generator.addFieldAnnotation(entry, Template.parse(asRef().nonEmptyString()))
else
generator.addFieldAnnotation(entry)
}
}
forEachKey {
if (it != "classes" && it != "fields")
fatal("Config item $pointer unrecognised annotation type")
}
}
ref.withOptionalChild("companionObject") {
when (it) {
is JSONBoolean -> generator.companionObjectForAll = it.value
is JSONArray -> {
generator.companionObjectForClasses.addAll(it.map { v ->
if (v !is JSONString)
fatal("Config item companionObject invalid value")
v.value
})
}
else -> fatal("Config item companionObject invalid value")
}
}
if (extensionValidators.isNotEmpty()) {
parser.customValidationHandler = { key, _, _, value ->
extensionValidators[key]?.get((value as? JSONString)?.value)
}
}
if (nonStandardFormats.isNotEmpty()) {
parser.nonstandardFormatHandler = { keyword ->
nonStandardFormats[keyword]?.let { FormatValidator.DelegatingFormatChecker(keyword, it) }
}
}
}
private fun JSONRef.optionalNonEmptyString(): String? = node?.let {
if (it.value.isEmpty())
it.typeError("non-empty string", pointer, "Config item")
it.value
}
private fun JSONRef.nonEmptyString(): String = node.let {
if (it.value.isEmpty())
it.typeError("non-empty string", pointer, "Config item")
it.value
}
private fun JSONRef.invalid(): Nothing {
fatal("Config item $pointer invalid - ${node.displayValue()}")
}
class CustomValidator(uri: URI?, location: JSONPointer, val name: String, val schema: JSONSchema) :
JSONSchema.Validator(uri, location) {
override fun getErrorEntry(
relativeLocation: JSONPointer,
json: JSONValue?,
instanceLocation: JSONPointer,
): BasicErrorEntry? {
if (schema is Validator)
return schema.getErrorEntry(relativeLocation.child(name), json, instanceLocation)
val basicOutput = schema.validateBasic(relativeLocation.child(name), json, instanceLocation)
return basicOutput.errors?.let {
it.firstOrNull { e -> e.error != subSchemaErrorMessage }
}
}
override fun validate(json: JSONValue?, instanceLocation: JSONPointer): Boolean =
schema.validate(json, instanceLocation)
}
class CustomFormat(uri: URI?, location: JSONPointer, val name: String, val schema: JSONSchema) :
JSONSchema.Validator(uri, location) {
override fun getErrorEntry(
relativeLocation: JSONPointer,
json: JSONValue?,
instanceLocation: JSONPointer,
): BasicErrorEntry? {
if (schema is Validator)
return schema.getErrorEntry(relativeLocation, json, instanceLocation)
val basicOutput = schema.validateBasic(relativeLocation, json, instanceLocation)
return basicOutput.errors?.let {
it.firstOrNull { e -> e.error != subSchemaErrorMessage }
}
}
override fun validate(json: JSONValue?, instanceLocation: JSONPointer): Boolean =
schema.validate(json, instanceLocation)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy