Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3054adc
feat: parse grpc_service_config.json
miraleung Aug 20, 2020
6398d2a
feat: pipe ServiceConfig into settings codegen
miraleung Aug 20, 2020
db304ed
Merge branch 'master' of github.com:googleapis/gapic-generator-java i…
miraleung Aug 20, 2020
6e03c6a
feat: add settings fields to ServiceStubSettings codegen
miraleung Aug 21, 2020
f0a851a
feat: add settings getters to ServiceStubSettings codegen
miraleung Aug 21, 2020
8448c3b
fix!: Refactor ThrowExpr msgs to String-typed exprs
miraleung Aug 21, 2020
e9f96c1
feat: add createStub to ServiceStubSettings codegen
miraleung Aug 21, 2020
6e049c0
feat: add default* helperMethods to ServiceStubSettings codegen
miraleung Aug 21, 2020
36308fd
feat: add static refs to VariableExpr
miraleung Aug 21, 2020
f28b817
feat: add more default* helpers to ServiceStubSettings codegen
miraleung Aug 21, 2020
98d303c
feat: support Foo.class field refs in VariableExpr
miraleung Aug 22, 2020
d8f6143
feat: add apiDefaultHelper to ServiceStubSettings codegen
miraleung Aug 22, 2020
34c25c9
feat: add builder helpers to ServiceStubSettings codegen
miraleung Aug 22, 2020
16207d0
feat: support this/super ctor in ExprStatements
miraleung Aug 22, 2020
9e50411
fix: clean up error msgs, add varargs in RefCtorExpr
miraleung Aug 22, 2020
bb04a41
feat: add ctor to ServiceStubSettings codegen
miraleung Aug 22, 2020
c6853c7
fix: use super in ServiceSettings codegen
miraleung Aug 22, 2020
ada6b50
fix: use this and new in ServiceSettings codegen
miraleung Aug 22, 2020
4b80cd0
feat: use this ctor in GrpcServiceStub codegen
miraleung Aug 22, 2020
9ff208a
feat: fill out util methods in ServiceClient codegen
miraleung Aug 22, 2020
6a94f6a
feat: add creator methods to ServiceClient codegen
miraleung Aug 22, 2020
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
18 changes: 18 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ java_proto_library(
],
)

java_proto_library(
name = "rpc_java_proto",
visibility = ["//:__subpackages__"],
deps = [
"@com_google_googleapis//google/rpc:code_proto",
"@com_google_googleapis//google/rpc:error_details_proto",
"@com_google_googleapis//google/rpc:status_proto",
],
)

java_proto_library(
name = "service_config_java_proto",
visibility = ["//:__subpackages__"],
deps = [
"@io_grpc_proto//:service_config_proto",
],
)

# ============= Binary targets ================

java_binary(
Expand Down
9 changes: 9 additions & 0 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ def com_google_api_codegen_repositories():
strip_prefix = "grpc-java-%s" % _io_grpc_version,
)

# grpc-proto doesn't have releases, so we use hashes instead.
_io_grpc_proto_prefix = "0020624375a8ee4c7dd9b3e513e443b90bc28990" # Aug. 20, 2020.
_maybe(
http_archive,
name = "io_grpc_proto",
urls = ["https://github.com/grpc/grpc-proto/archive/%s.zip" % _io_grpc_proto_prefix],
strip_prefix = "grpc-proto-%s" % _io_grpc_proto_prefix,
)

def _maybe(repo_rule, name, strip_repo_prefix = "", **kwargs):
if not name.startswith(strip_repo_prefix):
return
Expand Down
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Usage example here:
: << 'EXAMPLE'
DIR=~/dev/googleapis/google/showcase/v1beta1
./run.sh --g ~/dev/googleapis --p "$DIR"-s "$DIR/showcase_grpc_config.json"
./run.sh --g ~/dev/googleapis --p "$DIR"-s "$DIR/showcase_grpc_service_config.json"
EXAMPLE

