use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractServiceClientTestClassComposer method createTestMethods.
private List<MethodDefinition> createTestMethods(Service service, GapicContext context, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames) {
Map<String, Message> messageTypes = context.messages();
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Method method : service.methods()) {
Service matchingService = service;
if (method.isMixin()) {
int dotIndex = method.mixedInApiName().lastIndexOf(".");
String mixinServiceName = method.mixedInApiName().substring(dotIndex + 1);
String mixinServiceProtoPackage = method.mixedInApiName().substring(0, dotIndex);
Optional<Service> mixinServiceOpt = context.mixinServices().stream().filter(s -> s.name().equals(mixinServiceName) && s.protoPakkage().equals(mixinServiceProtoPackage)).findFirst();
if (mixinServiceOpt.isPresent()) {
matchingService = mixinServiceOpt.get();
}
}
// Ignore variants for streaming methods as well.
if (method.methodSignatures().isEmpty() || !method.stream().equals(Method.Stream.NONE)) {
javaMethods.add(createRpcTestMethod(method, service, matchingService, Collections.emptyList(), 0, true, classMemberVarExprs, resourceNames, messageTypes));
javaMethods.add(createRpcExceptionTestMethod(method, matchingService, Collections.emptyList(), 0, classMemberVarExprs, resourceNames, messageTypes));
} else {
for (int i = 0; i < method.methodSignatures().size(); i++) {
javaMethods.add(createRpcTestMethod(method, service, matchingService, method.methodSignatures().get(i), i, false, classMemberVarExprs, resourceNames, messageTypes));
javaMethods.add(createRpcExceptionTestMethod(method, matchingService, method.methodSignatures().get(i), i, classMemberVarExprs, resourceNames, messageTypes));
}
}
}
return javaMethods;
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createDynamicTypes.
private TypeStore createDynamicTypes(Service service, String pakkage) {
TypeStore typeStore = new TypeStore();
// This type.
String thisClassName = ClassNames.getServiceStubSettingsClassName(service);
typeStore.putAll(pakkage, Arrays.asList(thisClassName, ClassNames.getServiceStubSettingsClassName(service), ClassNames.getServiceStubClassName(service)));
typeStore.putAll(pakkage, getTransportContext().classNames().getTransportServiceStubClassNames(service));
// Nested builder class.
typeStore.put(pakkage, NESTED_BUILDER_CLASS_NAME, true, thisClassName);
// Pagination types.
typeStore.putAll(service.pakkage(), service.methods().stream().filter(m -> m.isPaged()).map(m -> String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name())).collect(Collectors.toList()), true, ClassNames.getServiceClientClassName(service));
return typeStore;
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createClassConstructor.
private static MethodDefinition createClassConstructor(Service service, Map<String, VariableExpr> methodSettingsMemberVarExprs, TypeStore typeStore) {
TypeNode thisType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
final VariableExpr settingsBuilderVarExpr = VariableExpr.withVariable(Variable.builder().setType(typeStore.get(NESTED_BUILDER_CLASS_NAME)).setName("settingsBuilder").build());
Expr superCtorExpr = ReferenceConstructorExpr.superBuilder().setType(FIXED_TYPESTORE.get("StubSettings")).setArguments(settingsBuilderVarExpr).build();
List<Statement> bodyStatements = new ArrayList<>();
bodyStatements.add(ExprStatement.withExpr(superCtorExpr));
bodyStatements.add(EMPTY_LINE_STATEMENT);
Function<Map.Entry<String, VariableExpr>, AssignmentExpr> varInitExprFn = e -> AssignmentExpr.builder().setVariableExpr(e.getValue()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderVarExpr).setMethodName(e.getKey()).build()).setMethodName("build").setReturnType(e.getValue().type()).build()).build();
bodyStatements.addAll(methodSettingsMemberVarExprs.entrySet().stream().map(e -> ExprStatement.withExpr(varInitExprFn.apply(e))).collect(Collectors.toList()));
return MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(thisType).setArguments(settingsBuilderVarExpr.toBuilder().setIsDecl(true).build()).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class))).setBody(bodyStatements).build();
}
use of com.google.api.generator.gapic.model.Service 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.Service in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createNestedBuilderSettingsGetterMethods.
private static List<MethodDefinition> createNestedBuilderSettingsGetterMethods(Service service, TypeStore typeStore) {
BiFunction<TypeNode, String, MethodDefinition.Builder> methodMakerFn = (retType, methodName) -> MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(retType).setName(methodName).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("getStubSettingsBuilder").build()).setMethodName(methodName).setReturnType(retType).build());
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Method protoMethod : service.methods()) {
String javaStyleName = JavaStyle.toLowerCamelCase(protoMethod.name());
String javaMethodName = String.format("%sSettings", javaStyleName);
MethodDefinition.Builder methodBuilder = methodMakerFn.apply(getCallSettingsBuilderType(protoMethod, typeStore), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsBuilderGetterComment(getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated())).setAnnotations(protoMethod.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList()).build());
if (protoMethod.hasLro()) {
javaMethodName = String.format("%sOperationSettings", javaStyleName);
methodBuilder = methodMakerFn.apply(getOperationCallSettingsBuilderType(protoMethod), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsBuilderGetterComment(getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated())).setAnnotations(protoMethod.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList()).build());
}
}
return javaMethods;
}
Aggregations