Search in sources :

Example 6 with Reference

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

the class ResourceNameHelperClassComposer method createFixedClassMemberVariables.

private static Map<String, VariableExpr> createFixedClassMemberVariables() {
    Map<String, TypeNode> memberVars = new HashMap<>();
    Reference stringRef = ConcreteReference.withClazz(String.class);
    memberVars.put("fieldValuesMap", TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(stringRef, stringRef)).build()));
    memberVars.put("pathTemplate", TypeNode.withReference(ConcreteReference.withClazz(PathTemplate.class)));
    memberVars.put("fixedValue", TypeNode.STRING);
    return memberVars.entrySet().stream().map(e -> Variable.builder().setName(e.getKey()).setType(e.getValue()).build()).collect(Collectors.toMap(v -> v.identifier().name(), v -> VariableExpr.withVariable(v)));
}
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) HashMap(java.util.HashMap) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 7 with Reference

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

the class ResourceNameHelperClassComposer method createGetFieldValuesMapMethod.

private static MethodDefinition createGetFieldValuesMapMethod(ResourceName resourceName, TypeNode thisClassType, Map<String, VariableExpr> patternTokenVarExprs, List<List<String>> tokenHierarchies) {
    Reference strRef = TypeNode.STRING.reference();
    TypeNode mapBuilderType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(Arrays.asList(strRef, strRef)).build());
    VariableExpr fieldMapBuilderVarExpr = VariableExpr.withVariable(Variable.builder().setName("fieldMapBuilder").setType(mapBuilderType).build());
    AssignmentExpr builderAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldMapBuilderVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(mapBuilderType).build()).build();
    // Special-cased PubSub handling.
    List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(tokens -> !tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
    // Innermost if-blocks.
    List<Statement> tokenIfStatements = new ArrayList<>();
    for (String token : getTokenSet(processedTokenHierarchies)) {
        VariableExpr tokenVarExpr = patternTokenVarExprs.get(token);
        Preconditions.checkNotNull(tokenVarExpr, String.format("No variable found for %s among %s", token, patternTokenVarExprs.keySet()));
        StringObjectValue tokenStrVal = StringObjectValue.withValue(token);
        MethodInvocationExpr putExpr = MethodInvocationExpr.builder().setExprReferenceExpr(fieldMapBuilderVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(tokenStrVal), tokenVarExpr).build();
        Expr notNullCheckExpr = RelationalOperationExpr.notEqualToWithExprs(tokenVarExpr, ValueExpr.createNullExpr());
        tokenIfStatements.add(IfStatement.builder().setConditionExpr(notNullCheckExpr).setBody(Arrays.asList(ExprStatement.withExpr(putExpr))).build());
    }
    // Put the innermost if-statements and assignment expressions together.
    VariableExpr fieldValuesMapVarExpr = FIXED_CLASS_VARS.get("fieldValuesMap");
    AssignmentExpr fieldValuesMapAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldValuesMapVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(fieldMapBuilderVarExpr).setMethodName("build").setReturnType(fieldValuesMapVarExpr.type()).build()).build();
    List<Statement> middleIfBlockStatements = new ArrayList<>();
    middleIfBlockStatements.add(ExprStatement.withExpr(builderAssignExpr));
    middleIfBlockStatements.addAll(tokenIfStatements);
    middleIfBlockStatements.add(ExprStatement.withExpr(fieldValuesMapAssignExpr));
    // Middle if-block, i.e. `if (fieldValuesMap == null)`.
    Expr fieldValuesMapNullCheckExpr = RelationalOperationExpr.equalToWithExprs(fieldValuesMapVarExpr, ValueExpr.createNullExpr());
    IfStatement fieldValuesMapIfStatement = IfStatement.builder().setConditionExpr(fieldValuesMapNullCheckExpr).setBody(middleIfBlockStatements).build();
    // Outer if-block.
    IfStatement outerIfStatement = IfStatement.builder().setConditionExpr(fieldValuesMapNullCheckExpr).setBody(Arrays.asList(SynchronizedStatement.builder().setLock(ThisObjectValue.withType(thisClassType)).setBody(Arrays.asList(fieldValuesMapIfStatement)).build())).build();
    // Put the method together.
    TypeNode mapStringType = fieldValuesMapVarExpr.type();
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(mapStringType).setName("getFieldValuesMap").setBody(Arrays.asList(outerIfStatement)).setReturnExpr(fieldValuesMapVarExpr).build();
}
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) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ForStatement(com.google.api.generator.engine.ast.ForStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ArrayList(java.util.ArrayList) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) IfStatement(com.google.api.generator.engine.ast.IfStatement) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) 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) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) List(java.util.List) ArrayList(java.util.ArrayList) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 8 with Reference

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

