in scrooge-thrift-validation/src/main/scala/com/twitter/scrooge/thrift_validation/ThriftValidator.scala [68:132]
def validateField[T](
fieldName: String,
fieldValue: T,
fieldAnnotations: Map[String, String]
): Set[ThriftValidationViolation] = {
val violations: mutable.Set[ThriftValidationViolation] = mutable.Set.empty
for ((annotationKey, annotationValue) <- fieldAnnotations) {
// skip validations if an annotation is not recognized. This is in order
// not to break the services when they use annotations for other purposes
// other than Thrift Validations.
customAnnotations.get(annotationKey) match {
case Some(constraintValidator) =>
val clazz = constraintValidator.annotationClass
val violation = {
if (clazz == classOf[java.lang.Long] || clazz == classOf[Long]) {
validateCustomConstraint[T, Long](
fieldName,
fieldValue,
annotationValue.toLong,
constraintValidator.asInstanceOf[ThriftConstraintValidator[T, Long]])
} else if (clazz == classOf[java.lang.Integer] || clazz == classOf[Int]) {
validateCustomConstraint[T, Int](
fieldName,
fieldValue,
annotationValue.toInt,
constraintValidator.asInstanceOf[ThriftConstraintValidator[T, Int]])
} else if (clazz == classOf[java.lang.Double] || clazz == classOf[Double]) {
validateCustomConstraint[T, Double](
fieldName,
fieldValue,
annotationValue.toDouble,
constraintValidator.asInstanceOf[ThriftConstraintValidator[T, Double]])
} else if (clazz == classOf[java.lang.Short] || clazz == classOf[Short]) {
validateCustomConstraint[T, Short](
fieldName,
fieldValue,
annotationValue.toShort,
constraintValidator.asInstanceOf[ThriftConstraintValidator[T, Short]])
} else if (clazz == classOf[java.lang.Byte] || clazz == classOf[Byte]) {
validateCustomConstraint[T, Byte](
fieldName,
fieldValue,
annotationValue.toByte,
constraintValidator.asInstanceOf[ThriftConstraintValidator[T, Byte]])
} else if (clazz == classOf[java.lang.String] || clazz == classOf[String]) {
validateCustomConstraint[T, String](
fieldName,
fieldValue,
annotationValue,
constraintValidator.asInstanceOf[ThriftConstraintValidator[T, String]])
} else {
throw new IllegalArgumentException(
s"The annotation with value $annotationValue's type is $clazz, $clazz is not among " +
s"the supported types Int, Long, Double, Short, Byte, and String.")
}
}
violations ++= violation
case None =>
// skip validations if an annotation is not recognized. This is in order
// not to break the services when they use annotations for other purposes
// other than Thrift Validations.
}
}
violations.toSet
}