Search in sources :

Example 1 with RoutingHeaderParam

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

the class RoutingRuleParserTest method parse_shouldParseRoutingRulesWithOneParameter.

@Test
public void parse_shouldParseRoutingRulesWithOneParameter() {
    RoutingHeaderRule actual = getRoutingHeaders(4);
    RoutingHeaderParam expected = RoutingHeaderParam.create("name", "rename", "/v1beta1/{rename=tests/*}");
    assertThat(actual.routingHeaderParams()).containsExactly(expected);
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) Test(org.junit.Test)

Example 2 with RoutingHeaderParam

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

the class RoutingRuleParserTest method parse_shouldParseRoutingRulesWithNestedFields.

@Test
public void parse_shouldParseRoutingRulesWithNestedFields() {
    RoutingHeaderRule actual = getRoutingHeaders(6);
    RoutingHeaderParam expectedHeader1 = RoutingHeaderParam.create("account.name", "rename", "/v1beta1/{rename=tests/*}");
    assertThat(actual.routingHeaderParams()).containsExactly(expectedHeader1);
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) Test(org.junit.Test)

Example 3 with RoutingHeaderParam

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

the class RoutingHeaderParamTest method getDescendantFieldNames_shouldSplitFieldNameByDot.

@Test
public void getDescendantFieldNames_shouldSplitFieldNameByDot() {
    RoutingHeaderParam routingHeaderParam = RoutingHeaderParam.create("table.name", "name", "/abc/dec");
    List<String> descendantFieldNames = routingHeaderParam.getDescendantFieldNames();
    assertThat(descendantFieldNames).containsExactly("table", "name");
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) Test(org.junit.Test)

Example 4 with RoutingHeaderParam

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

the class GrpcServiceStubClassComposer method createRequestParamsExtractorBodyForRoutingHeaders.

private void createRequestParamsExtractorBodyForRoutingHeaders(Method method, VariableExpr requestVarExpr, List<Statement> classStatements, List<Statement> bodyStatements, MethodInvocationExpr.Builder returnExprBuilder) {
    TypeNode routingHeadersBuilderType = TypeNode.withReference(ConcreteReference.builder().setClazz(RequestParamsBuilder.class).build());
    VariableExpr routingHeadersBuilderVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("builder").setType(routingHeadersBuilderType).build()).setIsDecl(true).build();
    MethodInvocationExpr routingHeaderBuilderInvokeExpr = MethodInvocationExpr.builder().setStaticReferenceType(routingHeadersBuilderType).setMethodName("create").setReturnType(routingHeadersBuilderType).build();
    Expr routingHeadersBuilderInitExpr = AssignmentExpr.builder().setVariableExpr(routingHeadersBuilderVarExpr).setValueExpr(routingHeaderBuilderInvokeExpr).build();
    bodyStatements.add(ExprStatement.withExpr(routingHeadersBuilderInitExpr));
    List<RoutingHeaderParam> routingHeaderParams = method.routingHeaderRule().routingHeaderParams();
    VariableExpr routingHeadersBuilderVarNonDeclExpr = VariableExpr.builder().setVariable(Variable.builder().setName("builder").setType(routingHeadersBuilderType).build()).build();
    for (int i = 0; i < routingHeaderParams.size(); i++) {
        RoutingHeaderParam routingHeaderParam = routingHeaderParams.get(i);
        MethodInvocationExpr requestFieldGetterExpr = createRequestFieldGetterExpr(requestVarExpr, routingHeaderParam.fieldName());
        Expr routingHeaderKeyExpr = ValueExpr.withValue(StringObjectValue.withValue(routingHeaderParam.key()));
        String pathTemplateName = String.format("%s_%s_PATH_TEMPLATE", JavaStyle.toUpperSnakeCase(method.name()), i);
        TypeNode pathTemplateType = TypeNode.withReference(ConcreteReference.withClazz(PathTemplate.class));
        Variable pathTemplateVar = Variable.builder().setType(pathTemplateType).setName(pathTemplateName).build();
        Expr routingHeaderPatternExpr = VariableExpr.withVariable(pathTemplateVar);
        Statement pathTemplateClassVar = createPathTemplateClassStatement(routingHeaderParam, pathTemplateType, pathTemplateVar);
        classStatements.add(pathTemplateClassVar);
        MethodInvocationExpr addParamMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(routingHeadersBuilderVarNonDeclExpr).setMethodName("add").setArguments(requestFieldGetterExpr, routingHeaderKeyExpr, routingHeaderPatternExpr).build();
        ExprStatement addParamStatement = ExprStatement.withExpr(addParamMethodExpr);
        // No need to add null check if there is no nested fields
        if (routingHeaderParam.getDescendantFieldNames().size() == 1) {
            bodyStatements.add(addParamStatement);
        } else {
            IfStatement ifStatement = IfStatement.builder().setConditionExpr(fieldValuesNotNullConditionExpr(requestVarExpr, routingHeaderParam.getDescendantFieldNames())).setBody(ImmutableList.of(addParamStatement)).build();
            bodyStatements.add(ifStatement);
        }
    }
    returnExprBuilder.setExprReferenceExpr(routingHeadersBuilderVarNonDeclExpr).setMethodName("build");
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) Variable(com.google.api.generator.engine.ast.Variable) IfStatement(com.google.api.generator.engine.ast.IfStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) PathTemplate(com.google.api.pathtemplate.PathTemplate) IfStatement(com.google.api.generator.engine.ast.IfStatement) 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) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 5 with RoutingHeaderParam

use of com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam 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();
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) RoutingParameter(com.google.api.RoutingParameter) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) RoutingRule(com.google.api.RoutingRule)

Aggregations

RoutingHeaderParam (com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam)7 RoutingHeaderRule (com.google.api.generator.gapic.model.RoutingHeaderRule)5 Test (org.junit.Test)5 RoutingParameter (com.google.api.RoutingParameter)1 RoutingRule (com.google.api.RoutingRule)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 LogicalOperationExpr (com.google.api.generator.engine.ast.LogicalOperationExpr)1 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)1 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)1 Statement (com.google.api.generator.engine.ast.Statement)1 TypeNode (com.google.api.generator.engine.ast.TypeNode)1 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)1 Variable (com.google.api.generator.engine.ast.Variable)1 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)1 PathTemplate (com.google.api.pathtemplate.PathTemplate)1