-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ast): array expressions (#1080)
* feat(ast): anonymous arrays for annotations * feat(ast): add convenience methods in anon array * feat(annotation): allow adding anon arrays * fix: fixed comments, test util class * feat(ast): arrays to allow VarExp and ValueExpr * fix(ast): add description only for assignments * fix(ast): comment correction, test array assignmnt * feat(test): test anon array assignment * fix(license): add licenses * fix(ast): various fixes * Perform validation in build() * Ensure type() to be explicitly set * Expose add Expr, but validate that they are either ValueExpr or VariableExpr * fix(ast): ArrayExpr ImportWriterVisitor fix & test * fix: remove unecessary comment
- Loading branch information
1 parent
f5d5524
commit bd9532c
Showing
10 changed files
with
411 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/com/google/api/generator/engine/ast/ArrayExpr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.api.generator.engine.ast; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@AutoValue | ||
public abstract class ArrayExpr implements Expr { | ||
|
||
public abstract List<Expr> exprs(); | ||
|
||
public abstract TypeNode type(); | ||
|
||
@Override | ||
public void accept(AstNodeVisitor visitor) { | ||
visitor.visit(this); | ||
} | ||
|
||
public static ArrayExpr.Builder builder() { | ||
return new AutoValue_ArrayExpr.Builder().setExprs(ImmutableList.of()); | ||
} | ||
|
||
public static ArrayExpr withStrings(String... stringValues) { | ||
ArrayExpr.Builder builder = ArrayExpr.builder(); | ||
Arrays.asList(stringValues).stream().forEach(s -> builder.addExpr(s)); | ||
return builder.build(); | ||
} | ||
|
||
public static ArrayExpr withExprs(Expr... exprs) { | ||
return ArrayExpr.builder().setExprs(Arrays.asList(exprs)).build(); | ||
} | ||
|
||
@AutoValue.Builder | ||
public abstract static class Builder { | ||
|
||
private static final String SAME_TYPE_EXPRS_MESSAGE = | ||
"All expressions must be of the type" + " specified in this ArrayExpr"; | ||
private static final String EXPR_ALLOWED_CLASSES_MESSAGE = | ||
"Only VariableExpr and ValueExpr can be used as elements of ArrayExpr"; | ||
|
||
abstract List<Expr> exprs(); | ||
|
||
abstract TypeNode type(); | ||
|
||
/** | ||
* To add a string expression same-type validation is performed | ||
* | ||
* @param expr | ||
* @return Builder | ||
*/ | ||
public ArrayExpr.Builder addExpr(String expr) { | ||
return addExpr(ValueExpr.withValue(StringObjectValue.withValue(expr))); | ||
} | ||
|
||
public ArrayExpr.Builder addExpr(Expr expr) { | ||
return setExprs((new ImmutableList.Builder<Expr>().addAll(exprs()).add(expr).build())); | ||
} | ||
|
||
public abstract ArrayExpr.Builder setExprs(List<Expr> exprs); | ||
|
||
public abstract ArrayExpr.Builder setType(TypeNode type); | ||
|
||
abstract ArrayExpr autoBuild(); | ||
|
||
public ArrayExpr build() { | ||
Preconditions.checkState( | ||
exprs().stream().allMatch(exp -> exp instanceof ValueExpr || exp instanceof VariableExpr), | ||
EXPR_ALLOWED_CLASSES_MESSAGE); | ||
TypeNode elementType = TypeNode.createElementTypeFromArrayType(type()); | ||
Preconditions.checkState( | ||
exprs().stream().allMatch(exp -> exp.type().equals(elementType)), | ||
SAME_TYPE_EXPRS_MESSAGE); | ||
return autoBuild(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/java/com/google/api/generator/engine/ast/ArrayExprTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.api.generator.engine.ast; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import com.google.api.generator.util.TestUtils; | ||
import org.junit.Test; | ||
|
||
public class ArrayExprTest { | ||
|
||
@Test | ||
public void validAnonymousArray_sametype() { | ||
ArrayExpr.Builder exprBuilder = | ||
ArrayExpr.builder() | ||
.setType(TypeNode.createArrayTypeOf(TypeNode.STRING)) | ||
.addExpr(TestUtils.generateStringValueExpr("test1")) | ||
.addExpr(TestUtils.generateStringValueExpr("test2")) | ||
.addExpr(TestUtils.generateStringValueExpr("test3")) | ||
.addExpr( | ||
ValueExpr.withValue( | ||
PrimitiveValue.builder().setValue("1").setType(TypeNode.INT).build())); | ||
|
||
Exception thrown = assertThrows(IllegalStateException.class, () -> exprBuilder.build()); | ||
assertThat(thrown) | ||
.hasMessageThat() | ||
.contains("All expressions must be of the type specified in this ArrayExpr"); | ||
} | ||
|
||
@Test | ||
public void validAnonymousArray_unsetTypeThrows() { | ||
ArrayExpr.Builder exprBuilder = ArrayExpr.builder(); | ||
IllegalStateException thrown = | ||
assertThrows(IllegalStateException.class, () -> exprBuilder.build()); | ||
assertThat(thrown).hasMessageThat().contains("Property \"type\" has not been set"); | ||
} | ||
|
||
@Test | ||
public void validAnonymousArray_onlyVariableAndValueExprs() { | ||
ArrayExpr.Builder exprBuilder = | ||
ArrayExpr.builder().setType(TypeNode.createArrayTypeOf(TypeNode.INT)); | ||
Variable variable = Variable.builder().setName("x").setType(TypeNode.INT).build(); | ||
VariableExpr variableExpr = | ||
VariableExpr.builder().setVariable(variable).setIsDecl(true).build(); | ||
Value value = PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build(); | ||
Expr valueExpr = ValueExpr.builder().setValue(value).build(); | ||
AssignmentExpr assignExpr = | ||
AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build(); | ||
exprBuilder.addExpr(assignExpr); | ||
IllegalStateException thrown = | ||
assertThrows(IllegalStateException.class, () -> exprBuilder.build()); | ||
assertThat(thrown) | ||
.hasMessageThat() | ||
.contains("Only VariableExpr and ValueExpr can be used as elements of ArrayExpr"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.