Skip to content

Commit

Permalink
Beautify concatenation using StreamEx
Browse files Browse the repository at this point in the history
  • Loading branch information
Duzhinsky committed Sep 19, 2023
1 parent bcad8c1 commit 38e6166
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
1 change: 1 addition & 0 deletions generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +21,12 @@ public FieldGenerator(GenerationContext context, Field field) {
this.field = field;
}

public static Stream<FieldProcessingResult> generateSeveral(Collection<Field> fields, GenerationContext context) {
return fields.stream()
.map(field -> new FieldGenerator(context, field).generate())
.filter(FieldProcessingResult::isNonVoid);
}

@NotNull
public FieldProcessingResult generate() {
if (field.isIgnored()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -84,13 +85,9 @@ private Iterable<ParameterSpec> 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<ParameterSpec> unfoldedFields = FieldGenerator.generateSeveral(method.getInputType().getFields(), context)
.map(FieldProcessingResult::field)
.map(Poem::fieldToParameter);
return StreamEx.of(unfoldedFields).append(generateObserverParameter()).nonNull().toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
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;
import org.sudu.protogen.utils.Poem;

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
Expand Down Expand Up @@ -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<CodeBlock> unfoldedRequestFields = method.getInputType().getFields().stream()
.map(field -> new FieldGenerator(context, field).generate())
.filter(FieldProcessingResult::isNonVoid)
List<CodeBlock> 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"
));
}

/**
Expand All @@ -115,12 +112,12 @@ private List<CodeBlock> 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"));
}
Expand All @@ -131,7 +128,7 @@ private List<CodeBlock> generateResponseObserver() {
* {@code GrpcSomeRequest request, StreamObserver<GrpcSomeResponse> response}
* </pre>
*/
private Iterable<ParameterSpec> generateMethodParameters() {
private List<ParameterSpec> generateMethodParameters() {
TypeName requestType = method.getInputType().getProtobufTypeName();
TypeName responseType = method.getOutputType().getProtobufTypeName();
ParameterizedTypeName responseObserverType = ParameterizedTypeName.get(
Expand Down

0 comments on commit 38e6166

Please sign in to comment.