Search in sources :

Example 26 with TypeNode

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;
}
Also used : Field(com.google.api.generator.gapic.model.Field) Message(com.google.api.generator.gapic.model.Message) ArrayList(java.util.ArrayList) Method(com.google.api.generator.gapic.model.Method) TypeNode(com.google.api.generator.engine.ast.TypeNode) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition)

Example 27 with TypeNode

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);
}
Also used : Sample(com.google.api.generator.gapic.model.Sample) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 28 with TypeNode

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();
}
Also used : BlockStatement(com.google.api.generator.engine.ast.BlockStatement) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) GapicLroRetrySettings(com.google.api.generator.gapic.model.GapicLroRetrySettings) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) ProtoOperationTransformers(com.google.api.gax.grpc.ProtoOperationTransformers) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) FlowController(com.google.api.gax.batching.FlowController) RetryPolicy(io.grpc.serviceconfig.MethodConfig.RetryPolicy) Variable(com.google.api.generator.engine.ast.Variable) Function(java.util.function.Function) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) Durations(com.google.protobuf.util.Durations) ArrayList(java.util.ArrayList) RetrySettings(com.google.api.gax.retrying.RetrySettings) UnaryCallSettings(com.google.api.gax.rpc.UnaryCallSettings) Expr(com.google.api.generator.engine.ast.Expr) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) Lists(com.google.common.collect.Lists) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) Code(com.google.rpc.Code) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BatchingSettings(com.google.api.gax.batching.BatchingSettings) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) Collectors(java.util.stream.Collectors) GapicRetrySettings(com.google.api.generator.gapic.model.GapicRetrySettings) Service(com.google.api.generator.gapic.model.Service) Duration(com.google.protobuf.Duration) List(java.util.List) OperationTimedPollAlgorithm(com.google.api.gax.longrunning.OperationTimedPollAlgorithm) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) Optional(java.util.Optional) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) StatusCode(com.google.api.gax.rpc.StatusCode) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ArrayList(java.util.ArrayList) List(java.util.List) TypeNode(com.google.api.generator.engine.ast.TypeNode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 29 with TypeNode

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();
}
Also used : BlockStatement(com.google.api.generator.engine.ast.BlockStatement) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) GapicLroRetrySettings(com.google.api.generator.gapic.model.GapicLroRetrySettings) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) ProtoOperationTransformers(com.google.api.gax.grpc.ProtoOperationTransformers) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) FlowController(com.google.api.gax.batching.FlowController) RetryPolicy(io.grpc.serviceconfig.MethodConfig.RetryPolicy) Variable(com.google.api.generator.engine.ast.Variable) Function(java.util.function.Function) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) Durations(com.google.protobuf.util.Durations) ArrayList(java.util.ArrayList) RetrySettings(com.google.api.gax.retrying.RetrySettings) UnaryCallSettings(com.google.api.gax.rpc.UnaryCallSettings) Expr(com.google.api.generator.engine.ast.Expr) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) Lists(com.google.common.collect.Lists) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) Code(com.google.rpc.Code) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BatchingSettings(com.google.api.gax.batching.BatchingSettings) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) Collectors(java.util.stream.Collectors) GapicRetrySettings(com.google.api.generator.gapic.model.GapicRetrySettings) Service(com.google.api.generator.gapic.model.Service) Duration(com.google.protobuf.Duration) List(java.util.List) OperationTimedPollAlgorithm(com.google.api.gax.longrunning.OperationTimedPollAlgorithm) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) Optional(java.util.Optional) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) StatusCode(com.google.api.gax.rpc.StatusCode) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) GapicRetrySettings(com.google.api.generator.gapic.model.GapicRetrySettings) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 30 with TypeNode

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();
}
Also used : Field(com.google.api.generator.gapic.model.Field) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Aggregations

TypeNode (com.google.api.generator.engine.ast.TypeNode)232 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)119 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)111 Test (org.junit.Test)103 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)84 Message (com.google.api.generator.gapic.model.Message)81 ArrayList (java.util.ArrayList)81 Expr (com.google.api.generator.engine.ast.Expr)77 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)77 Method (com.google.api.generator.gapic.model.Method)72 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)68 ResourceName (com.google.api.generator.gapic.model.ResourceName)65 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)64 List (java.util.List)63 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)61 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)59 Variable (com.google.api.generator.engine.ast.Variable)55 Statement (com.google.api.generator.engine.ast.Statement)53 Collectors (java.util.stream.Collectors)46 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)45