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

[operators][integrate](2/2)feat: Add HashCode in resource name #368

Merged
merged 29 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b730569
Add Equals method in ResourceName
summer-ji-eng Oct 1, 2020
d82d56b
correct function position
summer-ji-eng Oct 1, 2020
8d47696
Merge branch 'master' into resource_name_equals
summer-ji-eng Oct 1, 2020
7859d17
Add equals method in SessionName.golden
summer-ji-eng Oct 1, 2020
f15f2a1
Add comments in method
summer-ji-eng Oct 2, 2020
7397f11
Merge branch 'master' into resource_name_equals
summer-ji-eng Oct 2, 2020
b1d92c4
feat: Add HashCode in resource name
summer-ji-eng Oct 6, 2020
7bb9fef
Merge branch 'master' into resource_name_hashcode
summer-ji-eng Oct 6, 2020
9403468
Merge branch 'master' into resource_name_hashcode
summer-ji-eng Oct 6, 2020
dc5ae89
remove unused arg
summer-ji-eng Oct 6, 2020
ee58b82
Merge branch 'resource_name_hashcode' of github.com:googleapis/gapic-…
summer-ji-eng Oct 6, 2020
eb94e77
Merge branch 'master' into resource_name_hashcode
summer-ji-eng Oct 7, 2020
443ed6f
Merge branch 'master' into resource_name_hashcode
summer-ji-eng Oct 7, 2020
05d9c9e
[ggj][infra][5/5] support goldens updates for composer tests (#348)
xiaozhenliu-gg5 Oct 2, 2020
95cd0f5
[ggj][engx] fix: use a VM for CircleCI builds (#367)
miraleung Oct 5, 2020
8ba5c8e
fix: use imperative bazel rule names (#361)
miraleung Oct 6, 2020
4eaef97
[ggj][bazel][goldens] fix: refactor and fix bazel golden_update rules…
miraleung Oct 6, 2020
4526eb4
feat: Add HashCode in resource name
summer-ji-eng Oct 6, 2020
79e98a6
remove unused arg
summer-ji-eng Oct 6, 2020
82bc8e6
fix: add final keyword check on assignment expr (#366)
summer-ji-eng Oct 6, 2020
218c7ff
fix: add final keyword checking in post increment operation expr (#365)
summer-ji-eng Oct 6, 2020
60054ea
Merge branch 'resource_name_hashcode' of github.com:googleapis/gapic-…
summer-ji-eng Oct 7, 2020
9dd01bc
remove files
summer-ji-eng Oct 7, 2020
d6d3e36
Merge branch 'master' into resource_name_hashcode
summer-ji-eng Oct 7, 2020
aaf0d95
rebase
summer-ji-eng Oct 7, 2020
d5c627a
Merge branch 'master' into resource_name_hashcode
summer-ji-eng Oct 7, 2020
ca17c43
Merge branch 'resource_name_hashcode' of github.com:googleapis/gapic-…
summer-ji-eng Oct 7, 2020
0f5681a
fix resolve
summer-ji-eng Oct 7, 2020
758e898
revert readme
summer-ji-eng Oct 7, 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
Prev Previous commit
Next Next commit
Merge branch 'master' into resource_name_hashcode
  • Loading branch information
summer-ji-eng authored Oct 7, 2020
commit d5c627a436dba7ace3140d667a8568128bd8dd45
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.AssignmentOperationExpr;
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.ConcreteReference;
Expand Down Expand Up @@ -273,6 +274,7 @@ private static List<MethodDefinition> createClassMethods(
createFieldValueGetterMethods(resourceName, patternTokenVarExprs, tokenHierarchies, types));
javaMethods.add(
createToStringMethod(templateFinalVarExprs, patternTokenVarExprs, tokenHierarchies));
javaMethods.add(createEqualsMethod(resourceName, tokenHierarchies, types));
javaMethods.add(createHashCodeMethod(tokenHierarchies));
return javaMethods;
}
Expand Down Expand Up @@ -1145,6 +1147,103 @@ private static MethodDefinition createToStringMethod(
.build();
}

private static MethodDefinition createEqualsMethod(
ResourceName resourceName, List<List<String>> tokenHierarchies, Map<String, TypeNode> types) {
// Create method definition variables.
Variable oVariable = Variable.builder().setType(TypeNode.OBJECT).setName("o").build();
VariableExpr argVarExpr =
VariableExpr.builder().setIsDecl(false).setVariable(oVariable).build();
TypeNode thisClassType = types.get(getThisClassName(resourceName));
ValueExpr thisValueExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
ValueExpr trueValueExpr =
ValueExpr.withValue(
PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build());

// Create first if statement's return expression
ReturnExpr returnTrueExpr = ReturnExpr.withExpr(trueValueExpr);

// Create second if statement's condition expression
RelationalOperationExpr oEqualsThisExpr =
RelationalOperationExpr.equalToWithExprs(argVarExpr, thisValueExpr);
RelationalOperationExpr oNotEqualsNullExpr =
RelationalOperationExpr.notEqualToWithExprs(
argVarExpr, ValueExpr.withValue(NullObjectValue.create()));
MethodInvocationExpr getClassMethodInvocationExpr =
MethodInvocationExpr.builder().setMethodName("getClass").build();
RelationalOperationExpr getClassEqualsExpr =
RelationalOperationExpr.equalToWithExprs(
getClassMethodInvocationExpr,
getClassMethodInvocationExpr.toBuilder().setExprReferenceExpr(argVarExpr).build());
LogicalOperationExpr orLogicalExpr =
LogicalOperationExpr.logicalOrWithExprs(oNotEqualsNullExpr, getClassEqualsExpr);

// Create second if statement's body assignment expression.
Variable thatVariable = Variable.builder().setName("that").setType(thisClassType).build();
VariableExpr thatVariableExpr =
VariableExpr.builder().setIsDecl(false).setVariable(thatVariable).build();
CastExpr oCastExpr = CastExpr.builder().setExpr(argVarExpr).setType(thisClassType).build();
AssignmentExpr thatAssignmentExpr =
AssignmentExpr.builder()
.setVariableExpr(thatVariableExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(oCastExpr)
.build();

// Create return expression in the second if statement's body.
Set<String> tokenSet = getTokenSet(tokenHierarchies);
Iterator<String> itToken = tokenSet.iterator();
Expr curTokenExpr =
createObjectsEqualsForTokenMethodEpxr(
thisValueExpr,
thatVariableExpr,
Variable.builder()
.setType(TypeNode.STRING)
.setName(JavaStyle.toLowerCamelCase(itToken.next()))
.build());
while (itToken.hasNext()) {
Expr nextTokenExpr =
createObjectsEqualsForTokenMethodEpxr(
thisValueExpr,
thatVariableExpr,
Variable.builder()
.setType(TypeNode.STRING)
.setName(JavaStyle.toLowerCamelCase(itToken.next()))
.build());
curTokenExpr = LogicalOperationExpr.logicalAndWithExprs(curTokenExpr, nextTokenExpr);
}
ReturnExpr secondIfReturnExpr = ReturnExpr.withExpr(curTokenExpr);

// Code: if (o == this) { return true;}
IfStatement firstIfStatement =
IfStatement.builder()
.setConditionExpr(oEqualsThisExpr)
.setBody(Arrays.asList(ExprStatement.withExpr(returnTrueExpr)))
.build();
// Code: if (o != null || getClass() == o.getClass()) { FoobarName that = ((FoobarName) o);
// return ..}
IfStatement secondIfStatement =
IfStatement.builder()
.setConditionExpr(orLogicalExpr)
.setBody(
Arrays.asList(
ExprStatement.withExpr(thatAssignmentExpr),
ExprStatement.withExpr(secondIfReturnExpr)))
.build();

// Create method's return expression.
ValueExpr falseValueExpr =
ValueExpr.withValue(
PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("false").build());

return MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(TypeNode.INT)
.setName("hashCode")
.setBody(asgmtBody)
.setReturnExpr(hVarExpr)
.build();
}

private static MethodDefinition createHashCodeMethod(List<List<String>> tokenHierarchies) {
List<Statement> asgmtBody = new ArrayList<>();
// code: int h = 1;
Expand Down Expand Up @@ -1192,24 +1291,34 @@ private static MethodDefinition createHashCodeMethod(List<List<String>> tokenHie
AssignmentOperationExpr.xorAssignmentWithExprs(
hVarExpr, createObjectsHashCodeForVarMethod(tokenVarExpr))));
});
}

return MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(TypeNode.INT)
.setName("hashCode")
.setBody(asgmtBody)
.setReturnExpr(hVarExpr)
private static MethodInvocationExpr createObjectsEqualsForTokenMethodEpxr(
Expr thisExpr, Expr thatExpr, Variable tokenVar) {
VariableExpr varThisExpr =
VariableExpr.builder().setVariable(tokenVar).setExprReferenceExpr(thisExpr).build();
VariableExpr varThatExpr =
VariableExpr.builder().setVariable(tokenVar).setExprReferenceExpr(thatExpr).build();
return MethodInvocationExpr.builder()
.setStaticReferenceType(STATIC_TYPES.get("Objects"))
.setMethodName("equals")
.setArguments(Arrays.asList(varThisExpr, varThatExpr))
.setReturnType(TypeNode.BOOLEAN)
.build();
}

private static MethodInvocationExpr createObjectsHashCodeForVarMethod(VariableExpr varExpr) {
// code: Objects.hashCode(varExpr)
return MethodInvocationExpr.builder()
.setMethodName("hashCode")
.setStaticReferenceType(STATIC_TYPES.get("Objects"))
.setArguments(varExpr)
.setReturnType(TypeNode.INT)
.setArguments(argVarExpr.toBuilder().setIsDecl(true).build())
.setReturnType(TypeNode.BOOLEAN)
.setName("equals")
.setReturnExpr(falseValueExpr)
.setBody(Arrays.asList(firstIfStatement, secondIfStatement))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ public class FoobarName implements ResourceName {
h ^= Objects.hashCode(barFoo);
return h;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
FoobarName that = ((FoobarName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.foobar, that.foobar)
&& Objects.equals(this.variant, that.variant)
&& Objects.equals(this.barFoo, that.barFoo);
}
return false;
}

/** Builder for projects/{project}/foobars/{foobar}. */
public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public class SessionName implements ResourceName {
h ^= Objects.hashCode(session);
return h;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
SessionName that = ((SessionName) o);
return Objects.equals(this.session, that.session);
}
return false;
}

/** Builder for sessions/{session}. */
public static class Builder {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.