Skip to content

Commit

Permalink
polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
Squiry committed Jul 31, 2023
1 parent 5a8b79a commit f144958
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.squareup.javapoet.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import ru.tinkoff.kora.annotation.processor.common.*;
import ru.tinkoff.kora.annotation.processor.common.CommonClassNames;
import ru.tinkoff.kora.annotation.processor.common.CommonUtils;
import ru.tinkoff.kora.annotation.processor.common.FieldFactory;
import ru.tinkoff.kora.annotation.processor.common.Visitors;
import ru.tinkoff.kora.common.Tag;
import ru.tinkoff.kora.database.annotation.processor.DbUtils;
import ru.tinkoff.kora.database.annotation.processor.QueryWithParameters;
Expand All @@ -23,10 +26,7 @@
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.*;

public class CassandraRepositoryGenerator implements RepositoryGenerator {
private final TypeMirror repositoryInterface;
Expand Down Expand Up @@ -123,10 +123,7 @@ private MethodSpec generate(ExecutableElement method, ExecutableType methodType,
if (CommonUtils.isVoid(((DeclaredType) returnType).getTypeArguments().get(0))) {
b.addStatement("return $T.from(_rrs).then()", CommonClassNames.flux);
} else {
if(resultMapperName == null) {
throw new IllegalStateException("Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
}

Objects.requireNonNull(resultMapperName, () -> "Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
b.addStatement("return $N.apply(_rrs)", resultMapperName);
}
b.endControlFlow().addCode(")\n");// flatMap Statement
Expand All @@ -144,10 +141,7 @@ private MethodSpec generate(ExecutableElement method, ExecutableType methodType,
b.beginControlFlow("try");
b.addStatement("var _rs = _session.execute(_s)");
if (returnType.getKind() != TypeKind.VOID) {
if(resultMapperName == null) {
throw new IllegalStateException("Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
}

Objects.requireNonNull(resultMapperName, () -> "Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
b.addStatement("var _result = $N.apply(_rs)", resultMapperName);
}
b.addStatement("_telemetry.close(null)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public CassandraTypesExtension(ProcessingEnvironment env) {
}
},
fd -> CodeBlock.builder()
.beginControlFlow("if (_row.isNull($S))", fd.columnName())
.beginControlFlow("if (_row.isNull(_idx_$L))", fd.fieldName())
.add(fd.nullable()
? CodeBlock.of("$N = null;\n", fd.fieldName())
: CodeBlock.of("throw new $T($S);\n", NullPointerException.class, "Result field %s is not nullable but row %s has null".formatted(fd.fieldName(), fd.columnName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.squareup.javapoet.*;
import reactor.core.publisher.Mono;
import ru.tinkoff.kora.annotation.processor.common.*;
import ru.tinkoff.kora.annotation.processor.common.CommonUtils;
import ru.tinkoff.kora.annotation.processor.common.FieldFactory;
import ru.tinkoff.kora.annotation.processor.common.MethodUtils;
import ru.tinkoff.kora.annotation.processor.common.Visitors;
import ru.tinkoff.kora.common.Tag;
import ru.tinkoff.kora.database.annotation.processor.DbUtils;
import ru.tinkoff.kora.database.annotation.processor.DbUtils.Mapper;
Expand Down Expand Up @@ -194,9 +197,7 @@ public MethodSpec generate(ExecutableElement method, ExecutableType methodType,
? CodeBlock.of("_result")
: CodeBlock.of("$T.requireNonNull(_result)", Objects.class);

if(resultMapperName == null) {
throw new IllegalStateException("Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
}
Objects.requireNonNull(resultMapperName, () -> "Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());

b.addCode("try (var _rs = _stmt.executeQuery()) {$>\n")
.addCode("var _result = $L.apply(_rs);\n", resultMapperName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.squareup.javapoet.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import ru.tinkoff.kora.annotation.processor.common.*;
import ru.tinkoff.kora.annotation.processor.common.CommonUtils;
import ru.tinkoff.kora.annotation.processor.common.FieldFactory;
import ru.tinkoff.kora.annotation.processor.common.TagUtils;
import ru.tinkoff.kora.annotation.processor.common.Visitors;
import ru.tinkoff.kora.common.Context;
import ru.tinkoff.kora.common.Tag;
import ru.tinkoff.kora.database.annotation.processor.DbUtils;
Expand All @@ -26,6 +29,7 @@
import javax.lang.model.util.Types;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public final class R2dbcRepositoryGenerator implements RepositoryGenerator {
Expand Down Expand Up @@ -121,16 +125,10 @@ private MethodSpec generate(ExecutableElement method, ExecutableType methodType,
if (returnType.toString().equals(DbUtils.UPDATE_COUNT.canonicalName())) {
b.addCode("return _flux.flatMap($T::getRowsUpdated).reduce(0L, Long::sum).map($T::new)", R2dbcTypes.RESULT, DbUtils.UPDATE_COUNT);
} else if (resultFluxMapper != null) {
if(resultMapperName == null) {
throw new IllegalStateException("Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
}

Objects.requireNonNull(resultMapperName, () -> "Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
b.addCode("return $L.apply(_flux)\n", resultMapperName);
} else if (rowMapper != null || !CommonUtils.isVoid(returnType)) {
if(resultMapperName == null) {
throw new IllegalStateException("Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
}

Objects.requireNonNull(resultMapperName, () -> "Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
b.addCode("return $L.apply(_flux)\n", resultMapperName);
} else {
b.addCode("return _flux.flatMap($T::getRowsUpdated).then()", R2dbcTypes.RESULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.squareup.javapoet.*;
import ru.tinkoff.kora.annotation.processor.common.CommonUtils;
import ru.tinkoff.kora.annotation.processor.common.FieldFactory;
import ru.tinkoff.kora.annotation.processor.common.ProcessingErrorException;
import ru.tinkoff.kora.annotation.processor.common.Visitors;
import ru.tinkoff.kora.common.Tag;
import ru.tinkoff.kora.database.annotation.processor.DbUtils;
Expand Down Expand Up @@ -115,10 +114,7 @@ private MethodSpec generate(ExecutableElement method, ExecutableType methodType,
} else if (returnType.toString().equals(DbUtils.UPDATE_COUNT.canonicalName())) {
resultMapper = CodeBlock.of("$T::extractUpdateCount", VertxTypes.ROW_SET_MAPPER);
} else {
if(resultMapperName == null) {
throw new IllegalStateException("Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
}

Objects.requireNonNull(resultMapperName, () -> "Illegal State occurred when expected to get result mapper, but got null in " + method.getEnclosingElement().getSimpleName() + "#" + method.getSimpleName());
resultMapper = CodeBlock.of("$N", resultMapperName);
}
if (returnType.getKind() != TypeKind.VOID) {
Expand Down

0 comments on commit f144958

Please sign in to comment.