Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ggj][ast] feat: add EmptyLineStatement #375

Merged
merged 4 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public interface AstNodeVisitor {

public void visit(CommentStatement commentStatement);

public void visit(EmptyLineStatement emptyLineStatement);

/** =============================== OTHER =============================== */
public void visit(MethodDefinition methodDefinition);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2020 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;

public class EmptyLineStatement implements Statement {
private EmptyLineStatement() {}

@Override
public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
}

public static EmptyLineStatement create() {
return new EmptyLineStatement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.EmptyLineStatement;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
Expand Down Expand Up @@ -316,23 +317,28 @@ public void visit(SynchronizedStatement synchronizedStatement) {

@Override
public void visit(CommentStatement commentStatement) {
// Do nothing
// Nothing to do.
}

@Override
public void visit(EmptyLineStatement emptyLineStatement) {
// Nothing to do.
}

/** =============================== COMMENT =============================== */
@Override
public void visit(LineComment lineComment) {
// Do nothing
// Nothing to do.
}

@Override
public void visit(BlockComment blockComment) {
// Do nothing
// Nothing to do.
}

@Override
public void visit(JavaDocComment javaDocComment) {
// Do nothing
// Nothing to do.
}

/** =============================== OTHER =============================== */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.EmptyLineStatement;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
Expand Down Expand Up @@ -628,6 +629,11 @@ public void visit(CommentStatement commentStatement) {
commentStatement.comment().accept(this);
}

@Override
public void visit(EmptyLineStatement emptyLineStatement) {
newline();
}

/** =============================== COMMENT =============================== */
public void visit(LineComment lineComment) {
// Split comments by new line and add `//` to each line.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.google.api.generator.gapic.model.MethodArgument;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.util.concurrent.MoreExecutors;
Expand All @@ -90,6 +91,9 @@ public class ServiceClientClassComposer implements ClassComposer {
private static final String PAGED_CALLABLE_NAME_PATTERN = "%sPagedCallable";
private static final String OPERATION_CALLABLE_NAME_PATTERN = "%sOperationCallable";

private static final Reference LIST_REFERENCE = ConcreteReference.withClazz(List.class);
private static final Reference MAP_REFERENCE = ConcreteReference.withClazz(Map.class);

private enum CallableMethodKind {
REGULAR,
LRO,
Expand Down Expand Up @@ -499,8 +503,6 @@ private static List<MethodDefinition> createMethodVariants(
}

String methodInputTypeName = methodInputType.reference().name();
Reference listRef = ConcreteReference.withClazz(List.class);
Reference mapRef = ConcreteReference.withClazz(Map.class);

// Make the method signature order deterministic, which helps with unit testing and per-version
// diffs.
Expand Down Expand Up @@ -544,71 +546,12 @@ private static List<MethodDefinition> createMethodVariants(
.setIsDecl(true)
.build();

MethodInvocationExpr newBuilderExpr =
MethodInvocationExpr.builder()
.setMethodName("newBuilder")
.setStaticReferenceType(methodInputType)
.build();
// TODO(miraleung): Handle nested arguments and descending setters here.
for (MethodArgument argument : signature) {
String argumentName = JavaStyle.toLowerCamelCase(argument.name());
TypeNode argumentType = argument.type();
String setterMethodVariantPattern = "set%s";
if (TypeNode.isReferenceType(argumentType)) {
if (listRef.isSupertypeOrEquals(argumentType.reference())) {
setterMethodVariantPattern = "addAll%s";
} else if (mapRef.isSupertypeOrEquals(argumentType.reference())) {
setterMethodVariantPattern = "putAll%s";
}
}
String setterMethodName =
String.format(setterMethodVariantPattern, JavaStyle.toUpperCamelCase(argumentName));

Expr argVarExpr =
VariableExpr.withVariable(
Variable.builder().setName(argumentName).setType(argumentType).build());

if (argument.isResourceNameHelper()) {
MethodInvocationExpr isNullCheckExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(types.get("Objects"))
.setMethodName("isNull")
.setArguments(Arrays.asList(argVarExpr))
.setReturnType(TypeNode.BOOLEAN)
.build();
Expr nullExpr = ValueExpr.withValue(NullObjectValue.create());
MethodInvocationExpr toStringExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(argVarExpr)
.setMethodName("toString")
.setReturnType(TypeNode.STRING)
.build();
argVarExpr =
TernaryExpr.builder()
.setConditionExpr(isNullCheckExpr)
.setThenExpr(nullExpr)
.setElseExpr(toStringExpr)
.build();
}
Expr requestBuilderExpr = createRequestBuilderExpr(method, signature, types);

newBuilderExpr =
MethodInvocationExpr.builder()
.setMethodName(setterMethodName)
.setArguments(Arrays.asList(argVarExpr))
.setExprReferenceExpr(newBuilderExpr)
.build();
}

MethodInvocationExpr builderExpr =
MethodInvocationExpr.builder()
.setMethodName("build")
.setExprReferenceExpr(newBuilderExpr)
.setReturnType(methodInputType)
.build();
AssignmentExpr requestAssignmentExpr =
AssignmentExpr.builder()
.setVariableExpr(requestVarExpr)
.setValueExpr(builderExpr)
.setValueExpr(requestBuilderExpr)
.build();
List<Statement> statements = Arrays.asList(ExprStatement.withExpr(requestAssignmentExpr));

Expand Down Expand Up @@ -1373,6 +1316,75 @@ private static ClassDefinition createNestedRpcFixedSizeCollectionClass(
.build();
}

@VisibleForTesting
static Expr createRequestBuilderExpr(
Method method, List<MethodArgument> signature, Map<String, TypeNode> types) {
TypeNode methodInputType = method.inputType();
MethodInvocationExpr newBuilderExpr =
MethodInvocationExpr.builder()
.setMethodName("newBuilder")
.setStaticReferenceType(methodInputType)
.build();
// TODO(miraleung): Handle nested arguments and descending setters here.
for (MethodArgument argument : signature) {
String argumentName = JavaStyle.toLowerCamelCase(argument.name());
TypeNode argumentType = argument.type();
String setterMethodVariantPattern = "set%s";
if (TypeNode.isReferenceType(argumentType)) {
if (LIST_REFERENCE.isSupertypeOrEquals(argumentType.reference())) {
setterMethodVariantPattern = "addAll%s";
} else if (MAP_REFERENCE.isSupertypeOrEquals(argumentType.reference())) {
setterMethodVariantPattern = "putAll%s";
}
}
String setterMethodName =
String.format(setterMethodVariantPattern, JavaStyle.toUpperCamelCase(argumentName));

Expr argVarExpr =
VariableExpr.withVariable(
Variable.builder().setName(argumentName).setType(argumentType).build());

if (argument.isResourceNameHelper()) {
MethodInvocationExpr isNullCheckExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(types.get("Objects"))
.setMethodName("isNull")
.setArguments(Arrays.asList(argVarExpr))
.setReturnType(TypeNode.BOOLEAN)
.build();
Expr nullExpr = ValueExpr.withValue(NullObjectValue.create());
MethodInvocationExpr toStringExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(argVarExpr)
.setMethodName("toString")
.setReturnType(TypeNode.STRING)
.build();
argVarExpr =
TernaryExpr.builder()
.setConditionExpr(isNullCheckExpr)
.setThenExpr(nullExpr)
.setElseExpr(toStringExpr)
.build();
}

newBuilderExpr =
MethodInvocationExpr.builder()
.setMethodName(setterMethodName)
.setArguments(Arrays.asList(argVarExpr))
.setExprReferenceExpr(newBuilderExpr)
.build();
}

MethodInvocationExpr builderExpr =
MethodInvocationExpr.builder()
.setMethodName("build")
.setExprReferenceExpr(newBuilderExpr)
.setReturnType(methodInputType)
.build();

return builderExpr;
}

private static Map<String, TypeNode> createTypes(
Service service, Map<String, Message> messageTypes) {
Map<String, TypeNode> types = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.api.generator.engine.writer;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
Expand All @@ -25,6 +26,7 @@
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.EmptyLineStatement;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
Expand Down Expand Up @@ -1020,6 +1022,13 @@ public void writeLogicalOperationExprImports() {
"import com.google.api.generator.engine.ast.UnaryOperationExpr;\n\n");
}

@Test
public void writeEmptyLineStatementImports() {
EmptyLineStatement statement = EmptyLineStatement.create();
statement.accept(writerVisitor);
assertThat(writerVisitor.write()).isEmpty();
}

private static TypeNode createType(Class clazz) {
return TypeNode.withReference(ConcreteReference.withClazz(clazz));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.EmptyLineStatement;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
Expand Down Expand Up @@ -2209,6 +2210,13 @@ public void writeAssignmentOperationExpr_xorAssignment() {
assertThat(writerVisitor.write()).isEqualTo("h ^= Objects.hashCode(fixedValue)");
}

@Test
public void writeEmptyLineStatement() {
EmptyLineStatement statement = EmptyLineStatement.create();
statement.accept(writerVisitor);
assertEquals(writerVisitor.write(), "\n");
}

private static String createLines(int numLines) {
return new String(new char[numLines]).replace("\0", "%s");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ UPDATE_GOLDENS_TESTS = [
"MockServiceClassComposerTest",
"MockServiceImplClassComposerTest",
"ResourceNameHelperClassComposerTest",
"ServiceClientClassComposerTest",
"ServiceClientTestClassComposerTest",
"ServiceSettingsClassComposerTest",
"ServiceStubSettingsClassComposerTest",
"ServiceStubClassComposerTest",
Expand All @@ -20,8 +22,6 @@ TESTS = UPDATE_GOLDENS_TESTS + [
"DefaultValueComposerTest",
"ResourceNameTokenizerTest",
"RetrySettingsComposerTest",
"ServiceClientClassComposerTest",
"ServiceClientTestClassComposerTest",
]

TEST_DEPS = [
Expand Down
Loading