use of com.google.protobuf.DescriptorProtos.MethodOptions in project gapic-generator-java by googleapis.
the class HttpRuleParser method parse.
public static HttpBindings parse(MethodDescriptor protoMethod, Message inputMessage, Map<String, Message> messageTypes) {
MethodOptions methodOptions = protoMethod.getOptions();
if (!methodOptions.hasExtension(AnnotationsProto.http)) {
return null;
}
HttpRule httpRule = methodOptions.getExtension(AnnotationsProto.http);
// Body validation.
if (!Strings.isNullOrEmpty(httpRule.getBody()) && !httpRule.getBody().equals(ASTERISK)) {
checkHttpFieldIsValid(httpRule.getBody(), inputMessage, true);
}
return parseHttpRuleHelper(httpRule, Optional.of(inputMessage), messageTypes);
}
use of com.google.protobuf.DescriptorProtos.MethodOptions in project yamcs by yamcs.
the class ProtobufRegistry method importDefinitions.
public void importDefinitions(InputStream in) throws IOException {
if (in == null) {
throw new NullPointerException("input stream cannot be null");
}
FileDescriptorSet proto = FileDescriptorSet.parseFrom(in, extensionRegistry);
// Index all messages by fully-qualified protobuf name
for (FileDescriptorProto file : proto.getFileList()) {
scanComments(file);
String javaPackage = file.getOptions().getJavaPackage();
javaPackages.put(file.getName(), javaPackage);
for (DescriptorProto messageType : file.getMessageTypeList()) {
String qname = file.getPackage() + "." + messageType.getName();
messageTypes.put(qname, messageType);
}
}
// Index RPCs
for (FileDescriptorProto file : proto.getFileList()) {
for (ServiceDescriptorProto service : file.getServiceList()) {
for (MethodDescriptorProto method : service.getMethodList()) {
MethodOptions options = method.getOptions();
String serviceName = service.getName();
String methodName = method.getName();
DescriptorProto inputType = messageTypes.get(method.getInputType().substring(1));
DescriptorProto outputType = messageTypes.get(method.getOutputType().substring(1));
if (options.hasExtension(AnnotationsProto.route)) {
HttpRoute route = options.getExtension(AnnotationsProto.route);
RpcDescriptor descriptor = new RpcDescriptor(serviceName, methodName, inputType, outputType, route);
String qname = String.join(".", file.getPackage(), serviceName, methodName);
rpcs.put(qname, descriptor);
} else if (options.hasExtension(AnnotationsProto.websocket)) {
WebSocketTopic topic = options.getExtension(AnnotationsProto.websocket);
RpcDescriptor descriptor = new RpcDescriptor(serviceName, methodName, inputType, outputType, topic);
String qname = String.join(".", file.getPackage(), serviceName, methodName);
rpcs.put(qname, descriptor);
}
}
}
}
}
use of com.google.protobuf.DescriptorProtos.MethodOptions in project gapic-generator-java by googleapis.
the class Parser method parseLro.
@VisibleForTesting
static LongrunningOperation parseLro(String servicePackage, MethodDescriptor methodDescriptor, Map<String, Message> messageTypes) {
MethodOptions methodOptions = methodDescriptor.getOptions();
TypeNode operationServiceStubType = null;
String responseTypeName = null;
String metadataTypeName = null;
if (methodOptions.hasExtension(OperationsProto.operationInfo)) {
OperationInfo lroInfo = methodDescriptor.getOptions().getExtension(OperationsProto.operationInfo);
responseTypeName = lroInfo.getResponseType();
metadataTypeName = lroInfo.getMetadataType();
}
if (methodOptions.hasExtension(ExtendedOperationsProto.operationService)) {
// TODO: support full package name for operations_service annotation value
String opServiceName = methodOptions.getExtension(ExtendedOperationsProto.operationService);
operationServiceStubType = TypeNode.withReference(VaporReference.builder().setName(opServiceName + "Stub").setPakkage(servicePackage + ".stub").build());
if (responseTypeName == null) {
responseTypeName = methodDescriptor.getOutputType().getFullName();
}
if (metadataTypeName == null) {
metadataTypeName = methodDescriptor.getOutputType().getFullName();
}
}
if (responseTypeName == null || metadataTypeName == null) {
return null;
}
Message responseMessage = null;
Message metadataMessage = null;
int lastDotIndex = responseTypeName.lastIndexOf('.');
boolean isResponseTypeNameShortOnly = lastDotIndex < 0;
String responseTypeShortName = lastDotIndex >= 0 ? responseTypeName.substring(lastDotIndex + 1) : responseTypeName;
lastDotIndex = metadataTypeName.lastIndexOf('.');
boolean isMetadataTypeNameShortOnly = lastDotIndex < 0;
String metadataTypeShortName = lastDotIndex >= 0 ? metadataTypeName.substring(lastDotIndex + 1) : metadataTypeName;
// The messageTypes map keys to the Java fully-qualified name.
for (Map.Entry<String, Message> messageEntry : messageTypes.entrySet()) {
String messageKey = messageEntry.getKey();
int messageLastDotIndex = messageEntry.getKey().lastIndexOf('.');
String messageShortName = messageLastDotIndex >= 0 ? messageKey.substring(messageLastDotIndex + 1) : messageKey;
if (responseMessage == null) {
if (isResponseTypeNameShortOnly && responseTypeName.equals(messageShortName)) {
responseMessage = messageEntry.getValue();
} else if (!isResponseTypeNameShortOnly && responseTypeShortName.equals(messageShortName)) {
// Ensure that the full proto name matches.
Message candidateMessage = messageEntry.getValue();
if (candidateMessage.fullProtoName().equals(responseTypeName)) {
responseMessage = candidateMessage;
}
}
}
if (metadataMessage == null) {
if (isMetadataTypeNameShortOnly && metadataTypeName.equals(messageShortName)) {
metadataMessage = messageEntry.getValue();
} else if (!isMetadataTypeNameShortOnly && metadataTypeShortName.equals(messageShortName)) {
// Ensure that the full proto name matches.
Message candidateMessage = messageEntry.getValue();
if (candidateMessage.fullProtoName().equals(metadataTypeName)) {
metadataMessage = candidateMessage;
}
}
}
}
Preconditions.checkNotNull(responseMessage, String.format("LRO response message %s not found on method %s", responseTypeName, methodDescriptor.getName()));
// TODO(miraleung): Check that the packages are equal if those strings are not empty.
Preconditions.checkNotNull(metadataMessage, String.format("LRO metadata message %s not found in method %s", metadataTypeName, methodDescriptor.getName()));
return LongrunningOperation.builder().setResponseType(responseMessage.type()).setMetadataType(metadataMessage.type()).setOperationServiceStubType(operationServiceStubType).build();
}
use of com.google.protobuf.DescriptorProtos.MethodOptions in project gapic-generator-java by googleapis.
the class RoutingRuleParser method parse.
public static RoutingHeaderRule parse(MethodDescriptor protoMethod, Message inputMessage, Map<String, Message> messageTypes) {
MethodOptions methodOptions = protoMethod.getOptions();
if (!methodOptions.hasExtension(RoutingProto.routing)) {
return null;
}
RoutingHeaderRule.Builder routingHeaderRuleBuilder = RoutingHeaderRule.builder();
RoutingRule routingRule = methodOptions.getExtension(RoutingProto.routing);
for (RoutingParameter routingParameter : routingRule.getRoutingParametersList()) {
String pathTemplate = routingParameter.getPathTemplate();
String fieldName = routingParameter.getField();
// validate if field exist in Message or nested Messages and the type of leaf level field
inputMessage.validateField(fieldName, messageTypes, TypeNode.STRING);
String key;
if (Strings.isNullOrEmpty(pathTemplate)) {
key = fieldName;
pathTemplate = String.format("{%s=**}", key);
} else {
Set<String> namedSegments = PatternParser.getPattenBindings(pathTemplate);
Preconditions.checkArgument(namedSegments.size() == 1, String.format("There needs to be one and only one named segment in path template %s", pathTemplate));
key = namedSegments.iterator().next();
}
RoutingHeaderParam routingHeaderParam = RoutingHeaderParam.create(fieldName, key, pathTemplate);
routingHeaderRuleBuilder.addParam(routingHeaderParam);
}
return routingHeaderRuleBuilder.build();
}
Aggregations