source gbash.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ public ExprStatement build() {
} else {
Preconditions.checkState(
(expr instanceof MethodInvocationExpr)
|| (expr instanceof ReferenceConstructorExpr)
|| (expr instanceof AssignmentExpr)
|| (expr instanceof ThrowExpr)
|| (expr instanceof ReturnExpr),
"Expression statements must be either a method invocation, assignment, throw, or"
+ " return expression");
"Expression statements must be either a method invocation, assignment, throw, "
+ "this/super constructor, or return expression");
}
return exprStatement;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ public abstract static class Builder {

abstract IdentifierNode autoBuild();

public IdentifierNode buildVariableIdentifier() throws InvalidIdentifierException {
return build(/* isField= */ true);
}

public IdentifierNode build() throws InvalidIdentifierException {
return build(/* isField= */ false);
}

// Private.
IdentifierNode build(boolean isField) throws InvalidIdentifierException {
IdentifierNode identifier = autoBuild();
String identifierName = identifier.name();
Preconditions.checkNotNull(identifierName);
Expand All @@ -84,9 +93,17 @@ public IdentifierNode build() throws InvalidIdentifierException {
String.format("Name %s cannot contain non-alphanumeric characters", identifierName));
}

if (Keyword.isKeyword(identifierName)) {
throw new InvalidIdentifierException(
String.format("Name %s cannot be a keyword.", identifierName));
if (isField) {
if (Keyword.isInvalidFieldName(identifierName)) {
throw new InvalidIdentifierException(
String.format("Name %s cannot be a keyword.", identifierName));
}

} else {
if (Keyword.isKeyword(identifierName)) {
throw new InvalidIdentifierException(
String.format("Name %s cannot be a keyword.", identifierName));
}
}

return identifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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.Collections;
import java.util.List;

Expand Down Expand Up @@ -54,6 +55,10 @@ public static Builder superBuilder() {
public abstract static class Builder {
public abstract Builder setType(TypeNode node);

public Builder setArguments(Expr... arguments) {
return setArguments(Arrays.asList(arguments));
}

public abstract Builder setArguments(List<Expr> arguments);

// private.
Expand All @@ -65,7 +70,7 @@ public ReferenceConstructorExpr build() {
ReferenceConstructorExpr referenceConstructorExpr = autoBuild();
Preconditions.checkState(
referenceConstructorExpr.type().isReferenceType(referenceConstructorExpr.type()),
"ReferenceConstructExpr type must be reference type. ");
"A this/super constructor must have a reference type.");
return referenceConstructorExpr;
}
}
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class ThrowExpr implements Expr {
public abstract TypeNode type();

@Nullable
public abstract String message();
public abstract Expr messageExpr();

@Override
public void accept(AstNodeVisitor visitor) {
Expand All @@ -41,19 +41,29 @@ public static Builder builder() {
public abstract static class Builder {
public abstract Builder setType(TypeNode type);

public abstract Builder setMessage(String message);
public Builder setMessageExpr(String message) {
return setMessageExpr(ValueExpr.withValue(StringObjectValue.withValue(message)));
}

public abstract Builder setMessageExpr(Expr expr);

// Private.
abstract TypeNode type();

abstract Expr messageExpr();

abstract ThrowExpr autoBuild();

public ThrowExpr build() {
// TODO(miraleung): Escaping message() and the corresponding tests will be done when we switch
// to StringObjectValue.
ThrowExpr throwExpr = autoBuild();
Preconditions.checkState(
TypeNode.isExceptionType(throwExpr.type()),
String.format("Type %s must be an exception type", throwExpr.type()));
return throwExpr;
TypeNode.isExceptionType(type()),
String.format("Type %s must be an exception type", type()));
if (messageExpr() != null) {
Preconditions.checkState(
messageExpr().type().equals(TypeNode.STRING),
String.format("Message expression type must be a string for exception %s", type()));
}
return autoBuild();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public abstract static class Builder {
abstract Variable autoBuild();

public Variable build() {
IdentifierNode identifier = IdentifierNode.builder().setName(name()).build();
IdentifierNode identifier =
IdentifierNode.builder().setName(name()).buildVariableIdentifier();
setIdentifier(identifier);

Variable variable = autoBuild();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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

import com.google.api.generator.engine.lexicon.Keyword;
import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand All @@ -28,6 +29,9 @@ public abstract class VariableExpr implements Expr {
@Nullable
public abstract Expr exprReferenceExpr();

@Nullable
public abstract TypeNode staticReferenceType();

/** Variable declaration fields. */
public abstract boolean isDecl();

Expand Down Expand Up @@ -85,6 +89,9 @@ public abstract static class Builder {
// Optional, but cannot co-exist with a variable declaration.
public abstract Builder setExprReferenceExpr(Expr exprReference);

// Optional, but cannot co-exist with an expression reference.
public abstract Builder setStaticReferenceType(TypeNode type);

public abstract Builder setIsDecl(boolean isDecl);

public abstract Builder setScope(ScopeNode scope);
Expand Down Expand Up @@ -126,11 +133,35 @@ public VariableExpr build() {
variableExpr.isDecl() ^ (variableExpr.exprReferenceExpr() != null),
"Variable references cannot be declared");
}

Preconditions.checkState(
variableExpr.exprReferenceExpr() == null || variableExpr.staticReferenceType() == null,
"Only the expression reference or the static reference can be set, not both");

if (variableExpr.exprReferenceExpr() != null) {
Preconditions.checkState(
TypeNode.isReferenceType(variableExpr.exprReferenceExpr().type()),
"Variables can only be referenced from object types");
}
if (variableExpr.staticReferenceType() != null) {
Preconditions.checkState(
TypeNode.isReferenceType(variableExpr.staticReferenceType()),
String.format(
"Static field references can only be done on static types, but instead found %s",
variableExpr.staticReferenceType()));
}

// A variable name of "class" is valid only when it's a static reference.
String varName = variableExpr.variable().identifier().name();
if (Keyword.isKeyword(varName)) {
Preconditions.checkState(
variableExpr.staticReferenceType() != null
|| (variableExpr.exprReferenceExpr() != null
&& TypeNode.isReferenceType(variableExpr.exprReferenceExpr().type())),
String.format(
"Variable field name %s is invalid on non-static or non-reference types", varName));
}

return variableExpr;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.google.common.collect.ImmutableList;

public class Keyword {
// This is a valid field for all objects, so handle particular keyword differently.
private static final String CLASS_KEYWORD = "class";

private static final ImmutableList<String> KEYWORDS =
ImmutableList.of(
"abstract",
Expand Down Expand Up @@ -59,7 +62,6 @@ public class Keyword {
"interface",
"static",
"void",
"class",
"finally",
"long",
"strictfp",
Expand All @@ -71,6 +73,10 @@ public class Keyword {
"while");

public static boolean isKeyword(String s) {
return s.equals(CLASS_KEYWORD) || KEYWORDS.contains(s);
}

public static boolean isInvalidFieldName(String s) {
return KEYWORDS.contains(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public void visit(VariableExpr variableExpr) {
if (variableExpr.exprReferenceExpr() != null) {
variableExpr.exprReferenceExpr().accept(this);
}
if (variableExpr.staticReferenceType() != null) {
variableExpr.staticReferenceType().accept(this);
}
variableExpr.templateNodes().stream().forEach(n -> n.accept(this));
}

Expand All @@ -146,6 +149,8 @@ public void visit(AssignmentExpr assignmentExpr) {

@Override
public void visit(MethodInvocationExpr methodInvocationExpr) {
// May not actually be used in source, but import it anyway. Unused imports will be removed by
// the formatter.
methodInvocationExpr.returnType().accept(this);
if (methodInvocationExpr.staticReferenceType() != null) {
methodInvocationExpr.staticReferenceType().accept(this);
Expand Down Expand Up @@ -173,6 +178,9 @@ public void visit(AnonymousClassExpr anonymousClassExpr) {
@Override
public void visit(ThrowExpr throwExpr) {
throwExpr.type().accept(this);
if (throwExpr.messageExpr() != null) {
throwExpr.messageExpr().accept(this);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.engine.ast.WhileStatement;
import com.google.common.base.Strings;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -202,9 +201,15 @@ public void visit(VariableExpr variableExpr) {
rightAngle();
}
space();
} else if (variableExpr.exprReferenceExpr() != null) {
variableExpr.exprReferenceExpr().accept(this);
buffer.append(DOT);
} else {
// Expression or static reference.
if (variableExpr.exprReferenceExpr() != null) {
variableExpr.exprReferenceExpr().accept(this);
buffer.append(DOT);
} else if (variableExpr.staticReferenceType() != null) {
variableExpr.staticReferenceType().accept(this);
buffer.append(DOT);
}
}

variable.identifier().accept(this);
Expand Down Expand Up @@ -304,11 +309,8 @@ public void visit(ThrowExpr throwExpr) {
space();
throwExpr.type().accept(this);
leftParen();
if (!Strings.isNullOrEmpty(throwExpr.message())) {
// TODO(miraleung): Update this when we use StringObjectValue.
buffer.append(ESCAPED_QUOTE);
buffer.append(throwExpr.message());
buffer.append(ESCAPED_QUOTE);
if (throwExpr.messageExpr() != null) {
throwExpr.messageExpr().accept(this);
}
rightParen();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ java_library(
deps = [
"//:longrunning_java_proto",
"//:monitored_resource_java_proto",
"//:service_config_java_proto",
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/gapic:status_java_proto",
"//src/main/java/com/google/api/generator/gapic/model",
Expand Down
Loading