Skip to content

Make TransformResult non-nullable #2428

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

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public MaybeDocument applyToRemoteDocument(
verifyKeyMatches(maybeDoc);

hardAssert(
mutationResult.getTransformResults() == null,
mutationResult.getTransformResults().isEmpty(),
"Transform results received by DeleteMutation.");

// Unlike applyToLocalView, if we're applying a mutation to a remote document the server has
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.firebase.firestore.util.Preconditions.checkNotNull;

import androidx.annotation.Nullable;
import com.google.firebase.firestore.model.SnapshotVersion;
import com.google.firestore.v1.Value;
import java.util.List;
Expand All @@ -31,9 +30,9 @@
*/
public final class MutationResult {
private final SnapshotVersion version;
@Nullable private final List<Value> transformResults;
private final List<Value> transformResults;

public MutationResult(SnapshotVersion version, @Nullable List<Value> transformResults) {
public MutationResult(SnapshotVersion version, List<Value> transformResults) {
this.version = checkNotNull(version);
this.transformResults = transformResults;
}
Expand All @@ -58,9 +57,8 @@ public SnapshotVersion getVersion() {
* The resulting fields returned from the backend after a mutation containing field transforms has
* been committed. Contains one Value for each FieldTransform that was in the mutation.
*
* <p>Will be null if the mutation did not contain any field transforms.
* <p>Returns an empty list if the mutation did not contain any field transforms.
*/
@Nullable
public List<Value> getTransformResults() {
return transformResults;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ public MaybeDocument applyToRemoteDocument(
}

List<Value> transformResults =
mutationResult.getTransformResults() != null
? serverTransformResults(maybeDoc, mutationResult.getTransformResults())
: new ArrayList<>();

serverTransformResults(maybeDoc, mutationResult.getTransformResults());
SnapshotVersion version = mutationResult.getVersion();
ObjectValue newData = patchDocument(maybeDoc, transformResults);
return new Document(getKey(), version, newData, Document.DocumentState.COMMITTED_MUTATIONS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ public MaybeDocument applyToRemoteDocument(

SnapshotVersion version = mutationResult.getVersion();

ObjectValue newData = value;
if (mutationResult.getTransformResults() != null) {
List<Value> transformResults =
serverTransformResults(maybeDoc, mutationResult.getTransformResults());
newData = transformObject(newData, transformResults);
}
List<Value> transformResults =
serverTransformResults(maybeDoc, mutationResult.getTransformResults());
ObjectValue newData = transformObject(value, transformResults);

return new Document(getKey(), version, newData, Document.DocumentState.COMMITTED_MUTATIONS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,10 @@ public MutationResult decodeMutationResult(
version = commitVersion;
}

List<Value> transformResults = null;
int transformResultsCount = proto.getTransformResultsCount();
if (transformResultsCount > 0) {
transformResults = new ArrayList<>(transformResultsCount);
for (int i = 0; i < transformResultsCount; i++) {
transformResults.add(proto.getTransformResults(i));
}
List<Value> transformResults = new ArrayList<>(transformResultsCount);
for (int i = 0; i < transformResultsCount; i++) {
transformResults.add(proto.getTransformResults(i));
}
return new MutationResult(version, transformResults);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private void acknowledgeMutation(long documentVersion, @Nullable Object transfor
version,
transformResult != null
? Collections.singletonList(TestUtil.wrap(transformResult))
: null);
: Collections.emptyList());

Choose a reason for hiding this comment

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

optional nit: you can just remove the Nullable annotation in the signature and change this method's overload to pass in an empty list instead of null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, this would have been nice but unfortunately wrap throws when it is passed an empty Object.

MutationBatchResult result =
MutationBatchResult.create(
batch, version, singletonList(mutationResult), WriteStream.EMPTY_STREAM_TOKEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ public void testTransitions() {
doc("collection/key", 7, map(), Document.DocumentState.COMMITTED_MUTATIONS);
UnknownDocument docV7Unknown = unknownDoc("collection/key", 7);

MutationResult mutationResult = new MutationResult(version(7), /*transformResults=*/ null);
MutationResult transformResult = new MutationResult(version(7), Collections.emptyList());
MutationResult mutationResult =
new MutationResult(version(7), /*transformResults=*/ Collections.emptyList());

assertVersionTransitions(set, docV3, mutationResult, docV7Committed);
assertVersionTransitions(set, deletedV3, mutationResult, docV7Committed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ private void doWriteAck(JSONObject writeAckSpec) throws Exception {
validateNextWriteSent(write.first);

MutationResult mutationResult =
new MutationResult(version(version), /*transformResults=*/ null);
new MutationResult(version(version), /*transformResults=*/ Collections.emptyList());
queue.runSync(() -> datastore.ackWrite(version(version), singletonList(mutationResult)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public static VerifyMutation verifyMutation(String path, int micros) {
}

public static MutationResult mutationResult(long version) {
return new MutationResult(version(version), null);
return new MutationResult(version(version), Collections.emptyList());
}

public static LocalViewChanges viewChanges(
Expand Down