Skip to content

Commit

Permalink
core: add getMethodName to MethodDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
pkern committed Aug 19, 2020
1 parent a91acec commit c1c2ac1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
28 changes: 28 additions & 0 deletions api/src/main/java/io/grpc/MethodDescriptor.java
Expand Up @@ -42,6 +42,7 @@ public final class MethodDescriptor<ReqT, RespT> {
private final MethodType type;
private final String fullMethodName;
@Nullable private final String serviceName;
@Nullable private final String methodName;
private final Marshaller<ReqT> requestMarshaller;
private final Marshaller<RespT> responseMarshaller;
private final @Nullable Object schemaDescriptor;
Expand Down Expand Up @@ -225,6 +226,7 @@ private MethodDescriptor(
this.type = Preconditions.checkNotNull(type, "type");
this.fullMethodName = Preconditions.checkNotNull(fullMethodName, "fullMethodName");
this.serviceName = extractFullServiceName(fullMethodName);
this.methodName = extractMethodName(fullMethodName);
this.requestMarshaller = Preconditions.checkNotNull(requestMarshaller, "requestMarshaller");
this.responseMarshaller = Preconditions.checkNotNull(responseMarshaller, "responseMarshaller");
this.schemaDescriptor = schemaDescriptor;
Expand Down Expand Up @@ -262,6 +264,17 @@ public String getServiceName() {
return serviceName;
}

/**
* A convenience method for {@code extractMethodName(getFullMethodName())}.
*
* @since 1.32.0
*/
@Nullable
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635")
public String getMethodName() {
return methodName;
}

/**
* Parse a response payload from the given {@link InputStream}.
*
Expand Down Expand Up @@ -398,6 +411,21 @@ public static String extractFullServiceName(String fullMethodName) {
return fullMethodName.substring(0, index);
}

/**
* Extract the method name out of a fully qualified method name. May return {@code null}
* if the input is malformed, but you cannot rely on it for the validity of the input.
*
* @since 1.32.0
*/
@Nullable
public static String extractMethodName(String fullMethodName) {
int index = checkNotNull(fullMethodName, "fullMethodName").lastIndexOf('/');
if (index == -1) {
return null;
}
return fullMethodName.substring(index + 1);
}

/**
* Creates a new builder for a {@link MethodDescriptor}.
*
Expand Down
33 changes: 33 additions & 0 deletions api/src/test/java/io/grpc/MethodDescriptorTest.java
Expand Up @@ -177,6 +177,39 @@ public void getServiceName_returnsNull() {
assertNull(md.getServiceName());
}

@Test
public void getMethodName_extractsMethod() {
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
.setType(MethodType.UNARY)
.setFullMethodName("foo/bar")
.build();

assertEquals("bar", md.getMethodName());
}

@Test
public void getMethodName_returnsNull() {
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
.setType(MethodType.UNARY)
.setFullMethodName("foo-bar")
.build();

assertNull(md.getMethodName());
}

@Test
public void getMethodName_returnsEmptyStringWithMethodMissing() {
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
.setType(MethodType.UNARY)
.setFullMethodName("foo/")
.build();

assertTrue(md.getMethodName().isEmpty());
}

@Test
public void toBuilderTest() {
MethodDescriptor<String, String> md1 = MethodDescriptor.<String, String>newBuilder()
Expand Down

0 comments on commit c1c2ac1

Please sign in to comment.