use of com.google.api.generator.engine.ast.BlockStatement in project gapic-generator-java by googleapis.
the class RetrySettingsComposer method createRetryCodesDefinitionsBlock.
public static BlockStatement createRetryCodesDefinitionsBlock(Service service, GapicServiceConfig serviceConfig, VariableExpr retryCodesDefinitionsClassMemberVarExpr) {
TypeNode definitionsType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(retryCodesDefinitionsClassMemberVarExpr.type().reference().generics()).build());
VariableExpr definitionsVarExpr = VariableExpr.withVariable(Variable.builder().setType(definitionsType).setName("definitions").build());
List<Expr> bodyExprs = new ArrayList<>();
// Create the first expr.
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(definitionsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(definitionsVarExpr.type()).build()).build());
for (Map.Entry<String, List<Code>> codeEntry : serviceConfig.getAllRetryCodes(service).entrySet()) {
bodyExprs.add(createRetryCodeDefinitionExpr(codeEntry.getKey(), codeEntry.getValue(), definitionsVarExpr));
}
// Reassign the new codes.
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(retryCodesDefinitionsClassMemberVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(definitionsVarExpr).setMethodName("build").setReturnType(retryCodesDefinitionsClassMemberVarExpr.type()).build()).build());
// Put everything together.
return BlockStatement.builder().setIsStatic(true).setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
use of com.google.api.generator.engine.ast.BlockStatement in project gapic-generator-java by googleapis.
the class RetrySettingsComposer method createRetryParamDefinitionsBlock.
public static BlockStatement createRetryParamDefinitionsBlock(Service service, GapicServiceConfig serviceConfig, VariableExpr retryParamDefinitionsClassMemberVarExpr) {
List<Expr> bodyExprs = new ArrayList<>();
TypeNode definitionsType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(retryParamDefinitionsClassMemberVarExpr.type().reference().generics()).build());
VariableExpr definitionsVarExpr = VariableExpr.withVariable(Variable.builder().setType(definitionsType).setName("definitions").build());
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("RetrySettings")).setName("settings").build());
// Create the first two exprs.
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(definitionsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(definitionsVarExpr.type()).build()).build());
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(ValueExpr.createNullExpr()).build());
// Build the settings object for each config.
for (Map.Entry<String, GapicRetrySettings> settingsEntry : serviceConfig.getAllGapicRetrySettings(service).entrySet()) {
bodyExprs.addAll(createRetrySettingsExprs(settingsEntry.getKey(), settingsEntry.getValue(), settingsVarExpr, definitionsVarExpr));
}
// Reassign the new settings.
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(retryParamDefinitionsClassMemberVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(definitionsVarExpr).setMethodName("build").setReturnType(retryParamDefinitionsClassMemberVarExpr.type()).build()).build());
// Put everything together.
return BlockStatement.builder().setIsStatic(true).setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
use of com.google.api.generator.engine.ast.BlockStatement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeBlockStatement_simple.
@Test
public void writeBlockStatement_simple() {
TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.some.pakkage").build());
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setStaticReferenceType(someType).build();
BlockStatement blockStatement = BlockStatement.builder().setBody(Arrays.asList(ExprStatement.withExpr(methodExpr))).build();
blockStatement.accept(writerVisitor);
assertEquals("{\nSomeClass.foobar();\n}\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.BlockStatement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeBlockStatement_empty.
@Test
public void writeBlockStatement_empty() {
BlockStatement blockStatement = BlockStatement.builder().build();
blockStatement.accept(writerVisitor);
assertEquals("{\n}\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.BlockStatement in project gapic-generator-java by googleapis.
the class RetrySettingsComposerTest method codesDefinitionsBlock_basic.
@Test
public void codesDefinitionsBlock_basic() {
FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services = Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
assertEquals(1, services.size());
Service service = services.get(0);
String jsonFilename = "showcase_grpc_service_config.json";
Path jsonPath = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), jsonFilename);
Optional<GapicServiceConfig> serviceConfigOpt = ServiceConfigParser.parse(jsonPath.toString());
assertTrue(serviceConfigOpt.isPresent());
GapicServiceConfig serviceConfig = serviceConfigOpt.get();
BlockStatement paramDefinitionsBlock = RetrySettingsComposer.createRetryCodesDefinitionsBlock(service, serviceConfig, RETRY_CODES_DEFINITIONS_VAR_EXPR);
paramDefinitionsBlock.accept(writerVisitor);
String expected = LineFormatter.lines("static {\n", "ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions =" + " ImmutableMap.builder();\n", "definitions.put(\"retry_policy_1_codes\"," + " ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList(StatusCode.Code.UNAVAILABLE," + " StatusCode.Code.UNKNOWN)));\n", "definitions.put(\"no_retry_0_codes\"," + " ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));\n", "RETRYABLE_CODE_DEFINITIONS = definitions.build();\n", "}\n");
assertEquals(expected, writerVisitor.write());
}
Aggregations