Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

feat: support conditional policies #110

Merged
merged 36 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
67e52ce
Base implementation
frankyn Dec 17, 2019
84fcfea
Update with unit tests
frankyn Dec 20, 2019
145f7a0
lint
frankyn Dec 20, 2019
ac1fd6e
correct copyright date
frankyn Jan 7, 2020
2c8724b
lint
frankyn Jan 7, 2020
c90dbb0
Revert removal of helper functions
frankyn Jan 8, 2020
b0a617f
use auto-value
frankyn Jan 22, 2020
9dbfc2d
Merge branch 'master' into conditional-policy
frankyn Jan 22, 2020
54ad076
reformat Binding.java and Condition.java
frankyn Jan 22, 2020
ff49620
remove unnecessary dep
frankyn Jan 22, 2020
298b2be
code format
frankyn Jan 22, 2020
aaebba4
add dep on com.google.code.findbugs in google-cloud-core
frankyn Jan 22, 2020
a5f63ea
address comments
frankyn Feb 6, 2020
4873b73
Merge branch 'master' into conditional-policy
frankyn Feb 11, 2020
c853a84
Clean up
frankyn Feb 11, 2020
c64e55a
Merge branch 'conditional-policy' of github.com:frankyn/java-core int…
frankyn Feb 11, 2020
d2fab21
respond to comments
frankyn Feb 19, 2020
86cd863
Merge branch 'master' into conditional-policy
frankyn Feb 19, 2020
085959d
address comments
frankyn Feb 20, 2020
174e8c4
format
frankyn Feb 20, 2020
14e1aac
address feedback
frankyn Feb 21, 2020
fdb040a
remove unnecessary null check
frankyn Feb 21, 2020
42199d1
lint
frankyn Feb 21, 2020
bbb708a
address feedback
frankyn Feb 21, 2020
7f0e33e
remove ImmutableList from Binding AutoValue surface
frankyn Feb 21, 2020
108faec
address feedback
frankyn Feb 25, 2020
79126b5
split up unit test
frankyn Feb 25, 2020
505f9bc
use guava beta annotation
frankyn Feb 25, 2020
a89ef0c
surface ImmutableList<> for Binding class.
frankyn Feb 25, 2020
f0b5085
use BetaApi from api.core
frankyn Feb 25, 2020
9f5e600
return as expected
frankyn Feb 26, 2020
8580f5a
partial addressing of feedback
frankyn Feb 26, 2020
9fe4358
address feedback pt2
frankyn Feb 26, 2020
8f48a15
address remaining feedback
frankyn Feb 26, 2020
615ba06
Merge branch 'master' into conditional-policy
frankyn Feb 26, 2020
2b56641
address one last feedback
frankyn Feb 26, 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
8 changes: 8 additions & 0 deletions google-cloud-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
</dependency>
<dependency>
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
Expand Down Expand Up @@ -83,6 +87,10 @@
<artifactId>objenesis</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
Expand Down
82 changes: 82 additions & 0 deletions google-cloud-core/src/main/java/com/google/cloud/Binding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.cloud;

import com.google.api.core.BetaApi;
import com.google.auto.value.AutoValue;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;

@BetaApi("This is a Beta API is not stable yet and may change in the future.")
@AutoValue
public abstract class Binding {
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd very much recommend Javadoc for public types and public methods that aren't obvious.

public abstract String getRole();

public abstract ImmutableList<String> getMembers();

@Nullable
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why @Nullable instead of a non-nullable Optional<Condition>?

Either is okay, but it's interesting that you're using both patterns in the same class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I picked Nullable because it made more sense to me in this case. I'm not well versed in Optional's. Both patterns? Not sure I understand.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I thought for some reason that you were also using Optional. Don't know why I thought that.

public abstract Condition getCondition();

public abstract Builder toBuilder();

public static Builder newBuilder() {
return new AutoValue_Binding.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setRole(String role);

public abstract Builder setMembers(List<String> members);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could even take Iterable<String> if you want to be most general.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, addressing.


public abstract Builder setCondition(Condition condition);

public abstract String getRole();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used from anywhere? Do you need it? Generally, builders don't need getters.

Same for getCondition().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed to reduce surface size. At the moment they were convenience but I'd like to get more developer input before adding unnecessary methods to the public surface.


public abstract ImmutableList<String> getMembers();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you expect this to be called other than from addMembers(String...) and removeMembers(String...)? If not, make this have default access.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, good point.


public abstract Condition getCondition();

// Members property must be initialized before this method can be used.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid this constraint, newBuilder() could call setMembers(ImmutableList.of()).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet cohesive behavior. ty!

public Builder addMembers(String... members) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't make sense to call this with no arguments, right? If you change it to the following, then that wouldn't even compile.

Suggested change
public Builder addMembers(String... members) {
public Builder addMembers(String member, String... moreMembers) {

You can then use Guava's Lists.asList(member, moreMembers) (a good static import method) to combine those two into one list to add.

ImmutableList.Builder<String> membersBuilder = ImmutableList.builder();
membersBuilder.addAll(getMembers());
for (String member : members) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (String member : members) {
membersBuilder.add(members);

That should work, no?

membersBuilder.add(member);
}
setMembers(membersBuilder.build());
return this;
}

// Members property must be initialized before this method can be used.
public Builder removeMembers(String... members) {
Predicate<String> selectMembersNotInList =
Predicates.not(Predicates.in(Arrays.asList(members)));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nit: Predicates.not and Predicates.in look good when they're statically imported. Arrays.asList too.

Suggested change
Predicates.not(Predicates.in(Arrays.asList(members)));
not(in(asList(members)));

Collection<String> filter = Collections2.filter(getMembers(), selectMembersNotInList);
setMembers(ImmutableList.copyOf(filter));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to make a copy if setMembers(List) takes Iterable.

return this;
}

public abstract Binding build();
}
}
51 changes: 51 additions & 0 deletions google-cloud-core/src/main/java/com/google/cloud/Condition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.cloud;

import com.google.api.core.BetaApi;
import com.google.auto.value.AutoValue;
import javax.annotation.Nullable;

@BetaApi("This is a Beta API is not stable yet and may change in the future.")
@AutoValue
public abstract class Condition {
@Nullable
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these nullable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, no reason other than my first attempt with AutoValue. removing.

public abstract String getTitle();

@Nullable
public abstract String getDescription();

@Nullable
public abstract String getExpression();

public abstract Builder toBuilder();

public static Builder newBuilder() {
return new AutoValue_Condition.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setTitle(String title);

public abstract Builder setDescription(String description);

public abstract Builder setExpression(String expression);

public abstract Condition build();
}
}
Loading