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

gsonpath.adapter.standard.extension.range.RangeFunctions.kt Maven / Gradle / Ivy

Go to download

An annotation processor which generates Type Adapters for the Google Gson library

There is a newer version: 4.0.0
Show newest version
package gsonpath.adapter.standard.extension.range

import com.squareup.javapoet.CodeBlock
import gsonpath.adapter.standard.extension.addException
import gsonpath.util.`if`

/**
 * Creates the 'range' code validation code block.
 *
 * @param value the value being inspected
 * @param isFrom whether this is a 'from' or a 'to' range inspection
 * @param isInclusive whether the range validation is inclusive of the value
 * @param jsonPath the json path of the field being validated
 * @param variableName the name of the variable that is assigned back to the fieldName
 */
fun CodeBlock.Builder.handleRangeValue(value: String,
                                       isFrom: Boolean,
                                       isInclusive: Boolean,
                                       jsonPath: String,
                                       variableName: String): CodeBlock.Builder {

    val comparisonOperator: String =
            if (isFrom) {
                if (isInclusive) "<" else "<="
            } else {
                if (isInclusive) ">" else ">="
            }

    val expectedOperator: String =
            if (isFrom) {
                if (isInclusive) ">=" else ">"
            } else {
                if (isInclusive) "<=" else "<"
            }

    val label: String = if (isFrom) "from" else "to"

    return `if`("$variableName $comparisonOperator $value") {
        addException("Invalid '$label' range for JSON element '$jsonPath'. Expected: '$expectedOperator $value', " +
                """Found '" + $variableName + "'""")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy