use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createSettingsGetterMethods.
// TODO(miraleung): Consider merging this with createNestedBuilderSettingsGetterMethods.
private static List<MethodDefinition> createSettingsGetterMethods(Service service, TypeStore typeStore) {
TypeNode stubSettingsType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
BiFunction<TypeNode, String, MethodDefinition.Builder> methodMakerFn = (retType, javaMethodName) -> MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(retType).setName(javaMethodName).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(CastExpr.builder().setType(stubSettingsType).setExpr(MethodInvocationExpr.builder().setMethodName("getStubSettings").setReturnType(FIXED_TYPESTORE.get("StubSettings")).build()).build()).setMethodName(javaMethodName).setReturnType(retType).build());
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Method protoMethod : service.methods()) {
String javaStyleName = JavaStyle.toLowerCamelCase(protoMethod.name());
String javaMethodName = String.format("%sSettings", JavaStyle.toLowerCamelCase(protoMethod.name()));
MethodDefinition.Builder methodBuilder = methodMakerFn.apply(getCallSettingsType(protoMethod, typeStore), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsGetterComment(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(getOperationCallSettingsType(protoMethod), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsGetterComment(getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated())).setAnnotations(protoMethod.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList()).build());
}
}
return javaMethods;
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class RetrySettingsComposer method createLroSettingsBuilderExpr.
public static Expr createLroSettingsBuilderExpr(Service service, GapicServiceConfig serviceConfig, Method method, VariableExpr builderVarExpr, VariableExpr retryableCodeDefsVarExpr, VariableExpr retryParamDefsVarExpr, TypeNode operationResponseTransformer, TypeNode operationMetadataTransformer) {
Preconditions.checkState(method.hasLro(), String.format("Tried to create LRO settings initialization for non-LRO method %s", method.name()));
String codeName = serviceConfig.getRetryCodeName(service, method);
String retryParamName = serviceConfig.getRetryParamsName(service, method);
String settingsGetterMethodName = String.format("%sOperationSettings", JavaStyle.toLowerCamelCase(method.name()));
Function<String, ValueExpr> strValExprFn = s -> ValueExpr.withValue(StringObjectValue.withValue(s));
// Argument for setInitialCallSettings.
Expr unaryCallSettingsExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("UnaryCallSettings")).setGenerics(Arrays.asList(method.inputType().reference(), FIXED_TYPESTORE.get("OperationSnapshot").reference())).setMethodName("newUnaryCallSettingsBuilder").build();
unaryCallSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(unaryCallSettingsExpr).setMethodName("setRetryableCodes").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(retryableCodeDefsVarExpr).setMethodName("get").setArguments(strValExprFn.apply(codeName)).build()).build();
unaryCallSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(unaryCallSettingsExpr).setMethodName("setRetrySettings").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(retryParamDefsVarExpr).setMethodName("get").setArguments(strValExprFn.apply(retryParamName)).build()).build();
unaryCallSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(unaryCallSettingsExpr).setMethodName("build").build();
Expr builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName(settingsGetterMethodName).build();
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setInitialCallSettings").setArguments(unaryCallSettingsExpr).build();
Function<TypeNode, VariableExpr> classFieldRefFn = t -> VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build()).setStaticReferenceType(t).build();
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setResponseTransformer").setArguments(MethodInvocationExpr.builder().setStaticReferenceType(operationResponseTransformer).setMethodName("create").setArguments(classFieldRefFn.apply(method.lro().responseType())).build()).build();
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setMetadataTransformer").setArguments(MethodInvocationExpr.builder().setStaticReferenceType(operationMetadataTransformer).setMethodName("create").setArguments(classFieldRefFn.apply(method.lro().metadataType())).build()).build();
Expr lroRetrySettingsExpr = createLroRetrySettingsExpr(service, method, serviceConfig);
Expr pollAlgoExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("OperationTimedPollAlgorithm")).setMethodName("create").setArguments(lroRetrySettingsExpr).build();
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setPollingAlgorithm").setArguments(pollAlgoExpr).build();
return builderSettingsExpr;
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class RetrySettingsComposer method createSimpleBuilderSettingsExpr.
public static Expr createSimpleBuilderSettingsExpr(Service service, GapicServiceConfig serviceConfig, Method method, VariableExpr builderVarExpr, VariableExpr retryableCodeDefsVarExpr, VariableExpr retryParamDefsVarExpr) {
String codeName = serviceConfig.getRetryCodeName(service, method);
String retryParamName = serviceConfig.getRetryParamsName(service, method);
String settingsGetterMethodName = String.format("%sSettings", JavaStyle.toLowerCamelCase(method.name()));
Expr builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName(settingsGetterMethodName).build();
Function<String, ValueExpr> strValExprFn = s -> ValueExpr.withValue(StringObjectValue.withValue(s));
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setRetryableCodes").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(retryableCodeDefsVarExpr).setMethodName("get").setArguments(strValExprFn.apply(codeName)).build()).build();
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setRetrySettings").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(retryParamDefsVarExpr).setMethodName("get").setArguments(strValExprFn.apply(retryParamName)).build()).build();
return builderSettingsExpr;
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createStartStaticServerMethod.
@Override
protected MethodDefinition createStartStaticServerMethod(Service service, GapicContext context, Map<String, VariableExpr> classMemberVarExprs, TypeStore typeStore, String newBuilderMethod) {
VariableExpr serviceHelperVarExpr = classMemberVarExprs.get(SERVICE_HELPER_VAR_NAME);
Function<Service, VariableExpr> serviceToVarExprFn = s -> classMemberVarExprs.get(getMockServiceVarName(s));
Function<Service, Expr> serviceToVarInitExprFn = s -> {
VariableExpr mockServiceVarExpr = serviceToVarExprFn.apply(s);
return AssignmentExpr.builder().setVariableExpr(mockServiceVarExpr).setValueExpr(NewObjectExpr.builder().setType(mockServiceVarExpr.type()).build()).build();
};
List<Expr> varInitExprs = new ArrayList<>();
List<Expr> mockServiceVarExprs = new ArrayList<>();
varInitExprs.add(serviceToVarInitExprFn.apply(service));
mockServiceVarExprs.add(serviceToVarExprFn.apply(service));
// Context: https://github.com/googleapis/gapic-generator-java/pull/750
for (Service mixinService : context.mixinServices().stream().sorted((s1, s2) -> s2.name().compareTo(s1.name())).collect(Collectors.toList())) {
varInitExprs.add(serviceToVarInitExprFn.apply(mixinService));
mockServiceVarExprs.add(serviceToVarExprFn.apply(mixinService));
}
MethodInvocationExpr firstArg = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("UUID")).setMethodName("randomUUID").build();
firstArg = MethodInvocationExpr.builder().setExprReferenceExpr(firstArg).setMethodName("toString").build();
MethodInvocationExpr secondArg = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Arrays")).setGenerics(Arrays.asList(FIXED_GRPC_TYPESTORE.get("MockGrpcService").reference())).setMethodName("asList").setArguments(mockServiceVarExprs).build();
Expr initServiceHelperExpr = AssignmentExpr.builder().setVariableExpr(serviceHelperVarExpr).setValueExpr(NewObjectExpr.builder().setType(serviceHelperVarExpr.type()).setArguments(Arrays.asList(firstArg, secondArg)).build()).build();
Expr startServiceHelperExpr = MethodInvocationExpr.builder().setExprReferenceExpr(serviceHelperVarExpr).setMethodName("start").build();
varInitExprs.add(initServiceHelperExpr);
varInitExprs.add(startServiceHelperExpr);
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.withType(FIXED_TYPESTORE.get("BeforeClass")))).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(TypeNode.VOID).setName("startStaticServer").setBody(varInitExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createClassMemberVarExprs.
@Override
protected Map<String, VariableExpr> createClassMemberVarExprs(Service service, GapicContext context, TypeStore typeStore) {
BiFunction<String, TypeNode, VariableExpr> varExprFn = (name, type) -> VariableExpr.withVariable(Variable.builder().setName(name).setType(type).build());
// Keep keys sorted in alphabetical order to ensure that the test output is deterministic.
Map<String, TypeNode> fields = new TreeMap<>();
fields.put(getMockServiceVarName(service), typeStore.get(ClassNames.getMockServiceClassName(service)));
for (Service mixinService : context.mixinServices()) {
fields.put(getMockServiceVarName(mixinService), typeStore.get(ClassNames.getMockServiceClassName(mixinService)));
}
fields.put(SERVICE_HELPER_VAR_NAME, FIXED_GRPC_TYPESTORE.get("MockServiceHelper"));
fields.put(CLIENT_VAR_NAME, typeStore.get(ClassNames.getServiceClientClassName(service)));
fields.put(CHANNEL_PROVIDER_VAR_NAME, FIXED_GRPC_TYPESTORE.get("LocalChannelProvider"));
return fields.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> varExprFn.apply(e.getKey(), e.getValue()), (u, v) -> {
throw new IllegalStateException();
}, TreeMap::new));
}
Aggregations