Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: add accessor for bare method name in MethodDescriptor #7339

Merged
merged 3 commits into from Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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