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 1 commit
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
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should pre-compute this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really matter? This should be a fairly cheap operation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's so cheap we shouldn't mind doing it every time. Really, I don't think we want the startup initialization overhead and memory usage for something that may never be used (consider Android for example).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern is that then every caller will need to cache it, but alas. Done.

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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add something to the name to make users think whether they want the full name or this one. "simple" or "bare" or similar. Ditto for extract method.

I'm not sure if we have any such term today, as method name is normally fully qualified. Maybe another language has such a thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not aware of another language having solved this properly. The naming confusion is very real. Generally the full(y qualified) method name gets its own name (also here: getFullMethodName()) and the other one is just MethodName. But from the options I picked Bare for now. Let me know what you think.

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExperimentalApi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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