Search in sources :

Example 46 with TypeStore

use of com.google.api.generator.gapic.composer.store.TypeStore in project gapic-generator-java by googleapis.

the class ResourceNameHelperClassComposer method createDynamicTypes.

private static TypeStore createDynamicTypes(ResourceName resourceName, List<List<String>> tokenHierarchies) {
    String thisClassName = getThisClassName(resourceName);
    TypeStore typeStore = new TypeStore();
    typeStore.put(resourceName.pakkage(), thisClassName);
    typeStore.put(resourceName.pakkage(), "Builder", true, thisClassName);
    // Special-cased PubSub handling.
    List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(tokens -> !tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
    if (processedTokenHierarchies.size() > 1) {
        typeStore.putAll(resourceName.pakkage(), tokenHierarchies.subList(1, tokenHierarchies.size()).stream().map(ts -> getBuilderTypeName(ts)).collect(Collectors.toList()));
    }
    return typeStore;
}
Also used : Arrays(java.util.Arrays) Reference(com.google.api.generator.engine.ast.Reference) PathTemplate(com.google.api.pathtemplate.PathTemplate) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) Variable(com.google.api.generator.engine.ast.Variable) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Generated(javax.annotation.Generated) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) CommentComposer(com.google.api.generator.gapic.composer.comment.CommentComposer) Map(java.util.Map) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ImmutableMap(com.google.common.collect.ImmutableMap) ForStatement(com.google.api.generator.engine.ast.ForStatement) Set(java.util.Set) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ResourceNameConstants(com.google.api.generator.gapic.utils.ResourceNameConstants) List(java.util.List) GapicContext(com.google.api.generator.gapic.model.GapicContext) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) IfStatement(com.google.api.generator.engine.ast.IfStatement) BetaApi(com.google.api.core.BetaApi) TypeNode(com.google.api.generator.engine.ast.TypeNode) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) HashMap(java.util.HashMap) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) ValidationException(com.google.api.pathtemplate.ValidationException) LinkedHashSet(java.util.LinkedHashSet) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Iterator(java.util.Iterator) CastExpr(com.google.api.generator.engine.ast.CastExpr) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) GapicClass(com.google.api.generator.gapic.model.GapicClass) ResourceName(com.google.api.generator.gapic.model.ResourceName) Statement(com.google.api.generator.engine.ast.Statement) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) List(java.util.List) ArrayList(java.util.ArrayList)

Example 47 with TypeStore

use of com.google.api.generator.gapic.composer.store.TypeStore 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();
}
Also used : LocalChannelProvider(com.google.api.gax.grpc.testing.LocalChannelProvider) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) Variable(com.google.api.generator.engine.ast.Variable) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) GaxGrpcProperties(com.google.api.gax.grpc.GaxGrpcProperties) DefaultValueComposer(com.google.api.generator.gapic.composer.defaultvalue.DefaultValueComposer) Field(com.google.api.generator.gapic.model.Field) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) MockServiceHelper(com.google.api.gax.grpc.testing.MockServiceHelper) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Collectors(java.util.stream.Collectors) ClassNames(com.google.api.generator.gapic.composer.utils.ClassNames) List(java.util.List) GapicContext(com.google.api.generator.gapic.model.GapicContext) MockGrpcService(com.google.api.gax.grpc.testing.MockGrpcService) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) AbstractServiceClientTestClassComposer(com.google.api.generator.gapic.composer.common.AbstractServiceClientTestClassComposer) Function(java.util.function.Function) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AbstractMessage(com.google.protobuf.AbstractMessage) CastExpr(com.google.api.generator.engine.ast.CastExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) ResourceName(com.google.api.generator.gapic.model.ResourceName) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Statement(com.google.api.generator.engine.ast.Statement) Service(com.google.api.generator.gapic.model.Service) TreeMap(java.util.TreeMap) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) StatusCode(com.google.api.gax.rpc.StatusCode) Message(com.google.api.generator.gapic.model.Message) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) 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) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ArrayList(java.util.ArrayList) MockGrpcService(com.google.api.gax.grpc.testing.MockGrpcService) Service(com.google.api.generator.gapic.model.Service) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Example 48 with TypeStore

use of com.google.api.generator.gapic.composer.store.TypeStore 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));
}
Also used : LocalChannelProvider(com.google.api.gax.grpc.testing.LocalChannelProvider) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) Variable(com.google.api.generator.engine.ast.Variable) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) GaxGrpcProperties(com.google.api.gax.grpc.GaxGrpcProperties) DefaultValueComposer(com.google.api.generator.gapic.composer.defaultvalue.DefaultValueComposer) Field(com.google.api.generator.gapic.model.Field) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) MockServiceHelper(com.google.api.gax.grpc.testing.MockServiceHelper) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Collectors(java.util.stream.Collectors) ClassNames(com.google.api.generator.gapic.composer.utils.ClassNames) List(java.util.List) GapicContext(com.google.api.generator.gapic.model.GapicContext) MockGrpcService(com.google.api.gax.grpc.testing.MockGrpcService) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) AbstractServiceClientTestClassComposer(com.google.api.generator.gapic.composer.common.AbstractServiceClientTestClassComposer) Function(java.util.function.Function) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AbstractMessage(com.google.protobuf.AbstractMessage) CastExpr(com.google.api.generator.engine.ast.CastExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) ResourceName(com.google.api.generator.gapic.model.ResourceName) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Statement(com.google.api.generator.engine.ast.Statement) Service(com.google.api.generator.gapic.model.Service) TreeMap(java.util.TreeMap) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) StatusCode(com.google.api.gax.rpc.StatusCode) Message(com.google.api.generator.gapic.model.Message) MockGrpcService(com.google.api.gax.grpc.testing.MockGrpcService) Service(com.google.api.generator.gapic.model.Service) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 49 with TypeStore

use of com.google.api.generator.gapic.composer.store.TypeStore in project gapic-generator-java by googleapis.

the class MockServiceClassComposer method createTypes.

private static TypeStore createTypes(Service service) {
    List<Class<?>> concreteClazzes = Arrays.asList(AbstractMessage.class, BetaApi.class, Generated.class, ServerServiceDefinition.class);
    TypeStore typeStore = new TypeStore(concreteClazzes);
    typeStore.put("com.google.api.gax.grpc.testing", "MockGrpcService");
    typeStore.putAll(service.pakkage(), Arrays.asList(ClassNames.getMockServiceImplClassName(service), ClassNames.getMockServiceClassName(service)));
    return typeStore;
}
Also used : TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) GapicClass(com.google.api.generator.gapic.model.GapicClass)

Aggregations

TypeStore (com.google.api.generator.gapic.composer.store.TypeStore)49 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)41 GapicClass (com.google.api.generator.gapic.model.GapicClass)41 ArrayList (java.util.ArrayList)39 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)38 List (java.util.List)37 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)36 TypeNode (com.google.api.generator.engine.ast.TypeNode)36 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)35 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)35 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)35 ScopeNode (com.google.api.generator.engine.ast.ScopeNode)35 GapicContext (com.google.api.generator.gapic.model.GapicContext)35 JavaStyle (com.google.api.generator.gapic.utils.JavaStyle)35 Arrays (java.util.Arrays)35 Collectors (java.util.stream.Collectors)35 Expr (com.google.api.generator.engine.ast.Expr)34 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)34 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)34 Variable (com.google.api.generator.engine.ast.Variable)34