use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createNestedPagingClasses.
private static List<ClassDefinition> createNestedPagingClasses(Service service, Map<String, Message> messageTypes, TypeStore typeStore) {
List<ClassDefinition> nestedClasses = new ArrayList<>();
for (Method method : service.methods()) {
if (!method.isPaged()) {
continue;
}
// Find the repeated field.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapPaginatedRepeatedField();
Preconditions.checkNotNull(repeatedPagedResultsField, String.format("No repeated field found on message %s for method %s", methodOutputMessage.name(), method.name()));
TypeNode repeatedResponseType = repeatedPagedResultsField.type();
nestedClasses.add(createNestedRpcPagedResponseClass(method, repeatedResponseType, messageTypes, typeStore));
nestedClasses.add(createNestedRpcPageClass(method, repeatedResponseType, messageTypes, typeStore));
nestedClasses.add(createNestedRpcFixedSizeCollectionClass(method, repeatedResponseType, messageTypes, typeStore));
}
return nestedClasses;
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createClassHeaderComments.
protected List<CommentStatement> createClassHeaderComments(Service service, TypeStore typeStore, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes, List<Sample> samples) {
TypeNode clientType = typeStore.get(ClassNames.getServiceClientClassName(service));
TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
Sample classMethodSampleCode = ServiceClientHeaderSampleComposer.composeClassHeaderSample(service, clientType, resourceNames, messageTypes);
Sample credentialsSampleCode = ServiceClientHeaderSampleComposer.composeSetCredentialsSample(clientType, settingsType);
Sample endpointSampleCode = ServiceClientHeaderSampleComposer.composeSetEndpointSample(clientType, settingsType);
samples.addAll(Arrays.asList(classMethodSampleCode, credentialsSampleCode, endpointSampleCode));
return ServiceClientCommentComposer.createClassHeaderComments(service, SampleCodeWriter.writeInlineSample(classMethodSampleCode.body()), SampleCodeWriter.writeInlineSample(credentialsSampleCode.body()), SampleCodeWriter.writeInlineSample(endpointSampleCode.body()), null, null, null);
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class RetrySettingsComposer method createRetryCodesDefinitionsBlock.
public static BlockStatement createRetryCodesDefinitionsBlock(Service service, GapicServiceConfig serviceConfig, VariableExpr retryCodesDefinitionsClassMemberVarExpr) {
TypeNode definitionsType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(retryCodesDefinitionsClassMemberVarExpr.type().reference().generics()).build());
VariableExpr definitionsVarExpr = VariableExpr.withVariable(Variable.builder().setType(definitionsType).setName("definitions").build());
List<Expr> bodyExprs = new ArrayList<>();
// Create the first expr.
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(definitionsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(definitionsVarExpr.type()).build()).build());
for (Map.Entry<String, List<Code>> codeEntry : serviceConfig.getAllRetryCodes(service).entrySet()) {
bodyExprs.add(createRetryCodeDefinitionExpr(codeEntry.getKey(), codeEntry.getValue(), definitionsVarExpr));
}
// Reassign the new codes.
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(retryCodesDefinitionsClassMemberVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(definitionsVarExpr).setMethodName("build").setReturnType(retryCodesDefinitionsClassMemberVarExpr.type()).build()).build());
// Put everything together.
return BlockStatement.builder().setIsStatic(true).setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class RetrySettingsComposer method createRetryParamDefinitionsBlock.
public static BlockStatement createRetryParamDefinitionsBlock(Service service, GapicServiceConfig serviceConfig, VariableExpr retryParamDefinitionsClassMemberVarExpr) {
List<Expr> bodyExprs = new ArrayList<>();
TypeNode definitionsType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(retryParamDefinitionsClassMemberVarExpr.type().reference().generics()).build());
VariableExpr definitionsVarExpr = VariableExpr.withVariable(Variable.builder().setType(definitionsType).setName("definitions").build());
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("RetrySettings")).setName("settings").build());
// Create the first two exprs.
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(definitionsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(definitionsVarExpr.type()).build()).build());
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(ValueExpr.createNullExpr()).build());
// Build the settings object for each config.
for (Map.Entry<String, GapicRetrySettings> settingsEntry : serviceConfig.getAllGapicRetrySettings(service).entrySet()) {
bodyExprs.addAll(createRetrySettingsExprs(settingsEntry.getKey(), settingsEntry.getValue(), settingsVarExpr, definitionsVarExpr));
}
// Reassign the new settings.
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(retryParamDefinitionsClassMemberVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(definitionsVarExpr).setMethodName("build").setReturnType(retryParamDefinitionsClassMemberVarExpr.type()).build()).build());
// Put everything together.
return BlockStatement.builder().setIsStatic(true).setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
use of com.google.api.generator.engine.ast.TypeNode 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();
}
Aggregations