mirror of https://github.com/grpc/grpc-java.git
core: add getService to MethodDescriptor
This commit is contained in:
parent
0c304b1863
commit
ce6358e57f
|
|
@ -41,6 +41,7 @@ public final class MethodDescriptor<ReqT, RespT> {
|
|||
|
||||
private final MethodType type;
|
||||
private final String fullMethodName;
|
||||
@Nullable private final String serviceName;
|
||||
private final Marshaller<ReqT> requestMarshaller;
|
||||
private final Marshaller<RespT> responseMarshaller;
|
||||
private final @Nullable Object schemaDescriptor;
|
||||
|
|
@ -226,6 +227,7 @@ public final class MethodDescriptor<ReqT, RespT> {
|
|||
|
||||
this.type = Preconditions.checkNotNull(type, "type");
|
||||
this.fullMethodName = Preconditions.checkNotNull(fullMethodName, "fullMethodName");
|
||||
this.serviceName = extractFullServiceName(fullMethodName);
|
||||
this.requestMarshaller = Preconditions.checkNotNull(requestMarshaller, "requestMarshaller");
|
||||
this.responseMarshaller = Preconditions.checkNotNull(responseMarshaller, "responseMarshaller");
|
||||
this.schemaDescriptor = schemaDescriptor;
|
||||
|
|
@ -254,6 +256,17 @@ public final class MethodDescriptor<ReqT, RespT> {
|
|||
return fullMethodName;
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenience method for {@code extractFullServiceName(getFullMethodName())}.
|
||||
*
|
||||
* @since 1.21.0
|
||||
*/
|
||||
@Nullable
|
||||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635")
|
||||
public String getServiceName() {
|
||||
return serviceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a response payload from the given {@link InputStream}.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ public final class ServerServiceDefinition {
|
|||
public <ReqT, RespT> Builder addMethod(ServerMethodDefinition<ReqT, RespT> def) {
|
||||
MethodDescriptor<ReqT, RespT> method = def.getMethodDescriptor();
|
||||
checkArgument(
|
||||
serviceName.equals(MethodDescriptor.extractFullServiceName(method.getFullMethodName())),
|
||||
serviceName.equals(method.getServiceName()),
|
||||
"Method name should be prefixed with service name and separated with '/'. "
|
||||
+ "Expected service name: '%s'. Actual fully qualifed method name: '%s'.",
|
||||
serviceName, method.getFullMethodName());
|
||||
|
|
|
|||
|
|
@ -110,8 +110,7 @@ public final class ServiceDescriptor {
|
|||
Set<String> allNames = new HashSet<>(methods.size());
|
||||
for (MethodDescriptor<?, ?> method : methods) {
|
||||
checkNotNull(method, "method");
|
||||
String methodServiceName =
|
||||
MethodDescriptor.extractFullServiceName(method.getFullMethodName());
|
||||
String methodServiceName = method.getServiceName();
|
||||
checkArgument(serviceName.equals(methodServiceName),
|
||||
"service names %s != %s", methodServiceName, serviceName);
|
||||
checkArgument(allNames.add(method.getFullMethodName()),
|
||||
|
|
|
|||
|
|
@ -20,9 +20,12 @@ import static junit.framework.TestCase.assertSame;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import io.grpc.MethodDescriptor.Marshaller;
|
||||
import io.grpc.MethodDescriptor.MethodType;
|
||||
import io.grpc.testing.TestMethodDescriptors;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
|
@ -135,6 +138,28 @@ public class MethodDescriptorTest {
|
|||
assertTrue(md4.isSampledToLocalTracing());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getServiceName_extractsService() {
|
||||
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
|
||||
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
|
||||
.setType(MethodType.UNARY)
|
||||
.setFullMethodName("foo/bar")
|
||||
.build();
|
||||
|
||||
assertEquals("foo", md.getServiceName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getServiceName_returnsNull() {
|
||||
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
|
||||
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
|
||||
.setType(MethodType.UNARY)
|
||||
.setFullMethodName("foo-bar")
|
||||
.build();
|
||||
|
||||
assertNull(md.getServiceName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBuilderTest() {
|
||||
MethodDescriptor<String, String> md1 = MethodDescriptor.<String, String>newBuilder()
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ public final class ClientAuthInterceptor implements ClientInterceptor {
|
|||
// Always use HTTPS, by definition.
|
||||
final String scheme = "https";
|
||||
final int defaultPort = 443;
|
||||
String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName());
|
||||
String path = "/" + method.getServiceName();
|
||||
URI uri;
|
||||
try {
|
||||
uri = new URI(scheme, authority, path, null, null);
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ final class GoogleAuthLibraryCallCredentials extends io.grpc.CallCredentials2 {
|
|||
// Always use HTTPS, by definition.
|
||||
final String scheme = "https";
|
||||
final int defaultPort = 443;
|
||||
String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName());
|
||||
String path = "/" + method.getServiceName();
|
||||
URI uri;
|
||||
try {
|
||||
uri = new URI(scheme, authority, path, null, null);
|
||||
|
|
|
|||
|
|
@ -195,7 +195,7 @@ final class ServiceConfigInterceptor implements ClientInterceptor {
|
|||
info = mcsc.getServiceMethodMap().get(method.getFullMethodName());
|
||||
}
|
||||
if (info == null && mcsc != null) {
|
||||
String serviceName = MethodDescriptor.extractFullServiceName(method.getFullMethodName());
|
||||
String serviceName = method.getServiceName();
|
||||
info = mcsc.getServiceMap().get(serviceName);
|
||||
}
|
||||
return info;
|
||||
|
|
|
|||
Loading…
Reference in New Issue