Search in sources :

Example 1 with HttpBinding

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

the class GrpcServiceStubClassComposer method createRequestParamsExtractorBodyForHttpBindings.

private void createRequestParamsExtractorBodyForHttpBindings(Method method, VariableExpr requestVarExpr, List<Statement> bodyStatements, MethodInvocationExpr.Builder returnExprBuilder) {
    TypeNode paramsVarType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(TypeNode.STRING.reference(), TypeNode.STRING.reference()).build());
    VariableExpr paramsVarExpr = VariableExpr.withVariable(Variable.builder().setName("params").setType(paramsVarType).build());
    Expr paramsAssignExpr = AssignmentExpr.builder().setVariableExpr(paramsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(paramsVarType).build()).build();
    bodyStatements.add(ExprStatement.withExpr(paramsAssignExpr));
    for (HttpBinding httpBindingFieldBinding : method.httpBindings().pathParameters()) {
        MethodInvocationExpr requestBuilderExpr = createRequestFieldGetterExpr(requestVarExpr, httpBindingFieldBinding.name());
        Expr valueOfExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.STRING).setMethodName("valueOf").setArguments(requestBuilderExpr).build();
        Expr paramsPutExpr = MethodInvocationExpr.builder().setExprReferenceExpr(paramsVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(StringObjectValue.withValue(httpBindingFieldBinding.name())), valueOfExpr).build();
        bodyStatements.add(ExprStatement.withExpr(paramsPutExpr));
    }
    returnExprBuilder.setExprReferenceExpr(paramsVarExpr).setMethodName("build");
}
Also used : EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Expr(com.google.api.generator.engine.ast.Expr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) 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) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) RequestParamsBuilder(com.google.api.gax.rpc.RequestParamsBuilder) HttpBinding(com.google.api.generator.gapic.model.HttpBindings.HttpBinding) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 2 with HttpBinding

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

the class HttpJsonServiceStubClassComposer method createBodyFieldsExtractorClassInstance.

private Expr createBodyFieldsExtractorClassInstance(Method method, TypeNode extractorReturnType, Set<HttpBinding> httpBindingFieldNames, String serializerMethodName, boolean asteriskBody) {
    List<Statement> bodyStatements = new ArrayList<>();
    Expr returnExpr = null;
    Expr serializerExpr = MethodInvocationExpr.builder().setMethodName("create").setStaticReferenceType(FIXED_REST_TYPESTORE.get(ProtoRestSerializer.class.getSimpleName())).build();
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
    Expr bodyRequestExpr = requestVarExpr;
    String requestMethodPrefix = "get";
    String bodyParamName = null;
    if (asteriskBody) {
        bodyRequestExpr = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr).setMethodName("toBuilder").build();
        // In case of `body: "*"` case we send the whole request message as a body, minus the fields
        // in the path, therefore the "clear" prefix here.
        requestMethodPrefix = "clear";
    }
    Expr prevExpr = bodyRequestExpr;
    for (HttpBinding httpBindingFieldName : httpBindingFieldNames) {
        // Handle foo.bar cases by descending into the subfields.
        MethodInvocationExpr.Builder requestFieldMethodExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(prevExpr);
        bodyParamName = JavaStyle.toLowerCamelCase(httpBindingFieldName.name());
        String[] descendantFields = httpBindingFieldName.name().split("\\.");
        if (asteriskBody && descendantFields.length > 1) {
            // well-defined case, and it is generally safer to send more than less in such case.
            continue;
        }
        for (int i = 0; i < descendantFields.length; i++) {
            String currFieldName = descendantFields[i];
            String bindingFieldMethodName = String.format("%s%s", requestMethodPrefix, JavaStyle.toUpperCamelCase(currFieldName));
            requestFieldMethodExprBuilder = requestFieldMethodExprBuilder.setMethodName(bindingFieldMethodName);
            if (i < descendantFields.length - 1) {
                requestFieldMethodExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestFieldMethodExprBuilder.build());
            }
        }
        prevExpr = requestFieldMethodExprBuilder.build();
    }
    if (httpBindingFieldNames.isEmpty() && !asteriskBody) {
        returnExpr = ValueExpr.createNullExpr();
    } else {
        ImmutableList.Builder<Expr> paramsPutArgs = ImmutableList.builder();
        if (asteriskBody) {
            prevExpr = MethodInvocationExpr.builder().setExprReferenceExpr(prevExpr).setMethodName("build").build();
            bodyParamName = "*";
        }
        paramsPutArgs.add(ValueExpr.withValue(StringObjectValue.withValue(bodyParamName)));
        paramsPutArgs.add(prevExpr);
        returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(serializerExpr).setMethodName(serializerMethodName).setArguments(paramsPutArgs.build()).setReturnType(extractorReturnType).build();
    }
    // (https://github.com/googleapis/gax-java/blob/12b18ee255d3fabe13bb3969df40753b29f830d5/gax-httpjson/src/main/java/com/google/api/gax/httpjson/FieldsExtractor.java).
    return LambdaExpr.builder().setArguments(requestVarExpr.toBuilder().setIsDecl(true).build()).setBody(bodyStatements).setReturnExpr(returnExpr).build();
}
Also used : ProtoRestSerializer(com.google.api.gax.httpjson.ProtoRestSerializer) IfStatement(com.google.api.generator.engine.ast.IfStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) 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) HttpBinding(com.google.api.generator.gapic.model.HttpBindings.HttpBinding) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Example 3 with HttpBinding

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

