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][codegen] feat: add splitException, count{Elements,Bytes} ServiceStubSettings batching descriptor #265

Merged
merged 7 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
23 changes: 15 additions & 8 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,32 @@ function echo_success {
function header_check_preparation {
echo_status "Setting up license check environment"
export GOPATH=$(go env GOPATH)
if [ $? -ne 0 ];
then
echo_status "Please install Go first, instructions can be found here: https://golang.org/doc/install."
else
if [ $? -ne 0 ];
then
echo_status "Please install Go first, instructions can be found here: https://golang.org/doc/install."
else
export ENV_PATH=$(echo $PATH)
if [[ $ENV_PATH != *$GOPATH* ]];
then
echo_status "GOPATH is not in the system path, adding it now."
export PATH=$GOPATH/bin:$PATH
fi
which addlicense
if [ $? -ne 0 ];
if [ $? -ne 0 ];
then
echo_status "addlicense tool is not yet installed, downloading it now."
go get -u github.com/google/addlicense
fi
fi
}

# Disk cache.
BAZEL_CACHE_DIR=/tmp/bazel_cache_gapic_generator_java
if [ ! -d $BAZEL_CACHE_DIR ]
then
mkdir $BAZEL_CACHE_DIR
fi

# Constants.
# Check only the staged files.
NUM_TOTAL_FILES_CHANGED=$(git diff --cached --name-only | wc -l)
Expand All @@ -80,7 +87,7 @@ fi
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ]
then
echo_status "Running Java linter..."
bazel build //:google_java_format_verification
bazel build --disk_cache="$BAZEL_CACHE_DIR" //:google_java_format_verification
FORMAT_STATUS=$?
if [ $FORMAT_STATUS != 0 ]
then
Expand All @@ -93,7 +100,7 @@ fi
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] || [ $NUM_BAZEL_FILES_CHANGED -gt 0 ]
then
echo_status "Checking the build..."
bazel build //...
bazel build --disk_cache="$BAZEL_CACHE_DIR" //...
BUILD_STATUS=$?
if [ $BUILD_STATUS != 0 ]
then
Expand All @@ -105,7 +112,7 @@ fi
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ]
then
echo_status "Checking tests..."
bazel test //...
bazel test --disk_cache="$BAZEL_CACHE_DIR" //...
TEST_STATUS=$?
if [ $TEST_STATUS != 0 ]
then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,29 @@
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;

