From e39b8f562520326a6c9200a3c2e4a7fae8f69b5b Mon Sep 17 00:00:00 2001 From: Gavriil Sitnikov Date: Thu, 27 Apr 2017 01:55:06 +0300 Subject: [PATCH] generation of validation added --- gradle/apiGeneration.gradle | 66 ++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/gradle/apiGeneration.gradle b/gradle/apiGeneration.gradle index a1c2a68..2cdfd3d 100644 --- a/gradle/apiGeneration.gradle +++ b/gradle/apiGeneration.gradle @@ -21,8 +21,8 @@ abstract class SchemeObject { abstract void readLine(String line, Map objects) } +//TODO: datetime //TODO: missable in future -//TODO: validation/check for useless types //TODO: register all maps/collections classes and work with java typical types class ImportObject extends SchemeObject { @@ -107,7 +107,7 @@ class EnumObject extends SchemeObject { } } - JavaFile.builder("com.touchin.sberinkas", enumBuilder.build()).build().writeTo(directory); + JavaFile.builder("com.touchin.sberinkas", enumBuilder.build()).indent(" ").build().writeTo(directory); } Type typeOf(String value) { @@ -149,7 +149,7 @@ enum FieldType { BOOLEAN(TypeName.BOOLEAN), INT(TypeName.INT), LONG(TypeName.LONG), FLOAT(TypeName.FLOAT), STRING(ClassName.get(String.class)), ARRAY(ClassName.get(List.class)), MAP(ClassName.get(Map.class)), - ENUM(null), MODEL(null), GENERIC(null) + ENUM(null), MODEL(null), IMPORTED_MODEL(null), GENERIC(null) final TypeName typeName @@ -167,10 +167,14 @@ enum FieldType { case "long": return LONG case "float": return FLOAT default: - if (objects.get(typeString) instanceof EnumObject) { + SchemeObject object = objects.get(typeString); + if (object instanceof EnumObject) { return ENUM } - if (objects.get(typeString) != null) { + if (object instanceof ImportObject) { + return IMPORTED_MODEL + } + if (object instanceof ClassObject) { return MODEL } return GENERIC @@ -268,11 +272,21 @@ class FieldInfo { final String apiName final boolean nullable final boolean required + final boolean nonEmptyCollection + final boolean solidCollection final FieldType fieldType final TypeName typeName FieldInfo(String apiName, String typeString, String subTypeString, Map objects) { this.apiName = apiName + solidCollection = typeString.endsWith('%') + if (solidCollection) { + typeString = typeString.substring(0, typeString.length() - 1) + } + nonEmptyCollection = typeString.endsWith('|') + if (nonEmptyCollection) { + typeString = typeString.substring(0, typeString.length() - 1) + } required = typeString.endsWith('*') if (required) { typeString = typeString.substring(0, typeString.length() - 1) @@ -352,6 +366,39 @@ class FieldInfo { return builder.build() } + void addValidateStatements(String name, MethodSpec.Builder validateMethod) { + final String prefix + if (!nullable) { + prefix = "" + validateMethod.addStatement("validateNotNull(\$L)", name) + } else { + prefix = "if(" + name + "!=null)" + } + if (fieldType == FieldType.ARRAY) { + if (nonEmptyCollection) { + validateMethod.addStatement(prefix + "validateCollectionNotEmpty(\$L)", name) + } + if (solidCollection) { + validateMethod.addStatement(prefix + "validateCollection(\$L, CollectionValidationRule.EXCEPTION_IF_ANY_INVALID)", name) + } else if (nonEmptyCollection) { + validateMethod.addStatement(prefix + "validateCollection(\$L, CollectionValidationRule.EXCEPTION_IF_ALL_INVALID)", name) + } else { + validateMethod.addStatement(prefix + "validateCollection(\$L, CollectionValidationRule.REMOVE_INVALID_ITEMS)", name) + } + } else if (fieldType == FieldType.MAP) { + if (nonEmptyCollection) { + validateMethod.addStatement(prefix + "validateCollectionNotEmpty(\$L.values())", name) + } + validateMethod.addStatement(prefix + "validateCollection(\$L.values(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID)", name) + } else if (fieldType == FieldType.MODEL) { + validateMethod.addStatement(prefix + "\$L.validate()", name) + } else if (fieldType == FieldType.GENERIC || fieldType == FieldType.IMPORTED_MODEL) { + validateMethod.addStatement("if(\$L instanceof \$T) ((\$T)\$L).validate()", name, + ClassName.bestGuess("ru.touchin.templates.ApiModel"), + ClassName.bestGuess("ru.touchin.templates.ApiModel"), name) + } + } + } class ClassObject extends SchemeObject { @@ -404,6 +451,12 @@ class ClassObject extends SchemeObject { .addAnnotation(Override.class) .returns(TypeName.INT) + MethodSpec.Builder validateMethod = MethodSpec.methodBuilder("validate") + .addModifiers(Modifier.PUBLIC) + .addAnnotation(Override.class) + .addException(ClassName.bestGuess("ValidationException")) + .addStatement("super.validate()") + boolean first = true CodeBlock.Builder equalsStatement = CodeBlock.builder() CodeBlock.Builder hashCodeStatement = CodeBlock.builder() @@ -412,6 +465,7 @@ class ClassObject extends SchemeObject { classBuilder.addField(entry.value.createField(entry.key)) classBuilder.addMethod(entry.value.createGetter(entry.key)) classBuilder.addMethod(entry.value.createSetter(entry.key)) + entry.value.addValidateStatements(entry.key, validateMethod) if (first) { if (superclass == null) { hashCodeStatement.add("return \$T.hashCode(\$L", ClassName.bestGuess("ru.touchin.roboswag.core.utils.ObjectUtils"), entry.key) @@ -441,10 +495,12 @@ class ClassObject extends SchemeObject { equalsStatement.add(";") hashCodeStatement.add(");") + classBuilder.addMethod(validateMethod.build()) classBuilder.addMethod(equalsMethod.addCode(equalsStatement.build()).build()) classBuilder.addMethod(hashCodeMethod.addCode(hashCodeStatement.build()).build()) JavaFile.builder("com.touchin.sberinkas", classBuilder.build()) + .indent(" ") .build().writeTo(directory); }