the class HttpJsonServiceStubClassComposer method createFieldsExtractorClassInstance.

private Expr createFieldsExtractorClassInstance(Method method, TypeNode extractorReturnType, Set<HttpBinding> httpBindingFieldNames, String serializerMethodName) {
    List<Statement> bodyStatements = new ArrayList<>();
    VariableExpr fieldsVarExpr = VariableExpr.withVariable(Variable.builder().setName("fields").setType(extractorReturnType).build());
    Expr fieldsAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(FIXED_REST_TYPESTORE.get(HashMap.class.getSimpleName())).setIsGeneric(true).build()).build();
    bodyStatements.add(ExprStatement.withExpr(fieldsAssignExpr));
    TypeNode serializerVarType = TypeNode.withReference(ConcreteReference.builder().setClazz(ProtoRestSerializer.class).setGenerics(method.inputType().reference()).build());
    VariableExpr serializerVarExpr = VariableExpr.withVariable(Variable.builder().setName("serializer").setType(serializerVarType).build());
    Expr serializerAssignExpr = AssignmentExpr.builder().setVariableExpr(serializerVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_REST_TYPESTORE.get(ProtoRestSerializer.class.getSimpleName())).setMethodName("create").setReturnType(serializerVarType).build()).build();
    bodyStatements.add(ExprStatement.withExpr(serializerAssignExpr));
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
    for (HttpBinding httpBindingFieldName : httpBindingFieldNames) {
        // Handle foo.bar cases by descending into the subfields.
        MethodInvocationExpr.Builder requestFieldGetterExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr);
        MethodInvocationExpr.Builder requestFieldHasExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr);
        String[] descendantFields = httpBindingFieldName.name().split("\\.");
        for (int i = 0; i < descendantFields.length; i++) {
            String currFieldName = descendantFields[i];
            String bindingFieldMethodName = String.format("get%s", JavaStyle.toUpperCamelCase(currFieldName));
            requestFieldGetterExprBuilder = requestFieldGetterExprBuilder.setMethodName(bindingFieldMethodName);
            String bindingFieldHasMethodName = (i < descendantFields.length - 1) ? bindingFieldMethodName : String.format("has%s", JavaStyle.toUpperCamelCase(currFieldName));
            requestFieldHasExprBuilder = requestFieldHasExprBuilder.setMethodName(bindingFieldHasMethodName).setReturnType(TypeNode.BOOLEAN);
            if (i < descendantFields.length - 1) {
                requestFieldGetterExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestFieldGetterExprBuilder.build());
                requestFieldHasExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestFieldHasExprBuilder.build());
            }
        }
        MethodInvocationExpr requestBuilderExpr = requestFieldGetterExprBuilder.build();
        MethodInvocationExpr requestHasExpr = requestFieldHasExprBuilder.build();
        ImmutableList.Builder<Expr> paramsPutArgs = ImmutableList.builder();
        paramsPutArgs.add(fieldsVarExpr);
        paramsPutArgs.add(ValueExpr.withValue(StringObjectValue.withValue(JavaStyle.toLowerCamelCase(httpBindingFieldName.name()))));
        paramsPutArgs.add(requestBuilderExpr);
        Expr paramsPutExpr = MethodInvocationExpr.builder().setExprReferenceExpr(serializerVarExpr).setMethodName(serializerMethodName).setArguments(paramsPutArgs.build()).setReturnType(extractorReturnType).build();
        if (httpBindingFieldName.isOptional()) {
            bodyStatements.add(IfStatement.builder().setConditionExpr(requestHasExpr).setBody(Arrays.asList(ExprStatement.withExpr(paramsPutExpr))).build());
        } else {
            bodyStatements.add(ExprStatement.withExpr(paramsPutExpr));
        }
    }
    // (https://github.com/googleapis/gax-java/blob/12b18ee255d3fabe13bb3969df40753b29f830d5/gax-httpjson/src/main/java/com/google/api/gax/httpjson/FieldsExtractor.java).
    return LambdaExpr.builder().setArguments(requestVarExpr.toBuilder().setIsDecl(true).build()).setBody(bodyStatements).setReturnExpr(fieldsVarExpr).build();
}
Also used : ProtoRestSerializer(com.google.api.gax.httpjson.ProtoRestSerializer) IfStatement(com.google.api.generator.engine.ast.IfStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) 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) HttpBinding(com.google.api.generator.gapic.model.HttpBindings.HttpBinding) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 4 with HttpBinding

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

