Search in sources :

Example 1 with OperationResponseMapping

use of com.google.cloud.OperationResponseMapping in project gapic-generator-java by googleapis.

the class Parser method parseMessages.

private static Map<String, Message> parseMessages(Descriptor messageDescriptor, Set<ResourceReference> outputResourceReferencesSeen, List<String> outerNestedTypes) {
    Map<String, Message> messages = new HashMap<>();
    String messageName = messageDescriptor.getName();
    if (!messageDescriptor.getNestedTypes().isEmpty()) {
        for (Descriptor nestedMessage : messageDescriptor.getNestedTypes()) {
            if (isMapType(nestedMessage)) {
                continue;
            }
            List<String> currentNestedTypes = new ArrayList<>(outerNestedTypes);
            currentNestedTypes.add(messageName);
            messages.putAll(parseMessages(nestedMessage, outputResourceReferencesSeen, currentNestedTypes));
        }
    }
    TypeNode messageType = TypeParser.parseType(messageDescriptor);
    List<FieldDescriptor> fields = messageDescriptor.getFields();
    HashMap<String, String> operationRequestFields = new HashMap<String, String>();
    BiMap<String, String> operationResponseFields = HashBiMap.create();
    OperationResponse.Builder operationResponse = null;
    for (FieldDescriptor fd : fields) {
        if (fd.getOptions().hasExtension(ExtendedOperationsProto.operationRequestField)) {
            String orf = fd.getOptions().getExtension(ExtendedOperationsProto.operationRequestField);
            operationRequestFields.put(orf, fd.getName());
        }
        if (fd.getOptions().hasExtension(ExtendedOperationsProto.operationResponseField)) {
            String orf = fd.getOptions().getExtension(ExtendedOperationsProto.operationResponseField);
            operationResponseFields.put(orf, fd.getName());
        }
        if (fd.getOptions().hasExtension(ExtendedOperationsProto.operationField)) {
            OperationResponseMapping orm = fd.getOptions().getExtension(ExtendedOperationsProto.operationField);
            if (operationResponse == null) {
                operationResponse = OperationResponse.builder();
            }
            if (orm.equals(OperationResponseMapping.NAME)) {
                operationResponse.setNameFieldName(fd.getName());
            } else if (orm.equals(OperationResponseMapping.STATUS)) {
                operationResponse.setStatusFieldName(fd.getName());
                operationResponse.setStatusFieldTypeName(fd.toProto().getTypeName());
            } else if (orm.equals(OperationResponseMapping.ERROR_CODE)) {
                operationResponse.setErrorCodeFieldName(fd.getName());
            } else if (orm.equals(OperationResponseMapping.ERROR_MESSAGE)) {
                operationResponse.setErrorMessageFieldName(fd.getName());
            }
        }
    }
    messages.put(messageType.reference().fullName(), Message.builder().setType(messageType).setName(messageName).setFullProtoName(messageDescriptor.getFullName()).setFields(parseFields(messageDescriptor, outputResourceReferencesSeen)).setOuterNestedTypes(outerNestedTypes).setOperationRequestFields(operationRequestFields).setOperationResponseFields(operationResponseFields).setOperationResponse(operationResponse != null ? operationResponse.build() : null).build());
    return messages;
}
Also used : Message(com.google.api.generator.gapic.model.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OperationResponseMapping(com.google.cloud.OperationResponseMapping) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) ResourceDescriptor(com.google.api.ResourceDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) Descriptor(com.google.protobuf.Descriptors.Descriptor) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) TypeNode(com.google.api.generator.engine.ast.TypeNode) OperationResponse(com.google.api.generator.gapic.model.OperationResponse)

Aggregations

ResourceDescriptor (com.google.api.ResourceDescriptor)1 TypeNode (com.google.api.generator.engine.ast.TypeNode)1 Message (com.google.api.generator.gapic.model.Message)1 OperationResponse (com.google.api.generator.gapic.model.OperationResponse)1 OperationResponseMapping (com.google.cloud.OperationResponseMapping)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)1 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)1 MethodDescriptor (com.google.protobuf.Descriptors.MethodDescriptor)1 ServiceDescriptor (com.google.protobuf.Descriptors.ServiceDescriptor)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1