the class TypeParser method createMapType.

@VisibleForTesting
static TypeNode createMapType(FieldDescriptor field) {
    Preconditions.checkState(field.isMapField(), "createMapType can only be called on map-type fields");
    Descriptor type = field.getMessageType();
    FieldDescriptor keyField = type.findFieldByName("key");
    FieldDescriptor valueField = type.findFieldByName("value");
    Reference keyReference = parseFieldReference(keyField);
    Reference valueReference = parseFieldReference(valueField);
    Reference mapReference = ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(keyReference, valueReference)).build();
    return TypeNode.withReference(mapReference);
}
Also used : ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) Reference(com.google.api.generator.engine.ast.Reference) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with Reference

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

the class ImportWriterVisitorTest method writeMethodDefinitionImports_templatedMixedNamesAndTypes.

@Test
public void writeMethodDefinitionImports_templatedMixedNamesAndTypes() {
    Reference mapRef = ConcreteReference.withClazz(Map.class);
    List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("K", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).build(), VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("T", "V")).build());
    TypeNode returnType = TypeNode.withReference(mapRef);
    MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(returnType).setTemplateNames(Arrays.asList("T", "K", "V")).setReturnTemplateNames(Arrays.asList("K", "V")).setArguments(arguments).setReturnExpr(MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(returnType).build()).build();
    methodDefinition.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import java.util.Map;\n\n"), writerVisitor.write());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 10 with Reference

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

the class Message method findAndUnwrapPaginatedRepeatedField.

/**
 * Returns the first list repeated field in a message, unwrapped from its list type.
 */
@Nullable
public Field findAndUnwrapPaginatedRepeatedField() {
    for (Field field : fields()) {
        if (field.isMap()) {
            List<Reference> repeatedGenericMapRefs = field.type().reference().generics();
            TypeNode paginatedType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.Entry.class).setGenerics(Arrays.asList(repeatedGenericMapRefs.get(0), repeatedGenericMapRefs.get(1))).build());
            return field.toBuilder().setType(paginatedType).build();
        }
    }
    for (Field field : fields()) {
        if (field.isRepeated() && !field.isMap()) {
            Reference repeatedGenericRef = field.type().reference().generics().get(0);
            return field.toBuilder().setType(TypeNode.withReference(repeatedGenericRef)).build();
        }
    }
    return null;
}
Also used : ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Reference(com.google.api.generator.engine.ast.Reference) TypeNode(com.google.api.generator.engine.ast.TypeNode) Nullable(javax.annotation.Nullable)

Aggregations

Reference (com.google.api.generator.engine.ast.Reference)29 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)27 TypeNode (com.google.api.generator.engine.ast.TypeNode)21 VaporReference (com.google.api.generator.engine.ast.VaporReference)21 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)17 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)13 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)13 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)12 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)12 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)11 Expr (com.google.api.generator.engine.ast.Expr)11 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)11 Statement (com.google.api.generator.engine.ast.Statement)11 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)11 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)10 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)10 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)10 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)10 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)10