Search in sources :

Example 36 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class DefaultValueComposer method createResourceHelperValue.

@VisibleForTesting
static Expr createResourceHelperValue(ResourceName resourceName, boolean isChildType, List<ResourceName> resnames, String fieldOrMessageName, boolean allowAnonResourceNameClass) {
    if (isChildType) {
        resourceName = findParentResource(resourceName, resnames).orElse(resourceName);
    }
    if (resourceName.isOnlyWildcard()) {
        List<ResourceName> unexaminedResnames = new ArrayList<>(resnames);
        for (ResourceName resname : resnames) {
            unexaminedResnames.remove(resname);
            if (!resname.isOnlyWildcard()) {
                return createResourceHelperValue(resname, false, unexaminedResnames, fieldOrMessageName);
            }
        }
        return allowAnonResourceNameClass ? createAnonymousResourceNameClassValue(fieldOrMessageName) : ValueExpr.withValue(StringObjectValue.withValue(String.format("%s%s", fieldOrMessageName, fieldOrMessageName.hashCode())));
    }
    // The cost tradeoffs of new ctors versus distinct() don't really matter here, since this list
    // will usually have a very small number of elements.
    List<String> patterns = new ArrayList<>(new HashSet<>(resourceName.patterns()));
    boolean containsOnlyDeletedTopic = patterns.size() == 1 && patterns.get(0).equals(ResourceNameConstants.DELETED_TOPIC_LITERAL);
    String ofMethodName = "of";
    List<String> patternPlaceholderTokens = new ArrayList<>();
    if (containsOnlyDeletedTopic) {
        ofMethodName = "ofDeletedTopic";
    } else {
        for (String pattern : resourceName.patterns()) {
            if (pattern.equals(ResourceNameConstants.WILDCARD_PATTERN) || pattern.equals(ResourceNameConstants.DELETED_TOPIC_LITERAL)) {
                continue;
            }
            patternPlaceholderTokens.addAll(ResourceNameTokenizer.parseTokenHierarchy(Arrays.asList(pattern)).get(0));
            break;
        }
    }
    boolean hasOnePattern = resourceName.patterns().size() == 1;
    if (!hasOnePattern) {
        ofMethodName = String.format("of%sName", String.join("", patternPlaceholderTokens.stream().map(s -> JavaStyle.toUpperCamelCase(s)).collect(Collectors.toList())));
    }
    TypeNode resourceNameJavaType = resourceName.type();
    List<Expr> argExprs = patternPlaceholderTokens.stream().map(s -> ValueExpr.withValue(StringObjectValue.withValue(String.format("[%s]", s.toUpperCase())))).collect(Collectors.toList());
    return MethodInvocationExpr.builder().setStaticReferenceType(resourceNameJavaType).setMethodName(ofMethodName).setArguments(argExprs).setReturnType(resourceNameJavaType).build();
}
Also used : VaporReference(com.google.api.generator.engine.ast.VaporReference) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) HashMap(java.util.HashMap) Operation(com.google.longrunning.Operation) Variable(com.google.api.generator.engine.ast.Variable) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) HashSet(java.util.HashSet) Expr(com.google.api.generator.engine.ast.Expr) Field(com.google.api.generator.gapic.model.Field) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) ResourceReferenceUtils(com.google.api.generator.gapic.utils.ResourceReferenceUtils) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ResourceNameTokenizer(com.google.api.generator.gapic.composer.resourcename.ResourceNameTokenizer) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Collectors(java.util.stream.Collectors) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) ResourceName(com.google.api.generator.gapic.model.ResourceName) ResourceNameConstants(com.google.api.generator.gapic.utils.ResourceNameConstants) List(java.util.List) 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) Any(com.google.protobuf.Any) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Pattern(java.util.regex.Pattern) Message(com.google.api.generator.gapic.model.Message) Collections(java.util.Collections) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) 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) ResourceName(com.google.api.generator.gapic.model.ResourceName) ArrayList(java.util.ArrayList) TypeNode(com.google.api.generator.engine.ast.TypeNode) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 37 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class GrpcServiceStubClassComposer method createRequestParamsExtractorBodyForHttpBindings.

