gsonpath.generator.extension.range.RangeFunctions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gsonpath-compiler Show documentation
Show all versions of gsonpath-compiler Show documentation
An annotation processor which generates Type Adapters for the Google Gson library
package gsonpath.generator.extension.range
import com.squareup.javapoet.CodeBlock
import gsonpath.generator.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 + "'""")
}
}