@AutoValue
public abstract class ConcreteReference implements Reference {
private static final String EXTENDS = "extends";

private static final String COMMA = ", ";
private static final String DOT = ".";
private static final String SPACE = " ";
private static final String LEFT_ANGLE = "<";
private static final String RIGHT_ANGLE = ">";
private static final String QUESTION_MARK = "?";

private static final Class WILDCARD_CLAZZ = ReferenceWildcard.class;

// Private.
abstract Class clazz();

@Nullable
@Override
public abstract Reference wildcardUpperBound();

@Override
public abstract ImmutableList<Reference> generics();

Expand All @@ -38,8 +49,15 @@ public abstract class ConcreteReference implements Reference {
@Override
public String name() {
StringBuilder sb = new StringBuilder();
if (this.equals(TypeNode.WILDCARD_REFERENCE)) {
if (isWildcard()) {
sb.append(QUESTION_MARK);
if (wildcardUpperBound() != null) {
// Handle the upper bound.
sb.append(SPACE);
sb.append(EXTENDS);
sb.append(SPACE);
sb.append(wildcardUpperBound().name());
}
} else {
if (hasEnclosingClass() && !isStaticImport()) {
sb.append(clazz().getEnclosingClass().getSimpleName());
Expand Down Expand Up @@ -117,19 +135,28 @@ public boolean isAssignableFrom(Reference other) {
return clazz().isAssignableFrom(((ConcreteReference) other).clazz());
}

@Override
public boolean isWildcard() {
return clazz().equals(WILDCARD_CLAZZ);
}

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

ConcreteReference ref = (ConcreteReference) o;
return clazz().equals(ref.clazz()) && generics().equals(ref.generics());
return clazz().equals(ref.clazz())
&& generics().equals(ref.generics())
&& Objects.equals(wildcardUpperBound(), ref.wildcardUpperBound());
}

@Override
public int hashCode() {
return 17 * clazz().hashCode() + 31 * generics().hashCode();
int wildcardUpperBoundHash =
wildcardUpperBound() == null ? 0 : 11 * wildcardUpperBound().hashCode();
return 17 * clazz().hashCode() + 31 * generics().hashCode() + wildcardUpperBoundHash;
}

@Override
Expand All @@ -141,6 +168,14 @@ public static ConcreteReference withClazz(Class clazz) {
return builder().setClazz(clazz).build();
}

public static ConcreteReference wildcard() {
return withClazz(ReferenceWildcard.class);
}

public static ConcreteReference wildcardWithUpperBound(Reference upperBoundReference) {
return builder().setClazz(WILDCARD_CLAZZ).setWildcardUpperBound(upperBoundReference).build();
}

public static Builder builder() {
return new AutoValue_ConcreteReference.Builder()
.setGenerics(ImmutableList.of())
Expand All @@ -154,6 +189,8 @@ public static Builder builder() {
public abstract static class Builder {
public abstract Builder setClazz(Class clazz);

public abstract Builder setWildcardUpperBound(Reference reference);

public abstract Builder setGenerics(List<Reference> clazzes);

public abstract Builder setIsStaticImport(boolean isStaticImport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public interface Reference {
@Nullable
String enclosingClassName();

@Nullable
Reference wildcardUpperBound();

Reference copyAndSetGenerics(List<Reference> generics);

// Valid only for nested classes.
boolean isStaticImport();

Expand All @@ -42,5 +47,5 @@ public interface Reference {

boolean isAssignableFrom(Reference other);

Reference copyAndSetGenerics(List<Reference> generics);
boolean isWildcard();
}
20 changes: 17 additions & 3 deletions src/main/java/com/google/api/generator/engine/ast/TypeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
@AutoValue
public abstract class TypeNode implements AstNode {
static final Reference EXCEPTION_REFERENCE = ConcreteReference.withClazz(Exception.class);
public static final Reference WILDCARD_REFERENCE =
ConcreteReference.withClazz(ReferenceWildcard.class);
public static final Reference WILDCARD_REFERENCE = ConcreteReference.wildcard();

public enum TypeKind {
BYTE,
Expand Down Expand Up @@ -101,7 +100,22 @@ public abstract static class Builder {

public abstract Builder setReference(Reference reference);

public abstract TypeNode build();
// Private.
abstract Reference reference();

abstract TypeNode autoBuild();

public TypeNode build() {
if (reference() != null) {
// Disallow top-level wildcard references.
Preconditions.checkState(
!reference().isWildcard(),
String.format(
"The top-level referenece in a type cannot be a wildcard, found %s",
reference().name()));
}
return autoBuild();
}
}

// TODO(miraleung): More type creation helpers to come...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public abstract class VaporReference implements Reference {
@Override
public abstract String enclosingClassName();

@Nullable
@Override
public Reference wildcardUpperBound() {
return null;
}

@Override
public String fullName() {
// TODO(unsupported): Nested classes with depth greater than 1.
Expand Down Expand Up @@ -75,6 +81,11 @@ public boolean isAssignableFrom(Reference other) {
return false;
}

@Override
public boolean isWildcard() {
return false;
}

abstract String plainName();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.google.api.generator.engine.ast.WhileStatement;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -340,9 +341,15 @@ private void variableExpressions(List<VariableExpr> expressions) {
private void references(List<Reference> refs) {
for (Reference ref : refs) {
// Don't need to import this.
if ((!ref.isStaticImport()
&& (ref.isFromPackage(PKG_JAVA_LANG) || ref.isFromPackage(currentPackage)))
|| ref.equals(TypeNode.WILDCARD_REFERENCE)) {
if (!ref.isStaticImport()
&& (ref.isFromPackage(PKG_JAVA_LANG) || ref.isFromPackage(currentPackage))) {
continue;
}

if (ref.isWildcard()) {
if (ref.wildcardUpperBound() != null) {
references(Arrays.asList(ref.wildcardUpperBound()));
}
continue;
}

Expand Down
Loading