diff --git a/generator/build.gradle.kts b/generator/build.gradle.kts index ed78005..1c26ab7 100644 --- a/generator/build.gradle.kts +++ b/generator/build.gradle.kts @@ -79,6 +79,7 @@ dependencies { implementation("com.google.protobuf:protobuf-java:3.21.9") implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.2") + implementation("one.util:streamex:0.8.2") implementation("org.apache.commons:commons-lang3:3.13.0") implementation("commons-io:commons-io:2.13.0") } diff --git a/generator/src/main/java/org/sudu/protogen/generator/field/FieldGenerator.java b/generator/src/main/java/org/sudu/protogen/generator/field/FieldGenerator.java index b01b295..66a8021 100644 --- a/generator/src/main/java/org/sudu/protogen/generator/field/FieldGenerator.java +++ b/generator/src/main/java/org/sudu/protogen/generator/field/FieldGenerator.java @@ -7,6 +7,9 @@ import org.sudu.protogen.generator.type.TypeModel; import org.sudu.protogen.utils.Poem; +import java.util.Collection; +import java.util.stream.Stream; + public class FieldGenerator { private final GenerationContext context; @@ -18,6 +21,12 @@ public FieldGenerator(GenerationContext context, Field field) { this.field = field; } + public static Stream generateSeveral(Collection fields, GenerationContext context) { + return fields.stream() + .map(field -> new FieldGenerator(context, field).generate()) + .filter(FieldProcessingResult::isNonVoid); + } + @NotNull public FieldProcessingResult generate() { if (field.isIgnored()) { diff --git a/generator/src/main/java/org/sudu/protogen/generator/server/AbstractServiceMethodGenerator.java b/generator/src/main/java/org/sudu/protogen/generator/server/AbstractServiceMethodGenerator.java index e347f95..d001e74 100644 --- a/generator/src/main/java/org/sudu/protogen/generator/server/AbstractServiceMethodGenerator.java +++ b/generator/src/main/java/org/sudu/protogen/generator/server/AbstractServiceMethodGenerator.java @@ -1,6 +1,7 @@ package org.sudu.protogen.generator.server; import com.squareup.javapoet.*; +import one.util.streamex.StreamEx; import org.jetbrains.annotations.Nullable; import org.sudu.protogen.descriptors.Method; import org.sudu.protogen.generator.GenerationContext; @@ -84,13 +85,9 @@ private Iterable generateMethodParameters() { ).filter(Objects::nonNull).toList(); } } - return Stream.concat( - method.getInputType().getFields().stream() - .map(f -> new FieldGenerator(context, f).generate()) - .filter(FieldProcessingResult::isNonVoid) - .map(FieldProcessingResult::field) - .map(Poem::fieldToParameter), - Stream.of(generateObserverParameter()) - ).filter(Objects::nonNull).toList(); + Stream unfoldedFields = FieldGenerator.generateSeveral(method.getInputType().getFields(), context) + .map(FieldProcessingResult::field) + .map(Poem::fieldToParameter); + return StreamEx.of(unfoldedFields).append(generateObserverParameter()).nonNull().toList(); } } diff --git a/generator/src/main/java/org/sudu/protogen/generator/server/OverriddenServiceMethodGenerator.java b/generator/src/main/java/org/sudu/protogen/generator/server/OverriddenServiceMethodGenerator.java index 751e91d..d82e71c 100644 --- a/generator/src/main/java/org/sudu/protogen/generator/server/OverriddenServiceMethodGenerator.java +++ b/generator/src/main/java/org/sudu/protogen/generator/server/OverriddenServiceMethodGenerator.java @@ -1,11 +1,11 @@ package org.sudu.protogen.generator.server; import com.squareup.javapoet.*; +import one.util.streamex.StreamEx; import org.jetbrains.annotations.Nullable; import org.sudu.protogen.descriptors.Method; import org.sudu.protogen.generator.GenerationContext; import org.sudu.protogen.generator.field.FieldGenerator; -import org.sudu.protogen.generator.field.FieldProcessingResult; import org.sudu.protogen.generator.message.FieldTransformerGenerator; import org.sudu.protogen.generator.type.TypeModel; import org.sudu.protogen.generator.type.UnfoldedType; @@ -13,7 +13,6 @@ import javax.lang.model.element.Modifier; import java.util.List; -import java.util.stream.Stream; /** * Overrides default stub method to pass request and response as domain objects or list of fields @@ -82,24 +81,22 @@ private CodeBlock generateAbstractMethodCallParams() { if (requestType != null && !method.doUnfoldRequest()) { return CodeBlock.of("$L", Poem.separatedSequence( - Stream.concat( - Stream.of(requestType.fromGrpcTransformer(CodeBlock.of("request"))), - generateResponseObserver().stream() - ).toList(), + StreamEx.of(requestType.fromGrpcTransformer(CodeBlock.of("request"))) + .append(generateResponseObserver()) + .toList(), ",\n" ) ); } // todo think about how to took out such logic because client does the same - List unfoldedRequestFields = method.getInputType().getFields().stream() - .map(field -> new FieldGenerator(context, field).generate()) - .filter(FieldProcessingResult::isNonVoid) + List unfoldedRequestFields = FieldGenerator.generateSeveral(method.getInputType().getFields(), context) .map(f -> new FieldTransformerGenerator(f.type(), f.original().getName(), f.isNullable()) - .fromGrpc("request")) - .toList(); - return CodeBlock.of("$L", - Poem.separatedSequence(Stream.concat(unfoldedRequestFields.stream(), generateResponseObserver().stream()).toList(), ",\n") - ); + .fromGrpc("request") + ).toList(); + return CodeBlock.of("$L", Poem.separatedSequence( + StreamEx.of(unfoldedRequestFields).append(generateResponseObserver()).toList(), + ",\n" + )); } /** @@ -115,12 +112,12 @@ private List generateResponseObserver() { } if (method.doUnfoldResponse()) { var field = method.unfoldedResponseField(); - TypeModel type = context.processType(field); - return List.of(CodeBlock.of( - "$L", new AnonymousStreamObserverGenerator( - new UnfoldedType(type, field.getName(), method.getOutputType().getProtobufTypeName()) - ).generate() - )); + TypeModel type = new UnfoldedType( + context.processType(field), + field.getName(), + method.getOutputType().getProtobufTypeName() + ); + return List.of(CodeBlock.of("$L", new AnonymousStreamObserverGenerator(type).generate())); } return List.of(CodeBlock.of("responseObserver")); } @@ -131,7 +128,7 @@ private List generateResponseObserver() { * {@code GrpcSomeRequest request, StreamObserver response} * */ - private Iterable generateMethodParameters() { + private List generateMethodParameters() { TypeName requestType = method.getInputType().getProtobufTypeName(); TypeName responseType = method.getOutputType().getProtobufTypeName(); ParameterizedTypeName responseObserverType = ParameterizedTypeName.get(