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][engx] chore: remove debug printfs #627

Merged
merged 17 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ JAVA_SRCS = [
TEST_SRCS = [
"//src/test/java/com/google/api/generator/engine:engine_files",
"//src/test/java/com/google/api/generator/gapic:gapic_files",
"//src/test/java/com/google/api/generator/util:util_files",
"//src/test/java/com/google/api/generator/test/framework:framework_files",
]

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/api/generator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ java_library(
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/gapic",
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/util",
"@com_google_googleapis//google/api:api_java_proto",
"@com_google_googleapis//google/longrunning:longrunning_java_proto",
"@com_google_guava_guava//jar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ java_library(
"//src/main/java/com/google/api/generator/gapic/composer/utils",
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/gapic/utils",
"//src/main/java/com/google/api/generator/util",
"@com_google_api_api_common//jar",
"@com_google_api_gax_java//gax",
"@com_google_api_gax_java//gax-grpc:gax_grpc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
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.api.generator.util.TriFunction;
import com.google.api.generator.util.Trie;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
Expand All @@ -84,6 +86,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Generated;
Expand Down Expand Up @@ -1336,23 +1339,68 @@ private static ClassDefinition createNestedRpcFixedSizeCollectionClass(
static Expr createRequestBuilderExpr(
Method method, List<MethodArgument> signature, TypeStore typeStore) {
TypeNode methodInputType = method.inputType();
MethodInvocationExpr newBuilderExpr =
Expr newBuilderExpr =
MethodInvocationExpr.builder()
.setMethodName("newBuilder")
.setStaticReferenceType(methodInputType)
.build();
for (MethodArgument argument : signature) {
newBuilderExpr = buildNestedSetterInvocationExpr(argument, newBuilderExpr);
// Maintain the args' order of appearance for better determinism.
List<Field> rootFields = new ArrayList<>();
Map<Field, Trie<Field>> rootFieldToTrie = new HashMap<>();
for (MethodArgument arg : signature) {
Field rootField = arg.nestedFields().isEmpty() ? arg.field() : arg.nestedFields().get(0);
if (!rootFields.contains(rootField)) {
rootFields.add(rootField);
}
Trie<Field> updatedTrie =
rootFieldToTrie.containsKey(rootField) ? rootFieldToTrie.get(rootField) : new Trie();
List<Field> nestedFieldsWithChild = new ArrayList<>(arg.nestedFields());
nestedFieldsWithChild.add(arg.field());
updatedTrie.insert(nestedFieldsWithChild);
rootFieldToTrie.put(rootField, updatedTrie);
}

MethodInvocationExpr builderExpr =
MethodInvocationExpr.builder()
.setMethodName("build")
.setExprReferenceExpr(newBuilderExpr)
.setReturnType(methodInputType)
.build();
Function<Field, Expr> parentPreprocFn =
field ->
(Expr)
MethodInvocationExpr.builder()
.setStaticReferenceType(field.type())
.setMethodName("newBuilder")
.build();
TriFunction<Field, Expr, Expr, Expr> parentPostprocFn =
(field, baseRefExpr, leafProcessedExpr) -> {
boolean isRootNode = field == null;
return isRootNode
? leafProcessedExpr
: MethodInvocationExpr.builder()
.setExprReferenceExpr(baseRefExpr)
.setMethodName(String.format("set%s", JavaStyle.toUpperCamelCase(field.name())))
.setArguments(
MethodInvocationExpr.builder()
.setExprReferenceExpr(leafProcessedExpr)
.setMethodName("build")
.build())
.build();
};

final Map<Field, MethodArgument> fieldToMethodArg =
signature.stream().collect(Collectors.toMap(a -> a.field(), a -> a));
BiFunction<Field, Expr, Expr> leafProcFn =
(field, parentBaseRefExpr) ->
(Expr) buildNestedSetterInvocationExpr(fieldToMethodArg.get(field), parentBaseRefExpr);

for (Field rootField : rootFields) {
newBuilderExpr =
rootFieldToTrie
.get(rootField)
.dfsTraverseAndReduce(parentPreprocFn, parentPostprocFn, leafProcFn, newBuilderExpr);
}

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

@VisibleForTesting
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/google/api/generator/gapic/model/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.api.generator.engine.ast.TypeNode;
import com.google.auto.value.AutoValue;
import java.util.Objects;
import javax.annotation.Nullable;

@AutoValue
Expand Down Expand Up @@ -48,6 +49,37 @@ public boolean hasResourceReference() {
return type().equals(TypeNode.STRING) && resourceReference() != null;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Field)) {
return false;
}

Field other = (Field) o;
return name().equals(other.name())
&& type().equals(other.type())
&& isMessage() == other.isMessage()
&& isEnum() == other.isEnum()
&& isRepeated() == other.isRepeated()
&& isMap() == other.isMap()
&& isContainedInOneof() == other.isContainedInOneof()
&& Objects.equals(resourceReference(), other.resourceReference())
&& Objects.equals(description(), other.description());
}

@Override
public int hashCode() {
return 17 * name().hashCode()
+ 19 * type().hashCode()
+ (isMessage() ? 1 : 0) * 23
+ (isEnum() ? 1 : 0) * 29
+ (isRepeated() ? 1 : 0) * 31
+ (isMap() ? 1 : 0) * 37
+ (isContainedInOneof() ? 1 : 0) * 41
+ (resourceReference() == null ? 0 : resourceReference().hashCode())
+ (description() == null ? 0 : description().hashCode());
}

public abstract Builder toBuilder();

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ public int compareTo(MethodArgument other) {
return compareVal;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof MethodArgument)) {
return false;
}

MethodArgument other = (MethodArgument) o;
return name().equals(other.name())
&& type().equals(other.type())
&& field().equals(other.field()) & nestedFields().equals(other.nestedFields())
&& isResourceNameHelper() == other.isResourceNameHelper();
}

@Override
public int hashCode() {
return 17 * name().hashCode()
+ 19 * type().hashCode()
+ 23 * field().hashCode()
+ 29 * nestedFields().hashCode()
+ (isResourceNameHelper() ? 1 : 0) * 31;
}

public static Builder builder() {
return new AutoValue_MethodArgument.Builder()
.setNestedFields(ImmutableList.of())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,8 @@ private static void checkHttpFieldIsValid(String binding, Message inputMessage,
Preconditions.checkState(
inputMessage.fieldMap().containsKey(binding),
String.format(
"Expected message %s to contain field %s but none found"
+ ", DEL: "
+ inputMessage.fieldMap().keySet(),
inputMessage.name(),
binding));
"Expected message %s to contain field %s but none found",
inputMessage.name(), binding));
Field field = inputMessage.fieldMap().get(binding);
boolean fieldCondition = !field.isRepeated();
if (!isBody) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,8 @@ static LongrunningOperation parseLro(
Preconditions.checkNotNull(
metadataMessage,
String.format(
"LRO metadata message %s not found in method %s" + ", DEL: " + messageTypes.keySet(),
metadataTypeName,
methodDescriptor.getName()));
"LRO metadata message %s not found in method %s",
metadataTypeName, methodDescriptor.getName()));

return LongrunningOperation.withTypes(responseMessage.type(), metadataMessage.type());
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/google/api/generator/util/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@rules_java//java:defs.bzl", "java_library")

package(default_visibility = ["//visibility:public"])

filegroup(
name = "util_files",
srcs = glob(["*.java"]),
)

java_library(
name = "util",
srcs = [
":util_files",
],
)
28 changes: 28 additions & 0 deletions src/main/java/com/google/api/generator/util/TriFunction.java
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.util;

import java.util.Objects;
import java.util.function.Function;

@FunctionalInterface
public interface TriFunction<A, B, C, R> {
R apply(A a, B b, C c);

default <V> TriFunction<A, B, C, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (A a, B b, C c) -> after.apply(apply(a, b, c));
}
}
Loading