Skip to content

Commit

Permalink
if_not_found option as a replacement for "nullable"
Browse files Browse the repository at this point in the history
  • Loading branch information
Duzhinsky committed Sep 22, 2023
1 parent fdac51d commit bc784ec
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public boolean isOutputStreaming() {
return methodDescriptor.isServerStreaming();
}

public final boolean isNullable() {
return getNullableOption().orElse(false);
public final protogen.Options.IfNotFound ifNotFoundBehavior() {
return Options.wrapExtension(methodDescriptor.getOptions(), protogen.Options.ifNotFound)
.orElse(protogen.Options.IfNotFound.IGNORE);
}

public final String generatedName() {
Expand Down Expand Up @@ -83,10 +84,6 @@ protected Optional<Boolean> getUnfoldRequestOption() {
return Options.wrapExtension(methodDescriptor.getOptions(), protogen.Options.unfoldRequest);
}

protected Optional<Boolean> getNullableOption() {
return Options.wrapExtension(methodDescriptor.getOptions(), protogen.Options.nullable);
}

public RepeatedContainer getStreamToContainer() {
return Options.wrapExtension(methodDescriptor.getOptions(), protogen.Options.streamToContainer)
.map(RepeatedContainer::fromGrpc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.sudu.protogen.generator.message.ToGrpcMethodGenerator;
import org.sudu.protogen.generator.type.TypeModel;
import org.sudu.protogen.utils.Poem;
import protogen.Options;

import java.util.List;

Expand Down Expand Up @@ -37,7 +38,7 @@ public MethodSpec generate() {
.addParameters(params)
.addCode(body(params))
.addAnnotation(
method.isNullable()
method.ifNotFoundBehavior() == Options.IfNotFound.NULLIFY
? context.configuration().nullableAnnotationClass()
: context.configuration().nonnullAnnotationClass()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.sudu.protogen.generator.GenerationContext;
import org.sudu.protogen.generator.type.RepeatedType;
import org.sudu.protogen.generator.type.TypeModel;
import protogen.Options;

import javax.lang.model.element.Modifier;
import java.util.List;
Expand Down Expand Up @@ -35,7 +36,7 @@ public MethodSpec generate() {
.addCode(body())
.returns(returnType.getTypeName())
.addAnnotation(
method.isNullable()
method.ifNotFoundBehavior() == Options.IfNotFound.NULLIFY
? context.configuration().nullableAnnotationClass()
: context.configuration().nonnullAnnotationClass()
)
Expand All @@ -62,8 +63,10 @@ private CodeBlock body() {
returnExpr = returnType.fromGrpcTransformer(returnExpr);
}

if (method.isNullable()) {
returnExpr = CodeBlock.of("$T.nullifyIfNotFound(() -> $L)", BaseClientUtils.class, returnExpr);
switch (method.ifNotFoundBehavior()) {
case NULLIFY ->
returnExpr = CodeBlock.of("$T.nullifyIfNotFound(() -> $L)", BaseClientUtils.class, returnExpr);
case EMPTY -> returnExpr = CodeBlock.of("$T.emptyIfNotFound(() -> $L)", BaseClientUtils.class, returnExpr);
}

if (returnType.getTypeName() != TypeName.VOID) {
Expand Down
10 changes: 8 additions & 2 deletions options/src/main/proto/protogen/options.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ enum AccessModifier {
PRIVATE = 2;
}

enum IfNotFound {
IGNORE = 0;
NULLIFY = 1;
EMPTY = 2;
}

extend google.protobuf.FileOptions {
// Asks protogen to generate the whole content of the file excluding *Request/*Response messages
bool enable = 5000;
Expand Down Expand Up @@ -149,7 +155,7 @@ extend google.protobuf.MethodOptions {
// If enabled, applies field unfolding for request parameters even if input is a domain object
bool unfold_request = 5502;
// Annotates method with @Nullable and wraps response into nullifyIfNotFound method of BaseGrpcClient
bool nullable = 5503;
IfNotFound if_not_found = 5503;
// If method is output-streaming, collects its output into a specified container using spliterator
RepeatedContainer stream_to_container = 5504;
/*
Expand All @@ -161,7 +167,7 @@ extend google.protobuf.MethodOptions {
* }
*/
string method_name = 5505;
// Allows to make your method public/proteceted/private
// Allows to make your method public/protected/private
AccessModifier access_modifier = 5506;

}
2 changes: 1 addition & 1 deletion tests/src/test/proto/client.proto
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ service SomeService {
rpc commonRetList(google.protobuf.Empty) returns (GrpcListResponse);
rpc commonMulti(GrpcMultiFieldRequest) returns (GrpcDomain);
rpc commonMultiNullable(GrpcMultiFieldRequest) returns (GrpcDomain) {
option (.protogen.nullable) = true;
option (.protogen.if_not_found) = NULLIFY;
}
rpc commonUnfolded(GrpcMultiFieldRequest) returns (GrpcDomainId);
rpc commonOneField(GrpcMultiFieldRequest) returns (GrpcOneFieldResponse);
Expand Down

0 comments on commit bc784ec

Please sign in to comment.