use of com.google.api.generator.engine.ast.VariableExpr 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.VariableExpr 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.VariableExpr in project gapic-generator-java by googleapis.
the class RetrySettingsComposer method createBatchingBuilderSettingsExpr.
public static Expr createBatchingBuilderSettingsExpr(String settingsGetterMethodName, GapicBatchingSettings batchingSettings, VariableExpr builderVarExpr) {
Expr batchingSettingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("BatchingSettings")).setMethodName("newBuilder").build();
batchingSettingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(batchingSettingsBuilderExpr).setMethodName("setElementCountThreshold").setArguments(toValExpr(batchingSettings.elementCountThreshold())).build();
batchingSettingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(batchingSettingsBuilderExpr).setMethodName("setRequestByteThreshold").setArguments(toValExpr(batchingSettings.requestByteThreshold())).build();
batchingSettingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(batchingSettingsBuilderExpr).setMethodName("setDelayThreshold").setArguments(createDurationOfMillisExpr(toValExpr(batchingSettings.delayThresholdMillis()))).build();
// FlowControlSettings.
Expr flowControlSettingsExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("FlowControlSettings")).setMethodName("newBuilder").build();
if (batchingSettings.flowControlElementLimit() != null) {
flowControlSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(flowControlSettingsExpr).setMethodName("setMaxOutstandingElementCount").setArguments(toValExpr(batchingSettings.flowControlElementLimit())).build();
}
if (batchingSettings.flowControlByteLimit() != null) {
flowControlSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(flowControlSettingsExpr).setMethodName("setMaxOutstandingRequestBytes").setArguments(toValExpr(batchingSettings.flowControlByteLimit())).build();
}
flowControlSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(flowControlSettingsExpr).setMethodName("setLimitExceededBehavior").setArguments(EnumRefExpr.builder().setType(FIXED_TYPESTORE.get("LimitExceededBehavior")).setName(JavaStyle.toUpperCamelCase(batchingSettings.flowControlLimitExceededBehavior().name().toLowerCase())).build()).build();
flowControlSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(flowControlSettingsExpr).setMethodName("build").build();
batchingSettingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(batchingSettingsBuilderExpr).setMethodName("setFlowControlSettings").setArguments(flowControlSettingsExpr).build();
batchingSettingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(batchingSettingsBuilderExpr).setMethodName("build").build();
// Put everything together.
Expr builderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName(settingsGetterMethodName).build();
return MethodInvocationExpr.builder().setExprReferenceExpr(builderExpr).setMethodName("setBatchingSettings").setArguments(batchingSettingsBuilderExpr).build();
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class DefaultValueComposer method createSimpleOperationBuilderValue.
public static Expr createSimpleOperationBuilderValue(String name, VariableExpr responseExpr) {
Expr operationExpr = MethodInvocationExpr.builder().setStaticReferenceType(OPERATION_TYPE).setMethodName("newBuilder").build();
operationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(operationExpr).setMethodName("setName").setArguments(ValueExpr.withValue(StringObjectValue.withValue(name))).build();
operationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(operationExpr).setMethodName("setDone").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build())).build();
operationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(operationExpr).setMethodName("setResponse").setArguments(MethodInvocationExpr.builder().setStaticReferenceType(ANY_TYPE).setMethodName("pack").setArguments(responseExpr).build()).build();
return MethodInvocationExpr.builder().setExprReferenceExpr(operationExpr).setMethodName("build").setReturnType(OPERATION_TYPE).build();
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class MockServiceImplClassComposer method createHandleObjectStatement.
private static Statement createHandleObjectStatement(Method protoMethod, VariableExpr requestArgVarExpr, VariableExpr responseObserverVarExpr, VariableExpr localResponseVarExpr) {
List<Expr> ifBodyExprs = new ArrayList<>();
boolean isAnonymousClass = requestArgVarExpr == null;
if (!isAnonymousClass) {
ifBodyExprs.add(MethodInvocationExpr.builder().setMethodName("add").setArguments(Arrays.asList(requestArgVarExpr)).setExprReferenceExpr(requestsVarExpr).build());
}
ifBodyExprs.add(MethodInvocationExpr.builder().setMethodName("onNext").setArguments(Arrays.asList(CastExpr.builder().setType(protoMethod.outputType()).setExpr(localResponseVarExpr).build())).setExprReferenceExpr(responseObserverVarExpr).build());
if (!isAnonymousClass) {
ifBodyExprs.add(MethodInvocationExpr.builder().setMethodName("onCompleted").setExprReferenceExpr(responseObserverVarExpr).build());
}
TypeNode exceptionType = TypeNode.withReference(ConcreteReference.withClazz(Exception.class));
// Constructs `response == null ? "" : response.getClass().getName()`.
Expr actualResponseTypeString = TernaryExpr.builder().setConditionExpr(RelationalOperationExpr.equalToWithExprs(localResponseVarExpr, ValueExpr.createNullExpr())).setThenExpr(ValueExpr.withValue(StringObjectValue.withValue("null"))).setElseExpr(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setExprReferenceExpr(localResponseVarExpr).setMethodName("getClass").build()).setMethodName("getName").setReturnType(TypeNode.STRING).build()).build();
Function<TypeNode, Expr> typeToStrFn = t -> MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.builder().setStaticReferenceType(t).setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build()).build()).setMethodName("getName").setReturnType(TypeNode.STRING).build();
Expr expectedResponseTypeOneString = typeToStrFn.apply(protoMethod.outputType());
Expr expectedResponseTypeTwoString = typeToStrFn.apply(exceptionType);
Expr newExceptionExpr = NewObjectExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(IllegalArgumentException.class))).setArguments(// Operation.class.getName(), Exception.class.getName());
MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.STRING).setMethodName("format").setArguments(ValueExpr.withValue(StringObjectValue.withValue("Unrecognized response type %s for method " + protoMethod.name() + ", expected %s or %s")), actualResponseTypeString, expectedResponseTypeOneString, expectedResponseTypeTwoString).build()).build();
return IfStatement.builder().setConditionExpr(InstanceofExpr.builder().setExpr(localResponseVarExpr).setCheckType(protoMethod.outputType()).build()).setBody(ifBodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).addElseIf(InstanceofExpr.builder().setExpr(localResponseVarExpr).setCheckType(exceptionType).build(), Arrays.asList(ExprStatement.withExpr(MethodInvocationExpr.builder().setMethodName("onError").setArguments(Arrays.asList(CastExpr.builder().setType(exceptionType).setExpr(localResponseVarExpr).build())).setExprReferenceExpr(responseObserverVarExpr).build()))).setElseBody(Arrays.asList(ExprStatement.withExpr(MethodInvocationExpr.builder().setMethodName("onError").setExprReferenceExpr(responseObserverVarExpr).setArguments(Arrays.asList(newExceptionExpr)).build()))).build();
}
Aggregations