Skip to content
Closed
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
aecb241
fix bazel rules
xiaozhenliu-gg5 Aug 6, 2020
45dbadb
Merge branch 'master' of github.com:googleapis/gapic-generator-java
xiaozhenliu-gg5 Aug 8, 2020
3d660f3
Merge branch 'master' of github.com:googleapis/gapic-generator-java
xiaozhenliu-gg5 Aug 10, 2020
71e1955
Merge branch 'master' of github.com:googleapis/gapic-generator-java
xiaozhenliu-gg5 Aug 12, 2020
598f9a7
Merge branch 'master' of github.com:googleapis/gapic-generator-java
xiaozhenliu-gg5 Aug 13, 2020
8d541af
Merge branch 'master' of github.com:googleapis/gapic-generator-java
xiaozhenliu-gg5 Aug 19, 2020
c71f061
feat: add factory var decl in ServiceStubSettings codegen
miraleung Aug 25, 2020
4919bba
fix: prevent duplicate MethodDefinition annotations
miraleung Aug 25, 2020
b77848e
feat: add descriptor fields to ServiceStubSettings codegen
miraleung Aug 25, 2020
861f142
feat: add starter Builder to ServiceStubSettings codegen
miraleung Aug 25, 2020
5f285df
feat: add settings.builder decls to ServiceStubSettings codegen
miraleung Aug 25, 2020
dbc5735
feat: add first nested ctors to ServiceStubSettings codegen
miraleung Aug 26, 2020
164c0b3
feat: add GapicServiceConfig DS and processing
miraleung Aug 26, 2020
3783203
feat: integrate GapicServiceConfig into GapicContext, Parser, Composer
miraleung Aug 26, 2020
3984eaa
feat: initial param block, RetrySettingsComposer, test
miraleung Aug 26, 2020
a0ef19e
fix!: refactor GapicRetrySettings
miraleung Aug 27, 2020
74dbcbb
fix: recognize 1. or .1 double patterns
miraleung Aug 27, 2020
34285db
feat: support BlockStatement in ClassDef stmts
miraleung Aug 27, 2020
c71e180
Merge branch 'master' of github.com:googleapis/gapic-generator-java
xiaozhenliu-gg5 Aug 27, 2020
281afc0
Merge branch 'gp/g11' of github.com:googleapis/gapic-generator-java i…
xiaozhenliu-gg5 Aug 27, 2020
5eb0104
add comment statement check for class defininition
xiaozhenliu-gg5 Aug 27, 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
Original file line number Diff line number Diff line change
Expand Up @@ -163,26 +163,29 @@ public ClassDefinition build() {
}