private void createRequestParamsExtractorBodyForHttpBindings(Method method, VariableExpr requestVarExpr, List<Statement> bodyStatements, MethodInvocationExpr.Builder returnExprBuilder) {
    TypeNode paramsVarType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(TypeNode.STRING.reference(), TypeNode.STRING.reference()).build());
    VariableExpr paramsVarExpr = VariableExpr.withVariable(Variable.builder().setName("params").setType(paramsVarType).build());
    Expr paramsAssignExpr = AssignmentExpr.builder().setVariableExpr(paramsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(paramsVarType).build()).build();
    bodyStatements.add(ExprStatement.withExpr(paramsAssignExpr));
    for (HttpBinding httpBindingFieldBinding : method.httpBindings().pathParameters()) {
        MethodInvocationExpr requestBuilderExpr = createRequestFieldGetterExpr(requestVarExpr, httpBindingFieldBinding.name());
        Expr valueOfExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.STRING).setMethodName("valueOf").setArguments(requestBuilderExpr).build();
        Expr paramsPutExpr = MethodInvocationExpr.builder().setExprReferenceExpr(paramsVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(StringObjectValue.withValue(httpBindingFieldBinding.name())), valueOfExpr).build();
        bodyStatements.add(ExprStatement.withExpr(paramsPutExpr));
    }
    returnExprBuilder.setExprReferenceExpr(paramsVarExpr).setMethodName("build");
}
Also used : EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Expr(com.google.api.generator.engine.ast.Expr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) RequestParamsBuilder(com.google.api.gax.rpc.RequestParamsBuilder) HttpBinding(com.google.api.generator.gapic.model.HttpBindings.HttpBinding) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 38 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class GrpcServiceStubClassComposer method createMethodDescriptorVariableDecl.

