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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
return methodName; | ||
} | ||
|
||
/** | ||
* Parse a response payload from the given {@link InputStream}. | ||
* | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ExperimentalApi There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}. | ||
* | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.