Search in sources :

Example 21 with CodeBuilder

use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.

the class BeanValidatorBuilder method validateMethod.

private String validateMethod(Class<?> beanClass, String parentPath) {
    String methodName = "validate" + beanClass.getSimpleName() + (index++);
    var builder = new CodeBuilder().append("private void {}({} bean, {} errors, boolean partial) {\n", methodName, type(beanClass), type(ValidationErrors.class));
    for (Field field : Classes.instanceFields(beanClass)) {
        if (!hasValidationAnnotation(field))
            continue;
        Type fieldType = field.getGenericType();
        Class<?> fieldClass = GenericTypes.rawClass(fieldType);
        String pathLiteral = variable(path(field, parentPath));
        builder.indent(1).append("if (bean.{} == null) {\n", field.getName());
        NotNull notNull = field.getDeclaredAnnotation(NotNull.class);
        if (notNull != null)
            builder.indent(2).append("if (!partial) errors.add({}, {}, null);\n", pathLiteral, variable(notNull.message()));
        builder.indent(1).append("} else {\n");
        if (String.class.equals(fieldClass)) {
            buildStringValidation(builder, field, pathLiteral);
        } else if (GenericTypes.isList(fieldType)) {
            buildListValidation(builder, field, pathLiteral, parentPath);
        } else if (GenericTypes.isMap(fieldType)) {
            buildMapValidation(builder, field, pathLiteral, parentPath);
        } else if (Number.class.isAssignableFrom(fieldClass)) {
            buildNumberValidation(builder, field, pathLiteral);
        } else if (!isValueClass(fieldClass)) {
            String method = validateMethod(fieldClass, path(field, parentPath));
            builder.indent(2).append("{}(bean.{}, errors, partial);\n", method, field.getName());
        }
        builder.indent(1).append("}\n");
    }
    builder.append('}');
    this.builder.addMethod(builder.build());
    return methodName;
}
Also used : Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) NotNull(core.framework.api.validate.NotNull) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 22 with CodeBuilder

use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method validate.

private void validate(BulkResponse response) {
    if (!response.errors())
        return;
    var builder = new CodeBuilder();
    builder.append("bulk operation failed, errors=[\n");
    for (BulkResponseItem item : response.items()) {
        ErrorCause error = item.error();
        if (error != null) {
            builder.append("id={}, error={}, stackTrace={}\n", item.id(), error.reason(), error.stackTrace());
        }
    }
    builder.append("]");
    throw new SearchException(builder.build());
}
Also used : BulkResponseItem(co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem) ErrorCause(co.elastic.clients.elasticsearch._types.ErrorCause) SearchException(core.framework.search.SearchException) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Aggregations

CodeBuilder (core.framework.internal.asm.CodeBuilder)22 Field (java.lang.reflect.Field)9 Type (java.lang.reflect.Type)4 Column (core.framework.db.Column)3 ArrayList (java.util.ArrayList)3 PathParam (core.framework.api.web.service.PathParam)2 QueryParam (core.framework.api.web.service.QueryParam)2 PrimaryKey (core.framework.db.PrimaryKey)2 LocalDate (java.time.LocalDate)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)1 BulkResponseItem (co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem)1 NotNull (core.framework.api.validate.NotNull)1 ResponseStatus (core.framework.api.web.service.ResponseStatus)1 Table (core.framework.db.Table)1 SearchException (core.framework.search.SearchException)1 Request (core.framework.web.Request)1 Response (core.framework.web.Response)1 ActionDocument (core.log.domain.ActionDocument)1