Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfb7157

Browse files
feat: Add SuperObjectValue component (#159)
* feat: Add SuperObjectValue component * Add comment for super type
1 parent 0cc3fd9 commit bfb7157

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.engine.ast;
16+
17+
import com.google.auto.value.AutoValue;
18+
import com.google.common.base.Preconditions;
19+
20+
@AutoValue
21+
public abstract class SuperObjectValue implements ObjectValue {
22+
private static final String SUPER_VALUE = "super";
23+
24+
@Override
25+
public abstract TypeNode type();
26+
27+
@Override
28+
public String value() {
29+
return SUPER_VALUE;
30+
}
31+
32+
// Note: This is the type of "super" object, and not the child type of the supertype.
33+
public static SuperObjectValue withType(TypeNode type) {
34+
return builder().setType(type).build();
35+
}
36+
37+
private static Builder builder() {
38+
return new AutoValue_SuperObjectValue.Builder();
39+
}
40+
41+
@AutoValue.Builder
42+
abstract static class Builder {
43+
abstract Builder setType(TypeNode type);
44+
45+
abstract SuperObjectValue autoBuild();
46+
47+
private SuperObjectValue build() {
48+
SuperObjectValue superObjectValue = autoBuild();
49+
Preconditions.checkState(
50+
TypeNode.isReferenceType(superObjectValue.type()),
51+
"The \"super\" can only refer to object types");
52+
return superObjectValue;
53+
}
54+
}
55+
}

src/test/java/com/google/api/generator/engine/ast/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ TESTS = [
2626
"VaporReferenceTest",
2727
"MethodInvocationExprTest",
2828
"ThisObjectValueTest",
29+
"SuperObjectValueTest",
2930
]
3031

3132
filegroup(
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.engine.ast;
16+
17+
import static org.junit.Assert.assertThrows;
18+
19+
import org.junit.Test;
20+
21+
public class SuperObjectValueTest {
22+
23+
@Test
24+
public void validSuperObjectValue_basic() {
25+
VaporReference ref =
26+
VaporReference.builder()
27+
.setName("Student")
28+
.setPakkage("com.google.example.examples.v1")
29+
.build();
30+
TypeNode typeNode = TypeNode.withReference(ref);
31+
SuperObjectValue.withType(typeNode);
32+
// No exception thrown, we're good.
33+
}
34+
35+
@Test
36+
public void invalidSuperObjectValue_nonReferenceType() {
37+
assertThrows(
38+
IllegalStateException.class,
39+
() -> {
40+
SuperObjectValue.withType(TypeNode.DOUBLE);
41+
});
42+
}
43+
44+
@Test
45+
public void invalidSuperObjectValue_nullType() {
46+
assertThrows(
47+
IllegalStateException.class,
48+
() -> {
49+
SuperObjectValue.withType(TypeNode.NULL);
50+
});
51+
}
52+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
import com.google.api.generator.engine.ast.NewObjectExpr;
3434
import com.google.api.generator.engine.ast.Reference;
3535
import com.google.api.generator.engine.ast.ScopeNode;
36+
import com.google.api.generator.engine.ast.SuperObjectValue;
3637
import com.google.api.generator.engine.ast.TernaryExpr;
3738
import com.google.api.generator.engine.ast.ThrowExpr;
3839
import com.google.api.generator.engine.ast.TypeNode;
40+
import com.google.api.generator.engine.ast.ValueExpr;
3941
import com.google.api.generator.engine.ast.VaporReference;
4042
import com.google.api.generator.engine.ast.Variable;
4143
import com.google.api.generator.engine.ast.VariableExpr;
@@ -676,6 +678,25 @@ public void writeMethodDefinitionImports_templatedMixedNamesAndTypes() {
676678
"import java.util.Map;\n\n"));
677679
}
678680

681+
@Test
682+
public void writeSuperObjectValueImports() {
683+
VaporReference ref =
684+
VaporReference.builder()
685+
.setName("Student")
686+
.setPakkage("com.google.example.examples.v1")
687+
.build();
688+
TypeNode typeNode = TypeNode.withReference(ref);
689+
SuperObjectValue superObjectValue = SuperObjectValue.withType(typeNode);
690+
MethodInvocationExpr methodExpr =
691+
MethodInvocationExpr.builder()
692+
.setMethodName("getName")
693+
.setExprReferenceExpr(ValueExpr.withValue(superObjectValue))
694+
.setReturnType(TypeNode.STRING)
695+
.build();
696+
methodExpr.accept(writerVisitor);
697+
assertEquals(writerVisitor.write(), "import com.google.example.examples.v1.Student;\n\n");
698+
}
699+
679700
private static TypeNode createType(Class clazz) {
680701
return TypeNode.withReference(ConcreteReference.withClazz(clazz));
681702
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.google.api.generator.engine.ast.ScopeNode;
4545
import com.google.api.generator.engine.ast.Statement;
4646
import com.google.api.generator.engine.ast.StringObjectValue;
47+
import com.google.api.generator.engine.ast.SuperObjectValue;
4748
import com.google.api.generator.engine.ast.TernaryExpr;
4849
import com.google.api.generator.engine.ast.ThisObjectValue;
4950
import com.google.api.generator.engine.ast.ThrowExpr;
@@ -1750,6 +1751,36 @@ public void writeThisObjectValue_accessFieldAndInvokeMethod() {
17501751
assertThat(writerVisitor.write()).isEqualTo("this.name = this.getName(id)");
17511752
}
17521753

1754+
@Test
1755+
public void writeSuperObjectValue_accessFieldAndInvokeMethod() {
1756+
VaporReference ref =
1757+
VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
1758+
TypeNode classType = TypeNode.withReference(ref);
1759+
SuperObjectValue superObjectValue = SuperObjectValue.withType(classType);
1760+
ValueExpr superValueExpr = ValueExpr.withValue(superObjectValue);
1761+
Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
1762+
VariableExpr superVariableExpr =
1763+
VariableExpr.builder()
1764+
.setVariable(subVariable)
1765+
.setExprReferenceExpr(superValueExpr)
1766+
.build();
1767+
1768+
MethodInvocationExpr methodExpr =
1769+
MethodInvocationExpr.builder()
1770+
.setMethodName("getName")
1771+
.setExprReferenceExpr(ValueExpr.withValue(superObjectValue))
1772+
.setReturnType(TypeNode.STRING)
1773+
.build();
1774+
AssignmentExpr assignmentExpr =
1775+
AssignmentExpr.builder()
1776+
.setVariableExpr(superVariableExpr)
1777+
.setValueExpr(methodExpr)
1778+
.build();
1779+
1780+
assignmentExpr.accept(writerVisitor);
1781+
assertThat(writerVisitor.write()).isEqualTo("super.name = super.getName()");
1782+
}
1783+
17531784
private static String createLines(int numLines) {
17541785
return new String(new char[numLines]).replace("\0", "%s");
17551786
}

0 commit comments

Comments
 (0)