Skip to content

Commit

Permalink
core: add accessor for bare method name in MethodDescriptor (#7339)
Browse files Browse the repository at this point in the history
Added getBareMethodName and extractBareMethodName for accessing the unqualified method name.
  • Loading branch information
pkern committed Sep 3, 2020
1 parent 3493347 commit 0701242
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
27 changes: 27 additions & 0 deletions api/src/main/java/io/grpc/MethodDescriptor.java
Expand Up @@ -262,6 +262,17 @@ public String getServiceName() {
return serviceName;
}

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

/**
* Parse a response payload from the given {@link InputStream}.
*
Expand Down Expand Up @@ -398,6 +409,22 @@ 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
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635")
public static String extractBareMethodName(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 getBareMethodName_extractsMethod() {
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
.setType(MethodType.UNARY)
.setFullMethodName("foo/bar")
.build();

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

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

assertNull(md.getBareMethodName());
}

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

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

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

0 comments on commit 0701242

Please sign in to comment.