use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class DefaultValueComposer method createSimplePagedResponseValue.
public static Expr createSimplePagedResponseValue(TypeNode responseType, String repeatedFieldName, Expr responseElementVarExpr, boolean isMap) {
// Code for paginated maps:
// AggregatedMessageList.newBuilder()
// .setNextPageToken("")
// .putAllItems(Collections.singletonMap("items", responsesElement))
// .build();
//
// Code for paginated arrays:
// MessageList expectedResponse =
// AddressList.newBuilder()
// .setNextPageToken("")
// .addAllItems(Arrays.asList(responsesElement))
// .build();
Expr pagedResponseExpr = MethodInvocationExpr.builder().setStaticReferenceType(responseType).setMethodName("newBuilder").build();
pagedResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedResponseExpr).setMethodName("setNextPageToken").setArguments(ValueExpr.withValue(StringObjectValue.withValue(""))).build();
if (isMap) {
pagedResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedResponseExpr).setMethodName(String.format("putAll%s", JavaStyle.toUpperCamelCase(repeatedFieldName))).setArguments(MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Collections.class))).setMethodName("singletonMap").setArguments(ValueExpr.withValue(StringObjectValue.withValue(JavaStyle.toLowerCamelCase(repeatedFieldName))), responseElementVarExpr).build()).build();
} else {
pagedResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedResponseExpr).setMethodName(String.format("addAll%s", JavaStyle.toUpperCamelCase(repeatedFieldName))).setArguments(MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Arrays.class))).setMethodName("asList").setArguments(responseElementVarExpr).build()).build();
}
return MethodInvocationExpr.builder().setExprReferenceExpr(pagedResponseExpr).setMethodName("build").setReturnType(responseType).build();
}
use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class DefaultValueComposer method createSimpleMessageBuilderValue.
public static Expr createSimpleMessageBuilderValue(Message message, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes, Map<String, String> valuePatterns) {
MethodInvocationExpr builderExpr = MethodInvocationExpr.builder().setStaticReferenceType(message.type()).setMethodName("newBuilder").build();
for (Field field : message.fields()) {
if (// Avoid colliding fields.
field.isContainedInOneof() || ((field.isMessage() || (field.isEnum() && // Avoid importing unparsed messages.
message.operationResponse() == null)) && !field.isRepeated() && !messageTypes.containsKey(field.type().reference().fullName()))) {
continue;
}
String setterMethodNamePattern = "set%s";
if (field.isRepeated()) {
setterMethodNamePattern = field.isMap() ? "putAll%s" : "addAll%s";
}
Expr defaultExpr = null;
if (field.hasResourceReference() && resourceNames.get(field.resourceReference().resourceTypeString()) != null) {
defaultExpr = createResourceHelperValue(resourceNames.get(field.resourceReference().resourceTypeString()), field.resourceReference().isChildType(), resourceNames.values().stream().collect(Collectors.toList()), message.name(), /* allowAnonResourceNameClass = */
false);
defaultExpr = MethodInvocationExpr.builder().setExprReferenceExpr(defaultExpr).setMethodName("toString").setReturnType(TypeNode.STRING).build();
} else {
if (message.operationResponse() != null) {
if (field.name().equals(message.operationResponse().statusFieldName())) {
String statusTypeName = message.operationResponse().statusFieldTypeName();
String statusClassName = statusTypeName.substring(statusTypeName.lastIndexOf('.') + 1);
TypeNode statusType = TypeNode.withReference(VaporReference.builder().setName(statusClassName).setPakkage(message.type().reference().fullName()).setIsStaticImport(false).build());
defaultExpr = VariableExpr.builder().setVariable(Variable.builder().setName("DONE").setType(statusType).build()).setStaticReferenceType(statusType).build();
} else if (field.name().equals(message.operationResponse().errorCodeFieldName())) {
defaultExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(field.type()).setValue("0").build());
}
}
if (defaultExpr == null) {
defaultExpr = createValue(field, true, resourceNames, messageTypes, valuePatterns);
}
}
builderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderExpr).setMethodName(String.format(setterMethodNamePattern, JavaStyle.toUpperCamelCase(field.name()))).setArguments(defaultExpr).build();
}
return MethodInvocationExpr.builder().setExprReferenceExpr(builderExpr).setMethodName("build").setReturnType(message.type()).build();
}
use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class DefaultValueComposer method createMethodArgValue.
public static Expr createMethodArgValue(MethodArgument methodArg, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes, Map<String, String> valuePatterns) {
if (methodArg.isResourceNameHelper()) {
Preconditions.checkState(methodArg.field().hasResourceReference(), String.format("No corresponding resource reference for argument %s found on field %s %s", methodArg.name(), methodArg.field().type(), methodArg.field().name()));
ResourceName resourceName = resourceNames.get(methodArg.field().resourceReference().resourceTypeString());
Preconditions.checkNotNull(resourceName, String.format("No resource name found for reference %s", methodArg.field().resourceReference().resourceTypeString()));
Expr defValue = createResourceHelperValue(resourceName, methodArg.field().resourceReference().isChildType(), resourceNames.values().stream().collect(Collectors.toList()), methodArg.field().name());
if (!methodArg.isResourceNameHelper() && methodArg.field().hasResourceReference()) {
defValue = MethodInvocationExpr.builder().setExprReferenceExpr(defValue).setMethodName("toString").setReturnType(TypeNode.STRING).build();
}
return defValue;
}
if (methodArg.type().equals(methodArg.field().type())) {
return createValue(methodArg.field(), false, resourceNames, messageTypes, valuePatterns);
}
return createValue(Field.builder().setName(methodArg.name()).setType(methodArg.type()).build());
}
use of com.google.api.generator.engine.ast.Expr 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.Expr 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