use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createPagedListResponseFactoryAssignExpr.
private static Expr createPagedListResponseFactoryAssignExpr(VariableExpr pageStrDescVarExpr, Method method, TypeNode repeatedResponseType, TypeStore typeStore) {
Preconditions.checkState(method.isPaged(), String.format("Method %s is not paged", method.name()));
// Create the PagedListResponseFactory.
TypeNode pagedResponseType = typeStore.get(getPagedResponseTypeName(method.name()));
TypeNode apiFutureType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(pagedResponseType.reference())).build());
VariableExpr callableVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(UnaryCallable.class).setGenerics(Arrays.asList(method.inputType().reference(), method.outputType().reference())).build())).setName("callable").build());
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
VariableExpr contextVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("ApiCallContext")).setName("context").build());
VariableExpr futureResponseVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(method.outputType().reference())).build())).setName("futureResponse").build());
TypeNode pageContextType = TypeNode.withReference(ConcreteReference.builder().setClazz(PageContext.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
VariableExpr pageContextVarExpr = VariableExpr.withVariable(Variable.builder().setType(pageContextType).setName("pageContext").build());
AssignmentExpr pageContextAssignExpr = AssignmentExpr.builder().setVariableExpr(pageContextVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("PageContext")).setMethodName("create").setArguments(callableVarExpr, pageStrDescVarExpr, requestVarExpr, contextVarExpr).setReturnType(pageContextVarExpr.type()).build()).build();
Expr returnExpr = MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(getPagedResponseTypeName(method.name()))).setMethodName("createAsync").setArguments(pageContextVarExpr, futureResponseVarExpr).setReturnType(apiFutureType).build();
MethodDefinition getFuturePagedResponseMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(apiFutureType).setName("getFuturePagedResponse").setArguments(Arrays.asList(callableVarExpr, requestVarExpr, contextVarExpr, futureResponseVarExpr).stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setBody(Arrays.asList(ExprStatement.withExpr(pageContextAssignExpr))).setReturnExpr(returnExpr).build();
// Create the variable.
TypeNode pagedResponseFactoryType = TypeNode.withReference(ConcreteReference.builder().setClazz(PagedListResponseFactory.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), typeStore.get(getPagedResponseTypeName(method.name()))).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
String varName = String.format(PAGED_RESPONSE_FACTORY_PATTERN, JavaStyle.toUpperSnakeCase(method.name()));
VariableExpr pagedListResponseFactoryVarExpr = VariableExpr.withVariable(Variable.builder().setType(pagedResponseFactoryType).setName(varName).build());
AnonymousClassExpr factoryAnonClassExpr = AnonymousClassExpr.builder().setType(pagedResponseFactoryType).setMethods(Arrays.asList(getFuturePagedResponseMethod)).build();
return AssignmentExpr.builder().setVariableExpr(pagedListResponseFactoryVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(factoryAnonClassExpr).build();
}
use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createPagingStaticAssignExprs.
private static List<Expr> createPagingStaticAssignExprs(Service service, GapicServiceConfig serviceConfig, Map<String, Message> messageTypes, TypeStore typeStore) {
List<Expr> descExprs = new ArrayList<>();
List<Expr> factoryExprs = new ArrayList<>();
for (Method method : service.methods()) {
if (!method.isPaged()) {
continue;
}
// Find the repeated type.
String pagedResponseMessageKey = method.outputType().reference().fullName();
if (method.hasLro()) {
pagedResponseMessageKey = method.lro().responseType().reference().fullName();
}
Message pagedResponseMessage = messageTypes.get(pagedResponseMessageKey);
Preconditions.checkNotNull(pagedResponseMessage, String.format("No method found for message type %s for method %s among %s", pagedResponseMessageKey, method.name(), messageTypes.keySet()));
Field repeatedPagedResultsField = pagedResponseMessage.findAndUnwrapPaginatedRepeatedField();
Preconditions.checkNotNull(repeatedPagedResultsField, String.format("No repeated type found for paged reesponse %s for method %s", method.outputType().reference().name(), method.name()));
TypeNode repeatedResponseType = repeatedPagedResultsField.type();
String repeatedFieldName = repeatedPagedResultsField.name();
// Create the PAGE_STR_DESC variable.
TypeNode pagedListDescType = TypeNode.withReference(ConcreteReference.builder().setClazz(PagedListDescriptor.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
String pageStrDescVarName = String.format(PAGE_STR_DESC_PATTERN, JavaStyle.toUpperSnakeCase(method.name()));
VariableExpr pagedListDescVarExpr = VariableExpr.withVariable(Variable.builder().setType(pagedListDescType).setName(pageStrDescVarName).build());
descExprs.add(createPagedListDescriptorAssignExpr(pagedListDescVarExpr, method, repeatedResponseType, repeatedFieldName, messageTypes, typeStore));
factoryExprs.add(createPagedListResponseFactoryAssignExpr(pagedListDescVarExpr, method, repeatedResponseType, typeStore));
}
descExprs.addAll(factoryExprs);
return descExprs;
}
use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createClassStatements.
private static List<Statement> createClassStatements(Service service, GapicServiceConfig serviceConfig, Map<String, VariableExpr> methodSettingsMemberVarExprs, Map<String, Message> messageTypes, TypeStore typeStore) {
Function<Expr, Statement> exprToStatementFn = e -> ExprStatement.withExpr(e);
List<Statement> statements = new ArrayList<>();
// Assign DEFAULT_SERVICE_SCOPES.
statements.add(SettingsCommentComposer.DEFAULT_SCOPES_COMMENT);
VariableExpr defaultServiceScopesDeclVarExpr = DEFAULT_SERVICE_SCOPES_VAR_EXPR.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build();
MethodInvocationExpr listBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableList")).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class))).setMethodName("builder").build();
for (String serviceScope : service.oauthScopes()) {
listBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(listBuilderExpr).setMethodName("add").setArguments(ValueExpr.withValue(StringObjectValue.withValue(serviceScope))).build();
}
listBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(listBuilderExpr).setMethodName("build").setReturnType(DEFAULT_SERVICE_SCOPES_VAR_EXPR.type()).build();
statements.add(exprToStatementFn.apply(AssignmentExpr.builder().setVariableExpr(defaultServiceScopesDeclVarExpr).setValueExpr(listBuilderExpr).build()));
// Declare settings members.
statements.addAll(methodSettingsMemberVarExprs.values().stream().map(v -> exprToStatementFn.apply(v.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsFinal(true).build())).collect(Collectors.toList()));
statements.add(EMPTY_LINE_STATEMENT);
for (Expr pagingAssignExpr : createPagingStaticAssignExprs(service, serviceConfig, messageTypes, typeStore)) {
statements.add(exprToStatementFn.apply(pagingAssignExpr));
statements.add(EMPTY_LINE_STATEMENT);
}
for (Method method : service.methods()) {
Optional<GapicBatchingSettings> batchingSettingOpt = Objects.isNull(serviceConfig) ? Optional.empty() : serviceConfig.getBatchingSetting(service, method);
if (batchingSettingOpt.isPresent()) {
statements.add(exprToStatementFn.apply(BatchingDescriptorComposer.createBatchingDescriptorFieldDeclExpr(method, batchingSettingOpt.get(), messageTypes)));
}
statements.add(EMPTY_LINE_STATEMENT);
}
return statements;
}
use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createMethodSettingsClassMemberVarExprs.
private static Map<String, VariableExpr> createMethodSettingsClassMemberVarExprs(Service service, GapicServiceConfig serviceConfig, TypeStore typeStore, boolean isNestedClass, Set<String> deprecatedSettingVarNames) {
// Maintain insertion order.
Map<String, VariableExpr> varExprs = new LinkedHashMap<>();
// Creates class variables <method>Settings, e.g. echoSettings.
for (Method method : service.methods()) {
boolean hasBatchingSettings = !Objects.isNull(serviceConfig) && serviceConfig.hasBatchingSetting(service, method);
TypeNode settingsType = getCallSettingsType(method, typeStore, hasBatchingSettings, isNestedClass);
String varName = String.format("%sSettings", JavaStyle.toLowerCamelCase(method.name()));
if (method.isDeprecated()) {
deprecatedSettingVarNames.add(varName);
}
varExprs.put(varName, VariableExpr.withVariable(Variable.builder().setType(settingsType).setName(varName).build()));
if (method.hasLro()) {
settingsType = getOperationCallSettingsType(method, isNestedClass);
varName = JavaStyle.toLowerCamelCase(String.format("%sOperationSettings", method.name()));
varExprs.put(varName, VariableExpr.withVariable(Variable.builder().setType(settingsType).setName(varName).build()));
}
}
return varExprs;
}
use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method getCallSettingsType.
private static TypeNode getCallSettingsType(Method method, TypeStore typeStore, boolean isBatchingSettings, final boolean isSettingsBuilder) {
Function<Class<?>, TypeNode> typeMakerFn = clz -> TypeNode.withReference(ConcreteReference.withClazz(clz));
// Default: No streaming.
TypeNode callSettingsType = method.isPaged() ? typeMakerFn.apply(isSettingsBuilder ? PagedCallSettings.Builder.class : PagedCallSettings.class) : typeMakerFn.apply(isSettingsBuilder ? UnaryCallSettings.Builder.class : UnaryCallSettings.class);
if (isBatchingSettings) {
callSettingsType = typeMakerFn.apply(isSettingsBuilder ? BatchingCallSettings.Builder.class : BatchingCallSettings.class);
}
// Streaming takes precedence over paging, as per the monolith's existing behavior.
switch(method.stream()) {
case SERVER:
callSettingsType = typeMakerFn.apply(isSettingsBuilder ? ServerStreamingCallSettings.Builder.class : ServerStreamingCallSettings.class);
break;
case CLIENT:
// Fall through.
case BIDI:
callSettingsType = typeMakerFn.apply(isSettingsBuilder ? StreamingCallSettings.Builder.class : StreamingCallSettings.class);
break;
case NONE:
// Fall through.
default:
break;
}
List<Reference> generics = new ArrayList<>();
generics.add(method.inputType().reference());
generics.add(method.outputType().reference());
if (method.isPaged()) {
generics.add(typeStore.get(getPagedResponseTypeName(method.name())).reference());
}
return TypeNode.withReference(callSettingsType.reference().copyAndSetGenerics(generics));
}
Aggregations