for (Statement statement : classDef.statements()) {
// TODO(xiaozhenliu): Add CommentStatement check here.
Preconditions.checkState(
statement instanceof ExprStatement,
"Class statement type must be either an expression or comment statement");
Expr expr = ((ExprStatement) statement).expression();
if (expr instanceof VariableExpr) {
VariableExpr variableExpr = (VariableExpr) expr;
Preconditions.checkState(
variableExpr.isDecl(), "Class expression variable statements must be declarations");
Preconditions.checkState(
!variableExpr.scope().equals(ScopeNode.LOCAL),
"Class variable statement cannot have a local scope");
} else {
Preconditions.checkState(
expr instanceof AssignmentExpr,
"Class expression statement must be assignment or variable declaration");
VariableExpr variableExpr = ((AssignmentExpr) expr).variableExpr();
Preconditions.checkState(
!variableExpr.scope().equals(ScopeNode.LOCAL),
"Class variable in assignment statement cannot have a local scope");
statement instanceof ExprStatement
|| statement instanceof BlockStatement
|| statement instanceof CommentStatement,
"Class statement type must be either an expression, block, or comment statement");
if (statement instanceof ExprStatement) {
Expr expr = ((ExprStatement) statement).expression();
if (expr instanceof VariableExpr) {
VariableExpr variableExpr = (VariableExpr) expr;
Preconditions.checkState(
variableExpr.isDecl(), "Class expression variable statements must be declarations");
Preconditions.checkState(
!variableExpr.scope().equals(ScopeNode.LOCAL),
"Class variable statement cannot have a local scope");
} else {
Preconditions.checkState(
expr instanceof AssignmentExpr,
"Class expression statement must be assignment or variable declaration");
VariableExpr variableExpr = ((AssignmentExpr) expr).variableExpr();
Preconditions.checkState(
!variableExpr.scope().equals(ScopeNode.LOCAL),
"Class variable in assignment statement cannot have a local scope");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -122,10 +123,7 @@ public abstract static class Builder {
public abstract Builder setHeaderCommentStatements(
List<CommentStatement> headeCommentStatements);

public Builder setAnnotations(List<AnnotationNode> annotations) {
annotationsBuilder().addAll(annotations);
return this;
}
public abstract Builder setAnnotations(List<AnnotationNode> annotations);

public abstract Builder setIsStatic(boolean isStatic);

Expand Down Expand Up @@ -162,10 +160,10 @@ public Builder setArguments(VariableExpr... arguments) {

// Private accessors.

abstract ImmutableList.Builder<AnnotationNode> annotationsBuilder();

abstract String name();

abstract ImmutableList<AnnotationNode> annotations();

abstract TypeNode returnType();

abstract boolean isOverride();
Expand Down Expand Up @@ -231,9 +229,19 @@ public MethodDefinition build() {
}

// If this method overrides another, ensure that the Override annotaiton is the last one.
ImmutableList<AnnotationNode> processedAnnotations = annotations();
if (isOverride()) {
annotationsBuilder().add(AnnotationNode.OVERRIDE);
processedAnnotations =
annotations()
.<AnnotationNode>builder()
.addAll(annotations())
.add(AnnotationNode.OVERRIDE)
.build();
}
// Remove duplicates while maintaining insertion order.
setAnnotations(
new LinkedHashSet<AnnotationNode>(processedAnnotations)
.stream().collect(Collectors.toList()));

MethodDefinition method = autoBuild();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public class Literal {
private static final Pattern LONG_PATTERN = Pattern.compile("^[0-9]+[Ll]?$");
private static final Pattern FLOAT_PATTERN =
Pattern.compile("^[0-9]+([fF]|(\\.(([0-9]+[fF])|[fF])))?$");
private static final Pattern DOUBLE_PATTERN =
private static final Pattern DOUBLE_PATTERN_ONE =
Pattern.compile("^[0-9]+(\\.[0-9]+)?(\\.?[eE]\\-?[0-9]+)$");
private static final Pattern DOUBLE_PATTERN_TWO = Pattern.compile("^\\d*\\.\\d+$|^\\d+\\.\\d*$");

public static boolean isBooleanLiteral(String str) {
return str.equals(BOOLEAN_TRUE) || str.equals(BOOLEAN_FALSE);
Expand All @@ -46,7 +47,9 @@ public static boolean isFloatLiteral(String str) {
}

public static boolean isDoubleLiteral(String str) {
return isFloatLiteral(str) || DOUBLE_PATTERN.matcher(str).matches();
return isFloatLiteral(str)
|| DOUBLE_PATTERN_ONE.matcher(str).matches()
|| DOUBLE_PATTERN_TWO.matcher(str).matches();
}

public static boolean isNullLiteral(String str) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicClass.Kind;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicServiceConfig;
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.model.ResourceName;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.ApacheLicense;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.serviceconfig.ServiceConfig;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -45,7 +45,7 @@ public static List<GapicClass> composeServiceClasses(GapicContext context) {

public static List<GapicClass> generateServiceClasses(
@Nonnull Service service,
@Nullable ServiceConfig serviceConfig,
@Nullable GapicServiceConfig serviceConfig,
@Nonnull Map<String, Message> messageTypes) {
List<GapicClass> clazzes = new ArrayList<>();
clazzes.addAll(generateStubClasses(service, serviceConfig, messageTypes));
Expand All @@ -64,7 +64,7 @@ public static List<GapicClass> generateResourceNameHelperClasses(
}

public static List<GapicClass> generateStubClasses(
Service service, ServiceConfig serviceConfig, Map<String, Message> messageTypes) {
Service service, GapicServiceConfig serviceConfig, Map<String, Message> messageTypes) {
List<GapicClass> clazzes = new ArrayList<>();
clazzes.add(ServiceStubClassComposer.instance().generate(service, messageTypes));
clazzes.add(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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.gapic.composer;

import com.google.api.gax.retrying.RetrySettings;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.BlockStatement;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NullObjectValue;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.gapic.model.GapicServiceConfig;
import com.google.api.generator.gapic.model.Service;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RetrySettingsComposer {
private static final Map<String, TypeNode> STATIC_TYPES = createStaticTypes();

public static BlockStatement createRetryParamDefinitionsBlock(
Service service,
GapicServiceConfig serviceConfig,
VariableExpr retryParamDefinitionsClassMemberVarExpr) {
List<Expr> bodyExprs = new ArrayList<>();

TypeNode definitionsType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(ImmutableMap.Builder.class)
.setGenerics(retryParamDefinitionsClassMemberVarExpr.type().reference().generics())
.build());
VariableExpr definitionsVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(definitionsType).setName("definitions").build());
VariableExpr settingsVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setType(STATIC_TYPES.get("RetrySettings"))
.setName("settings")
.build());

// Create the first two exprs.
bodyExprs.add(
AssignmentExpr.builder()
.setVariableExpr(definitionsVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(
MethodInvocationExpr.builder()
.setStaticReferenceType(STATIC_TYPES.get("ImmutableMap"))
.setMethodName("builder")
.setReturnType(definitionsVarExpr.type())
.build())
.build());
bodyExprs.add(
AssignmentExpr.builder()
.setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(ValueExpr.withValue(NullObjectValue.create()))
.build());

// Build the settings object for each config.
// TODO(miraleung): Fill this out.

// Reassign the new settings.
bodyExprs.add(
AssignmentExpr.builder()
.setVariableExpr(retryParamDefinitionsClassMemberVarExpr)
.setValueExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(definitionsVarExpr)
.setMethodName("build")
.setReturnType(retryParamDefinitionsClassMemberVarExpr.type())
.build())
.build());

// Put everything together.
return BlockStatement.builder()
.setIsStatic(true)
.setBody(
bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()))
.build();
}

private static Map<String, TypeNode> createStaticTypes() {
List<Class> concreteClazzes =
Arrays.asList(org.threeten.bp.Duration.class, ImmutableMap.class, RetrySettings.class);
return concreteClazzes.stream()
.collect(
Collectors.toMap(
c -> c.getSimpleName(),
c -> TypeNode.withReference(ConcreteReference.withClazz(c))));
}
}
Loading