Search in sources :

Example 11 with AssignmentExpr

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

the class ServiceClientMethodSampleComposer method composeCanonicalSample.

public static Sample composeCanonicalSample(Method method, TypeNode clientType, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
    VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(clientType.reference().name())).setType(clientType).build());
    // Create request variable expression and assign with its default value.
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(method.inputType()).build());
    List<VariableExpr> rpcMethodArgVarExprs = Arrays.asList(requestVarExpr);
    Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
    Preconditions.checkNotNull(requestMessage, String.format("Could not find the message type %s.", method.inputType().reference().fullName()));
    Expr requestBuilderExpr = DefaultValueComposer.createSimpleMessageBuilderValue(requestMessage, resourceNames, messageTypes);
    AssignmentExpr requestAssignmentExpr = AssignmentExpr.builder().setVariableExpr(requestVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(requestBuilderExpr).build();
    List<Expr> bodyExprs = new ArrayList<>();
    bodyExprs.add(requestAssignmentExpr);
    List<Statement> bodyStatements = new ArrayList<>();
    RegionTag regionTag;
    if (method.isPaged()) {
        // e.g. echoClient.pagedExpand(request).iterateAll()
        Sample unaryPagedRpc = composePagedSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs, messageTypes);
        bodyStatements.addAll(unaryPagedRpc.body());
        regionTag = unaryPagedRpc.regionTag();
    } else if (method.hasLro()) {
        Sample unaryLroRpc = composeLroSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs);
        bodyStatements.addAll(unaryLroRpc.body());
        regionTag = unaryLroRpc.regionTag();
    } else {
        // e.g. echoClient.echo(request)
        Sample unaryRpc = composeSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs);
        bodyStatements.addAll(unaryRpc.body());
        regionTag = unaryRpc.regionTag();
    }
    List<Statement> body = Arrays.asList(TryCatchStatement.builder().setTryResourceExpr(SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)).setTryBody(bodyStatements).setIsSampleCode(true).build());
    return Sample.builder().setBody(body).setRegionTag(regionTag).setIsCanonical(true).build();
}
Also used : Message(com.google.api.generator.gapic.model.Message) 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) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) RegionTag(com.google.api.generator.gapic.model.RegionTag) Sample(com.google.api.generator.gapic.model.Sample) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 12 with AssignmentExpr

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

the class JavaWriterVisitorTest method writeSuperObjectValue_accessFieldAndInvokeMethod.

@Test
public void writeSuperObjectValue_accessFieldAndInvokeMethod() {
    VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
    TypeNode classType = TypeNode.withReference(ref);
    SuperObjectValue superObjectValue = SuperObjectValue.withType(classType);
    ValueExpr superValueExpr = ValueExpr.withValue(superObjectValue);
    Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
    VariableExpr superVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(superValueExpr).build();
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
    AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(superVariableExpr).setValueExpr(methodExpr).build();
    assignmentExpr.accept(writerVisitor);
    assertThat(writerVisitor.write()).isEqualTo("super.name = super.getName()");
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) SuperObjectValue(com.google.api.generator.engine.ast.SuperObjectValue) Test(org.junit.Test)

Example 13 with AssignmentExpr

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

the class JavaWriterVisitorTest method writeForStatement.

@Test
public void writeForStatement() {
    AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
    Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
    List<Statement> body = Arrays.asList(assignExprStatement, assignExprStatement);
    VariableExpr varDeclExpr = createVariableDeclExpr("str", TypeNode.STRING);
    Expr collectionExpr = MethodInvocationExpr.builder().setMethodName("getSomeStrings").build();
    ForStatement forStatement = ForStatement.builder().setLocalVariableExpr(varDeclExpr).setCollectionExpr(collectionExpr).setBody(body).build();
    forStatement.accept(writerVisitor);
    assertEquals(String.format("%s%s%s%s", "for (String str : getSomeStrings()) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) 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) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) 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) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) GeneralForStatement(com.google.api.generator.engine.ast.GeneralForStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) 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) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ForStatement(com.google.api.generator.engine.ast.ForStatement) GeneralForStatement(com.google.api.generator.engine.ast.GeneralForStatement) Test(org.junit.Test)

Example 14 with AssignmentExpr

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

the class JavaCodeGeneratorTest method createOverrideCreateBookMethod.

private MethodDefinition createOverrideCreateBookMethod(VaporReference classRef, Expr bookKindDefaultExpr) {
    VariableExpr thisBookKindVariableExpr = createVarExprFromRefVarExpr(bookKindVar, ValueExpr.withValue(ThisObjectValue.withType(TypeNode.withReference(classRef))));
    VariableExpr thisSeriesNumVariableExpr = createVarExprFromRefVarExpr(seriesNumVar, ValueExpr.withValue(ThisObjectValue.withType(TypeNode.withReference(classRef))));
    AssignmentExpr thisSeriesNumAssignExpr = createAssignmentExpr(thisSeriesNumVariableExpr, VariableExpr.withVariable(seriesNumVar));
    AssignmentExpr thisBookKindAssignmentExpr = createAssignmentExpr(thisBookKindVariableExpr, bookKindDefaultExpr);
    MethodDefinition overrideCreateBook = MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.OVERRIDE)).setArguments(Arrays.asList(createVarDeclExpr(seriesNumVar), createVarDeclExpr(bookKindVar))).setReturnType(TypeNode.VOID).setName("createBook").setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(thisSeriesNumAssignExpr), ExprStatement.withExpr(thisBookKindAssignmentExpr))).build();
    return overrideCreateBook;
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 15 with AssignmentExpr

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

the class JavaCodeGeneratorTest method createNestedClassShelf.

private ClassDefinition createNestedClassShelf() {
    VariableExpr shelfNameDel = createVarPublicDeclExpr(shelfNameVar);
    VariableExpr seriesNumDel = createVarPublicDeclExpr(seriesNumVar);
    VariableExpr shelfServiceNameDel = createVarPublicDeclExpr(shelfServiceNameVar);
    AssignmentExpr shelfServiceNameAssignmentExpr = createAssignmentExpr(shelfServiceNameDel, VariableExpr.withVariable(createVarFromType(TypeNode.STRING, "serviceName")));
    MethodDefinition nestedShelfClassCtor = createNestedShelfClassCtor();
    return ClassDefinition.builder().setIsNested(true).setMethods(Arrays.asList(nestedShelfClassCtor)).setStatements(Arrays.asList(ExprStatement.withExpr(shelfNameDel), ExprStatement.withExpr(seriesNumDel), ExprStatement.withExpr(shelfServiceNameAssignmentExpr))).setName("Shelf").setScope(ScopeNode.PUBLIC).build();
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Aggregations

AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)58 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)54 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)43 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)37 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)35 Statement (com.google.api.generator.engine.ast.Statement)33 Expr (com.google.api.generator.engine.ast.Expr)32 TypeNode (com.google.api.generator.engine.ast.TypeNode)31 Variable (com.google.api.generator.engine.ast.Variable)29 ArrayList (java.util.ArrayList)28 IfStatement (com.google.api.generator.engine.ast.IfStatement)27 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)26 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)26 ForStatement (com.google.api.generator.engine.ast.ForStatement)24 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)22 CastExpr (com.google.api.generator.engine.ast.CastExpr)21 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)21 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)20 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)20 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)20