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);
}
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);
}
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");
}
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");
}
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();
}
Aggregations