All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mercateo.jsonschema.schema.mapper.PrimitiveJsonPropertyBuilder.kt Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
package com.mercateo.jsonschema.schema.mapper

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.databind.node.ObjectNode
import com.mercateo.jsonschema.property.Property
import com.mercateo.jsonschema.schema.ObjectContext

internal class PrimitiveJsonPropertyBuilder(private val nodeFactory: JsonNodeFactory) {
    private val genericJsonPropertyMapper: GenericJsonPropertyMapper

    init {
        this.genericJsonPropertyMapper = GenericJsonPropertyMapper(nodeFactory)
    }

    fun  forProperty(jsonProperty: ObjectContext): Builder {
        return Builder(jsonProperty)
    }

    internal inner class Builder(private val jsonProperty: ObjectContext) {
        private val propertyNode: ObjectNode

        init {
            propertyNode = ObjectNode(nodeFactory)
        }

        fun withType(type: String): Builder {
            propertyNode.put("type", type)
            return this
        }

        fun withDefaultAndAllowedValues(nodeCreator: (T) -> JsonNode): Builder {
            genericJsonPropertyMapper.addDefaultAndAllowedValues(propertyNode, this.jsonProperty, nodeCreator)
            return this
        }

        fun withDefaultValue(value: JsonNode): Builder {
            propertyNode.set("default", value)
            return this
        }

        fun build(): ObjectNode {
            return propertyNode
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy