Skip to content

Commit a179558

Browse files
authored
feat(ast): add support for LambdaExpr to infer type from return expr type (#1011)
This will enable it to be assigned to an appropriate variable Before this change: cannot assign a lambda expression to a variable because its type is always VOID. After this change: lambda expression builder will infer type from returnExpr’s expression type. Allowing it to be assigned to an appropriate variable. Because returnExpr is a required field, its type should always be accessible. Example: a naive sample usage is provided in JavaWriterVisitorTest.java as test.
1 parent 6e02c70 commit a179558

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/main/java/com/google/api/generator/engine/ast/LambdaExpr.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
public abstract class LambdaExpr implements Expr {
2828
@Override
2929
public TypeNode type() {
30-
// TODO(v2): Support set of FunctionalInterface parameterized on the args and return type,
31-
// which would enable assignment to an appropriate variable.
32-
return TypeNode.VOID;
30+
return returnExpr().expr().type();
3331
}
3432

3533
public abstract ImmutableList<VariableExpr> arguments();

src/test/java/com/google/api/generator/engine/ast/LambdaExprTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.api.generator.engine.ast;
1616

17+
import static org.junit.Assert.assertEquals;
1718
import static org.junit.Assert.assertThrows;
1819

1920
import java.util.Arrays;
@@ -27,6 +28,15 @@ public void validLambdaExpr_noArguments() {
2728
.build();
2829
}
2930

31+
@Test
32+
public void validLambdaExpr_inferTypeFromReturnExpr() {
33+
LambdaExpr lambdaExpr =
34+
LambdaExpr.builder()
35+
.setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo")))
36+
.build();
37+
assertEquals(TypeNode.STRING, lambdaExpr.type());
38+
}
39+
3040
@Test
3141
public void validLambdaExpr_severalArguments() {
3242
VariableExpr argOneVarExpr =

src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -2345,6 +2345,23 @@ public void writeLambdaExpr_noParameters() {
23452345
assertEquals("() -> \"foo\"", writerVisitor.write());
23462346
}
23472347

2348+
@Test
2349+
public void writeLambdaExpr_assignToVariable() {
2350+
LambdaExpr lambdaExpr =
2351+
LambdaExpr.builder()
2352+
.setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo")))
2353+
.build();
2354+
AssignmentExpr assignmentExpr =
2355+
AssignmentExpr.builder()
2356+
.setVariableExpr(
2357+
VariableExpr.withVariable(
2358+
Variable.builder().setName("word").setType(TypeNode.STRING).build()))
2359+
.setValueExpr(lambdaExpr)
2360+
.build();
2361+
assignmentExpr.accept(writerVisitor);
2362+
assertEquals("word = () -> \"foo\"", writerVisitor.write());
2363+
}
2364+
23482365
@Test
23492366
public void writeLambdaExpr_oneParameter() {
23502367
VariableExpr argVarExpr =

0 commit comments

Comments
 (0)