Search in sources :

Example 1 with OperationResponse

use of com.google.api.generator.gapic.model.OperationResponse 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)

Example 2 with OperationResponse

use of com.google.api.generator.gapic.model.OperationResponse in project gapic-generator-java by googleapis.

the class HttpJsonServiceStubClassComposer method setOperationSnapshotFactoryExpr.

private List<Expr> setOperationSnapshotFactoryExpr(Method protoMethod, Map<String, Message> messageTypes) {
    // Generate input variables for create()
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(protoMethod.inputType()).setName("request").build());
    VariableExpr responseVarExpr = VariableExpr.withVariable(Variable.builder().setType(protoMethod.outputType()).setName("response").build());
    MethodInvocationExpr buildExpr;
    List<Statement> createBody = new ArrayList<>(4);
    TypeNode httpJsonOperationSnapshotType = FIXED_REST_TYPESTORE.get(HttpJsonOperationSnapshot.class.getSimpleName());
    TypeNode operationSnapshotType = FIXED_TYPESTORE.get(OperationSnapshot.class.getSimpleName());
    Message inputOperationMessage = messageTypes.get(protoMethod.inputType().reference().fullName());
    Message outputOperationMessage = messageTypes.get(protoMethod.outputType().reference().fullName());
    OperationResponse operationResponse = outputOperationMessage.operationResponse();
    if (operationResponse == null) {
        // AIP-151 LRO
        // HttpJsonOperationSnapshot.create(response)
        buildExpr = MethodInvocationExpr.builder().setStaticReferenceType(httpJsonOperationSnapshotType).setMethodName("create").setArguments(responseVarExpr).setReturnType(operationSnapshotType).build();
    } else {
        BiFunction<String, List<Expr>, Function<MethodInvocationExpr, MethodInvocationExpr>> methodMaker = getMethodMaker();
        // Generate opName
        TypeNode stringBuilderType = TypeNode.withReference(ConcreteReference.withClazz(StringBuilder.class));
        VariableExpr opNameVarExpr = VariableExpr.withVariable(Variable.builder().setType(stringBuilderType).setName("opName").build());
        MethodInvocationExpr getId = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName(getMethodFormat(operationResponse.nameFieldName())).build();
        Expr opNameObjectExpr = NewObjectExpr.builder().setType(stringBuilderType).setArguments(getId).build();
        AssignmentExpr opNameAssignExpr = AssignmentExpr.builder().setVariableExpr(opNameVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(opNameObjectExpr).build();
        createBody.add(ExprStatement.withExpr(opNameAssignExpr));
        // Generate compound operation name
        if (!protoMethod.isOperationPollingMethod()) {
            // TODO: Change to ordered map
            Map<String, String> requestFields = inputOperationMessage.operationRequestFields();
            List<String> fieldAnnotationNames = new ArrayList<>(requestFields.keySet());
            Collections.sort(fieldAnnotationNames);
            for (String fieldName : fieldAnnotationNames) {
                createBody.add(appendField(opNameVarExpr, requestVarExpr, requestFields.get(fieldName)));
            }
        }
        // Generate check for status == done
        MethodInvocationExpr getStatusExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName(getMethodFormat(operationResponse.statusFieldName())).build();
        String statusTypeName = operationResponse.statusFieldTypeName();
        String statusClassName = statusTypeName.substring(statusTypeName.lastIndexOf('.') + 1);
        TypeNode opType = protoMethod.hasLro() ? protoMethod.lro().responseType() : protoMethod.outputType();
        TypeNode statusType = TypeNode.withReference(VaporReference.builder().setName(statusClassName).setPakkage(opType.reference().fullName()).setIsStaticImport(false).build());
        VariableExpr statusDoneExpr = VariableExpr.builder().setVariable(Variable.builder().setName("DONE").setType(TypeNode.INT).build()).setStaticReferenceType(statusType).build();
        MethodInvocationExpr statusEqualsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(statusDoneExpr).setMethodName("equals").setArguments(getStatusExpr).build();
        // Generate return statement
        // Generate getter methods from annotations
        MethodInvocationExpr opNameToStringExpr = MethodInvocationExpr.builder().setExprReferenceExpr(opNameVarExpr).setMethodName("toString").build();
        MethodInvocationExpr getHttpErrorStatusCodeExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName(getMethodFormat(operationResponse.errorCodeFieldName())).build();
        MethodInvocationExpr getHttpErrorMessageExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName(getMethodFormat(operationResponse.errorMessageFieldName())).build();
        MethodInvocationExpr newBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(httpJsonOperationSnapshotType).setMethodName("newBuilder").build();
        newBuilderExpr = methodMaker.apply("setName", Collections.singletonList(opNameToStringExpr)).apply(newBuilderExpr);
        newBuilderExpr = methodMaker.apply("setMetadata", Collections.singletonList(responseVarExpr)).apply(newBuilderExpr);
        newBuilderExpr = methodMaker.apply("setDone", Collections.singletonList(statusEqualsExpr)).apply(newBuilderExpr);
        newBuilderExpr = methodMaker.apply("setResponse", Collections.singletonList(responseVarExpr)).apply(newBuilderExpr);
        newBuilderExpr = methodMaker.apply("setError", Arrays.asList(getHttpErrorStatusCodeExpr, getHttpErrorMessageExpr)).apply(newBuilderExpr);
        buildExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderExpr).setMethodName("build").setReturnType(operationSnapshotType).build();
    }
    // Generate lambda anonymous class
    return Collections.singletonList(LambdaExpr.builder().setArguments(requestVarExpr.toBuilder().setIsDecl(true).build(), responseVarExpr.toBuilder().setIsDecl(true).build()).setBody(createBody).setReturnExpr(buildExpr).build());
}
Also used : Message(com.google.api.generator.gapic.model.Message) IfStatement(com.google.api.generator.engine.ast.IfStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ArrayList(java.util.ArrayList) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) HttpJsonOperationSnapshot(com.google.api.gax.httpjson.HttpJsonOperationSnapshot) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TypeNode(com.google.api.generator.engine.ast.TypeNode) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) HttpJsonOperationSnapshot(com.google.api.gax.httpjson.HttpJsonOperationSnapshot) OperationResponse(com.google.api.generator.gapic.model.OperationResponse)

Aggregations

TypeNode (com.google.api.generator.engine.ast.TypeNode)2 Message (com.google.api.generator.gapic.model.Message)2 OperationResponse (com.google.api.generator.gapic.model.OperationResponse)2 ArrayList (java.util.ArrayList)2 ResourceDescriptor (com.google.api.ResourceDescriptor)1 HttpJsonOperationSnapshot (com.google.api.gax.httpjson.HttpJsonOperationSnapshot)1 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)1 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)1 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)1 Expr (com.google.api.generator.engine.ast.Expr)1 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)1 IfStatement (com.google.api.generator.engine.ast.IfStatement)1 LambdaExpr (com.google.api.generator.engine.ast.LambdaExpr)1 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)1 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)1 Statement (com.google.api.generator.engine.ast.Statement)1 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)1 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)1 OperationResponseMapping (com.google.cloud.OperationResponseMapping)1 ImmutableList (com.google.common.collect.ImmutableList)1