-
Notifications
You must be signed in to change notification settings - Fork 693
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2466 +/- ##
============================================
- Coverage 81.29% 74.09% -7.21%
+ Complexity 2343 2125 -218
============================================
Files 264 265 +1
Lines 7663 7678 +15
Branches 790 794 +4
============================================
- Hits 6230 5689 -541
- Misses 1097 1620 +523
- Partials 336 369 +33
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report at Codecov.
|
@@ -23,6 +23,7 @@ | |||
<dependency> | |||
<groupId>com.google.cloud</groupId> | |||
<artifactId>google-cloud-firestore</artifactId> | |||
<version>1.35.2-SNAPSHOT</version> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix indentation here; tab/space mismatch.
return Flux.from(entities).flatMap(this::create); | ||
} | ||
|
||
private <T> Mono<T> create(T entity) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to add a javadoc for this one describing purpose of method something like "Creates a single (non-batched) document in Firestore; ID generation is only available for non-batched created documents."
return ObservableReactiveUtil.streamingBidirectionalCall( | ||
this::openWriteStream, inputs, this::buildWriteRequest); | ||
return Flux.from(instances) | ||
.groupBy(t -> getIdValue(t) == null).flatMap(groupedFlux -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit, would prefer if you can format this section so each operator gets its own line like this:
return Flux.from(instances)
.groupBy(t -> getIdValue(t) == null)
.flatMap(groupedFlux -> groupedFlux.key() ? create(groupedFlux) : upsert(groupedFlux));
Clever usage of Group By!
} | ||
|
||
public FirestoreClassMapper getClassMapper() { | ||
return this.classMapper; | ||
} | ||
|
||
private static class ResourceName { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see; I would avoid introducing a new class here if possible -
I think if you include it, you will have to write a lot more docs to describe what it means for future readers of this code - i.e. it would at least require a class-level javadoc, and also - generated
is too general (i.e. generated what?); you may have to rename to needsGeneratedId
or add comment; similarly name
is too general as well - i.e. (name of what?)
It's not worth the burden of adding this class in my opinion if it is avoidable.
return Write.newBuilder() | ||
.setUpdate(document) | ||
.build(); | ||
ResourceName resourceName = buildResourceName(entity); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would avoid introducing the ResourceName
class to avoid coupling the resource name with whether the id
was generated or not. Also mentioned below; I think it's not worth the documentation burden.
Instead, maybe refactor so it's like:
Object idVal = getOrGenerateEntityId(entity);
String resourceName = buildResourceName(Class<?> entityType, Object idVal);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Just some minor polish suggestions.
String resourceName = buildResourceName(entity); | ||
Document document = getClassMapper().entityToDocument(entity, resourceName); | ||
Builder builder = Write.newBuilder().setUpdate(document); | ||
if (needsAutoId) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really see the point of having this needsAutoId
local variable, since it's only used once.
I would just put getIdValue(entity) == null
directly in the if
.
|
||
//TODO: replace with Internal.autoId() when it is available |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indicate the package of Internal
? Otherwise, it's ambiguous.
} | ||
|
||
private <T> String buildResourceName(T entity) { | ||
FirestorePersistentEntity<?> persistentEntity = | ||
this.mappingContext.getPersistentEntity(entity.getClass()); | ||
FirestorePersistentProperty idProperty = persistentEntity.getIdPropertyOrFail(); | ||
Object idVal = persistentEntity.getPropertyAccessor(entity).getProperty(idProperty); | ||
if (idVal == null) { | ||
if (idProperty.getType() != String.class) { | ||
throw new FirestoreDataException("Automatic ID generation only supported for String type"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also say that id was null
and what its type was to make the exception more clear.
} | ||
|
||
public FirestoreClassMapper getClassMapper() { | ||
return this.classMapper; | ||
} | ||
|
||
/** Creates a pseudo-random 20-character ID that can be used for Firestore documents. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you indicate where it was copied from?
.putAllFields(valuesMap) | ||
.setName(documentResourceName).build(); | ||
Builder builder = Document.newBuilder().putAllFields(valuesMap); | ||
if (documentResourceName != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it now possible for documentResourceName
to be null?
|
||
//Creates a pseudo-random 20-character ID that can be used for Firestore documents. | ||
//Copied from com.google.cloud.firestore.FirestoreImpl | ||
private static String autoId() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move the Id generating helper method autoId()
to a package-private class? maybe something like FirestoreIdGenerator
.
This way you can move all the code and fields in there related to ID generation; which allows you to remember what to delete once com.google.cloud.firestore.Internal.autoId()
becomes available. Otherwise you will have to comb through the code a little bit more in the future to do this removal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good; just some minor nits.
import java.security.SecureRandom; | ||
import java.util.Random; | ||
|
||
//Copied from com.google.cloud.firestore.FirestoreImpl |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For new classes, add a top level Javadoc :) i.e. with the author tag and brief description etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's why I didn't want to introduce a new class :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol sorry for the inconvenience. At least it will be deleted shortly though! (Once they expose auto id).
return Document.newBuilder() | ||
.putAllFields(valuesMap) | ||
.setName(documentResourceName).build(); | ||
Builder builder = Document.newBuilder().putAllFields(valuesMap); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might as well just revert this file then if there are no changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good from my side.
Kudos, SonarCloud Quality Gate passed! 0 Bugs 92.9% Coverage The version of Java (1.8.0_151) you have used to run this analysis is deprecated and we will stop accepting it from October 2020. Please update to at least Java 11. |
fixes #2428