the class HttpRuleParser method validateAndConstructHttpBindings.

private static Set<HttpBinding> validateAndConstructHttpBindings(Set<String> paramNames, Message inputMessage, Map<String, Message> messageTypes, Map<String, String> patternSampleValues) {
    ImmutableSortedSet.Builder<HttpBinding> httpBindings = ImmutableSortedSet.naturalOrder();
    for (String paramName : paramNames) {
        // Handle foo.bar cases by descending into the subfields.
        String patternSampleValue = patternSampleValues != null ? patternSampleValues.get(paramName) : null;
        String[] subFields = paramName.split("\\.");
        if (inputMessage == null) {
            httpBindings.add(HttpBinding.create(paramName, false, patternSampleValue));
            continue;
        }
        Message nestedMessage = inputMessage;
        for (int i = 0; i < subFields.length; i++) {
            String subFieldName = subFields[i];
            if (i < subFields.length - 1) {
                Field field = nestedMessage.fieldMap().get(subFieldName);
                nestedMessage = messageTypes.get(field.type().reference().fullName());
                Preconditions.checkNotNull(nestedMessage, String.format("No containing message found for field %s with type %s", field.name(), field.type().reference().simpleName()));
            } else {
                if (patternSampleValues != null) {
                    checkHttpFieldIsValid(subFieldName, nestedMessage, false);
                    patternSampleValue = patternSampleValues.get(paramName);
                }
                Field field = nestedMessage.fieldMap().get(subFieldName);
                httpBindings.add(HttpBinding.create(paramName, field.isProto3Optional(), patternSampleValue));
            }
        }
    }
    return httpBindings.build();
}
Also used : Field(com.google.api.generator.gapic.model.Field) Message(com.google.api.generator.gapic.model.Message) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) HttpBinding(com.google.api.generator.gapic.model.HttpBindings.HttpBinding)

Aggregations

HttpBinding (com.google.api.generator.gapic.model.HttpBindings.HttpBinding)4 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)3 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)3 Expr (com.google.api.generator.engine.ast.Expr)3 LambdaExpr (com.google.api.generator.engine.ast.LambdaExpr)3 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)3 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)3 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)3 ProtoRestSerializer (com.google.api.gax.httpjson.ProtoRestSerializer)2 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)2 IfStatement (com.google.api.generator.engine.ast.IfStatement)2 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)2 Statement (com.google.api.generator.engine.ast.Statement)2 TypeNode (com.google.api.generator.engine.ast.TypeNode)2 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 RequestParamsBuilder (com.google.api.gax.rpc.RequestParamsBuilder)1 LogicalOperationExpr (com.google.api.generator.engine.ast.LogicalOperationExpr)1 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)1 Field (com.google.api.generator.gapic.model.Field)1