@Override
protected Statement createMethodDescriptorVariableDecl(Service service, Method protoMethod, VariableExpr methodDescriptorVarExpr, Map<String, Message> messageTypes) {
    MethodInvocationExpr methodDescriptorMaker = MethodInvocationExpr.builder().setMethodName("newBuilder").setStaticReferenceType(FIXED_GRPC_TYPE_STORE.get("MethodDescriptor")).setGenerics(methodDescriptorVarExpr.variable().type().reference().generics()).build();
    BiFunction<String, Expr, Function<MethodInvocationExpr, MethodInvocationExpr>> methodMakerFn = (mName, argExpr) -> m -> MethodInvocationExpr.builder().setMethodName(mName).setArguments(Arrays.asList(argExpr)).setExprReferenceExpr(m).build();
    methodDescriptorMaker = methodMakerFn.apply("setType", getMethodDescriptorMethodTypeExpr(protoMethod)).apply(methodDescriptorMaker);
    String codeMethodNameArg = getProtoRpcFullMethodName(service, protoMethod);
    methodDescriptorMaker = methodMakerFn.apply("setFullMethodName", ValueExpr.withValue(StringObjectValue.withValue(codeMethodNameArg))).apply(methodDescriptorMaker);
    Function<MethodInvocationExpr, MethodInvocationExpr> protoUtilsMarshallerFn = m -> MethodInvocationExpr.builder().setStaticReferenceType(FIXED_GRPC_TYPE_STORE.get("ProtoUtils")).setMethodName("marshaller").setArguments(Arrays.asList(m)).build();
    MethodInvocationExpr methodInvocationArg = MethodInvocationExpr.builder().setMethodName("getDefaultInstance").setStaticReferenceType(protoMethod.inputType()).build();
    methodDescriptorMaker = methodMakerFn.apply("setRequestMarshaller", protoUtilsMarshallerFn.apply(methodInvocationArg)).apply(methodDescriptorMaker);
    methodInvocationArg = MethodInvocationExpr.builder().setMethodName("getDefaultInstance").setStaticReferenceType(protoMethod.outputType()).build();
    methodDescriptorMaker = methodMakerFn.apply("setResponseMarshaller", protoUtilsMarshallerFn.apply(methodInvocationArg)).apply(methodDescriptorMaker);
    methodDescriptorMaker = MethodInvocationExpr.builder().setMethodName("build").setExprReferenceExpr(methodDescriptorMaker).setReturnType(methodDescriptorVarExpr.type()).build();
    return ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(methodDescriptorVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(methodDescriptorMaker).build());
}
Also used : AbstractTransportServiceStubClassComposer(com.google.api.generator.gapic.composer.common.AbstractTransportServiceStubClassComposer) IfStatement(com.google.api.generator.engine.ast.IfStatement) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) GrpcStubCallableFactory(com.google.api.gax.grpc.GrpcStubCallableFactory) BiFunction(java.util.function.BiFunction) PathTemplate(com.google.api.pathtemplate.PathTemplate) Variable(com.google.api.generator.engine.ast.Variable) Function(java.util.function.Function) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) ArrayList(java.util.ArrayList) HttpBinding(com.google.api.generator.gapic.model.HttpBindings.HttpBinding) HashSet(java.util.HashSet) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) 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) ImmutableList(com.google.common.collect.ImmutableList) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) Map(java.util.Map) MethodDescriptor(io.grpc.MethodDescriptor) Splitter(com.google.common.base.Splitter) ProtoUtils(io.grpc.protobuf.ProtoUtils) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) RequestParamsBuilder(com.google.api.gax.rpc.RequestParamsBuilder) Statement(com.google.api.generator.engine.ast.Statement) Service(com.google.api.generator.gapic.model.Service) List(java.util.List) GrpcCallSettings(com.google.api.gax.grpc.GrpcCallSettings) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) GrpcOperationsStub(com.google.longrunning.stub.GrpcOperationsStub) Message(com.google.api.generator.gapic.model.Message) RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Expr(com.google.api.generator.engine.ast.Expr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr)

Example 39 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class MockServiceClassComposer method createGetRequestsMethod.

private static MethodDefinition createGetRequestsMethod(VariableExpr serviceImplVarExpr, TypeStore typeStore) {
    TypeNode returnType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(typeStore.get("AbstractMessage").reference())).build());
    String methodName = "getRequests";
    Expr returnExpr = MethodInvocationExpr.builder().setMethodName(methodName).setReturnType(returnType).setExprReferenceExpr(serviceImplVarExpr).build();
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(returnType).setName(methodName).setReturnExpr(returnExpr).build();
}
Also used : 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) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) List(java.util.List) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 40 with Expr

use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.

the class MockServiceClassComposer method createConstructor.

private static MethodDefinition createConstructor(Service service, VariableExpr serviceImplVarExpr, TypeStore typeStore) {
    String mockImplName = ClassNames.getMockServiceImplClassName(service);
    Expr serviceImplAssignExpr = AssignmentExpr.builder().setVariableExpr(serviceImplVarExpr).setValueExpr(NewObjectExpr.builder().setType(typeStore.get(mockImplName)).build()).build();
    return MethodDefinition.constructorBuilder().setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(serviceImplAssignExpr))).setReturnType(typeStore.get(ClassNames.getMockServiceClassName(service))).build();
}
Also used : 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) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr)

Aggregations

Expr (com.google.api.generator.engine.ast.Expr)151 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)130 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)126 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)124 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)114 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)96 TypeNode (com.google.api.generator.engine.ast.TypeNode)80 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)72 ArrayList (java.util.ArrayList)72 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)67 Statement (com.google.api.generator.engine.ast.Statement)64 CastExpr (com.google.api.generator.engine.ast.CastExpr)59 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)59 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)58 List (java.util.List)58 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)55 Variable (com.google.api.generator.engine.ast.Variable)54 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)51 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)49 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)47