use of com.google.api.generator.gapic.model.OperationResponse in project gapic-generator-java by googleapis.
the class Parser method parseMessages.
private static Map<String, Message> parseMessages(Descriptor messageDescriptor, Set<ResourceReference> outputResourceReferencesSeen, List<String> outerNestedTypes) {
Map<String, Message> messages = new HashMap<>();
String messageName = messageDescriptor.getName();
if (!messageDescriptor.getNestedTypes().isEmpty()) {
for (Descriptor nestedMessage : messageDescriptor.getNestedTypes()) {
if (isMapType(nestedMessage)) {
continue;
}
List<String> currentNestedTypes = new ArrayList<>(outerNestedTypes);
currentNestedTypes.add(messageName);
messages.putAll(parseMessages(nestedMessage, outputResourceReferencesSeen, currentNestedTypes));
}
}
TypeNode messageType = TypeParser.parseType(messageDescriptor);
List<FieldDescriptor> fields = messageDescriptor.getFields();
HashMap<String, String> operationRequestFields = new HashMap<String, String>();
BiMap<String, String> operationResponseFields = HashBiMap.create();
OperationResponse.Builder operationResponse = null;
for (FieldDescriptor fd : fields) {
if (fd.getOptions().hasExtension(ExtendedOperationsProto.operationRequestField)) {
String orf = fd.getOptions().getExtension(ExtendedOperationsProto.operationRequestField);
operationRequestFields.put(orf, fd.getName());
}
if (fd.getOptions().hasExtension(ExtendedOperationsProto.operationResponseField)) {
String orf = fd.getOptions().getExtension(ExtendedOperationsProto.operationResponseField);
operationResponseFields.put(orf, fd.getName());
}
if (fd.getOptions().hasExtension(ExtendedOperationsProto.operationField)) {
OperationResponseMapping orm = fd.getOptions().getExtension(ExtendedOperationsProto.operationField);
if (operationResponse == null) {
operationResponse = OperationResponse.builder();
}
if (orm.equals(OperationResponseMapping.NAME)) {
operationResponse.setNameFieldName(fd.getName());
} else if (orm.equals(OperationResponseMapping.STATUS)) {
operationResponse.setStatusFieldName(fd.getName());
operationResponse.setStatusFieldTypeName(fd.toProto().getTypeName());
} else if (orm.equals(OperationResponseMapping.ERROR_CODE)) {
operationResponse.setErrorCodeFieldName(fd.getName());
} else if (orm.equals(OperationResponseMapping.ERROR_MESSAGE)) {
operationResponse.setErrorMessageFieldName(fd.getName());
}
}
}
messages.put(messageType.reference().fullName(), Message.builder().setType(messageType).setName(messageName).setFullProtoName(messageDescriptor.getFullName()).setFields(parseFields(messageDescriptor, outputResourceReferencesSeen)).setOuterNestedTypes(outerNestedTypes).setOperationRequestFields(operationRequestFields).setOperationResponseFields(operationResponseFields).setOperationResponse(operationResponse != null ? operationResponse.build() : null).build());
return messages;
}
use of com.google.api.generator.gapic.model.OperationResponse in project gapic-generator-java by googleapis.
the class HttpJsonServiceStubClassComposer method setOperationSnapshotFactoryExpr.
private List<Expr> setOperationSnapshotFactoryExpr(Method protoMethod, Map<String, Message> messageTypes) {
// Generate input variables for create()
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(protoMethod.inputType()).setName("request").build());
VariableExpr responseVarExpr = VariableExpr.withVariable(Variable.builder().setType(protoMethod.outputType()).setName("response").build());
MethodInvocationExpr buildExpr;
List<Statement> createBody = new ArrayList<>(4);
TypeNode httpJsonOperationSnapshotType = FIXED_REST_TYPESTORE.get(HttpJsonOperationSnapshot.class.getSimpleName());
TypeNode operationSnapshotType = FIXED_TYPESTORE.get(OperationSnapshot.class.getSimpleName());
Message inputOperationMessage = messageTypes.get(protoMethod.inputType().reference().fullName());
Message outputOperationMessage = messageTypes.get(protoMethod.outputType().reference().fullName());
OperationResponse operationResponse = outputOperationMessage.operationResponse();
if (operationResponse == null) {
// AIP-151 LRO
// HttpJsonOperationSnapshot.create(response)
buildExpr = MethodInvocationExpr.builder().setStaticReferenceType(httpJsonOperationSnapshotType).setMethodName("create").setArguments(responseVarExpr).setReturnType(operationSnapshotType).build();
} else {
BiFunction<String, List<Expr>, Function<MethodInvocationExpr, MethodInvocationExpr>> methodMaker = getMethodMaker();
// Generate opName
TypeNode stringBuilderType = TypeNode.withReference(ConcreteReference.withClazz(StringBuilder.class));
VariableExpr opNameVarExpr = VariableExpr.withVariable(Variable.builder().setType(stringBuilderType).setName("opName").build());
MethodInvocationExpr getId = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName(getMethodFormat(operationResponse.nameFieldName())).build();
Expr opNameObjectExpr = NewObjectExpr.builder().setType(stringBuilderType).setArguments(getId).build();
AssignmentExpr opNameAssignExpr = AssignmentExpr.builder().setVariableExpr(opNameVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(opNameObjectExpr).build();
createBody.add(ExprStatement.withExpr(opNameAssignExpr));
// Generate compound operation name
if (!protoMethod.isOperationPollingMethod()) {
// TODO: Change to ordered map
Map<String, String> requestFields = inputOperationMessage.operationRequestFields();
List<String> fieldAnnotationNames = new ArrayList<>(requestFields.keySet());
Collections.sort(fieldAnnotationNames);
for (String fieldName : fieldAnnotationNames) {
createBody.add(appendField(opNameVarExpr, requestVarExpr, requestFields.get(fieldName)));
}
}
// Generate check for status == done
MethodInvocationExpr getStatusExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName(getMethodFormat(operationResponse.statusFieldName())).build();
String statusTypeName = operationResponse.statusFieldTypeName();
String statusClassName = statusTypeName.substring(statusTypeName.lastIndexOf('.') + 1);
TypeNode opType = protoMethod.hasLro() ? protoMethod.lro().responseType() : protoMethod.outputType();
TypeNode statusType = TypeNode.withReference(VaporReference.builder().setName(statusClassName).setPakkage(opType.reference().fullName()).setIsStaticImport(false).build());
VariableExpr statusDoneExpr = VariableExpr.builder().setVariable(Variable.builder().setName("DONE").setType(TypeNode.INT).build()).setStaticReferenceType(statusType).build();
MethodInvocationExpr statusEqualsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(statusDoneExpr).setMethodName("equals").setArguments(getStatusExpr).build();
// Generate return statement
// Generate getter methods from annotations
MethodInvocationExpr opNameToStringExpr = MethodInvocationExpr.builder().setExprReferenceExpr(opNameVarExpr).setMethodName("toString").build();
MethodInvocationExpr getHttpErrorStatusCodeExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName(getMethodFormat(operationResponse.errorCodeFieldName())).build();
MethodInvocationExpr getHttpErrorMessageExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName(getMethodFormat(operationResponse.errorMessageFieldName())).build();
MethodInvocationExpr newBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(httpJsonOperationSnapshotType).setMethodName("newBuilder").build();
newBuilderExpr = methodMaker.apply("setName", Collections.singletonList(opNameToStringExpr)).apply(newBuilderExpr);
newBuilderExpr = methodMaker.apply("setMetadata", Collections.singletonList(responseVarExpr)).apply(newBuilderExpr);
newBuilderExpr = methodMaker.apply("setDone", Collections.singletonList(statusEqualsExpr)).apply(newBuilderExpr);
newBuilderExpr = methodMaker.apply("setResponse", Collections.singletonList(responseVarExpr)).apply(newBuilderExpr);
newBuilderExpr = methodMaker.apply("setError", Arrays.asList(getHttpErrorStatusCodeExpr, getHttpErrorMessageExpr)).apply(newBuilderExpr);
buildExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderExpr).setMethodName("build").setReturnType(operationSnapshotType).build();
}
// Generate lambda anonymous class
return Collections.singletonList(LambdaExpr.builder().setArguments(requestVarExpr.toBuilder().setIsDecl(true).build(), responseVarExpr.toBuilder().setIsDecl(true).build()).setBody(createBody).setReturnExpr(buildExpr).build());
}
Aggregations