-
Notifications
You must be signed in to change notification settings - Fork 615
Adding test utilities to create Value types #1158
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
schmidt-sebastian
merged 10 commits into
mrschmidt/rewritefieldvalue
from
mrschmidt/testhelpers
Jan 24, 2020
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5d3f44d
Adding Proto-based equality and comparison
schmidt-sebastian 5d006f0
Adding test utilities to create Value types
schmidt-sebastian 8440ced
Merge branch 'mrschmidt/comparisons' into mrschmidt/testhelpers
schmidt-sebastian c315cdc
Address feeback
schmidt-sebastian a6f880a
Address feeback
schmidt-sebastian 529881b
Merge branch 'mrschmidt/comparisons' into mrschmidt/testhelpers
schmidt-sebastian c8d1ce8
Review feedback
schmidt-sebastian 02c0e78
Merge branch 'mrschmidt/rewritefieldvalue' into mrschmidt/testhelpers
schmidt-sebastian e8c299a
Fix merge
schmidt-sebastian 063a256
feedback
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,62 +22,71 @@ | |
import com.google.protobuf.NullValue; | ||
import com.google.type.LatLng; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** Test helper to create Firestore Value protos from Java types. */ | ||
public class ValueHelper { | ||
public class Values { | ||
|
||
// TODO(mrschmidt): Move into UserDataConverter | ||
public static Value valueOf(Object o) { | ||
if (o instanceof Value) { | ||
return (Value) o; | ||
} else if (o instanceof String) { | ||
return (Value.newBuilder().setStringValue((String) o).build()); | ||
} else if (o == null) { | ||
return Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(); | ||
} else if (o instanceof Boolean) { | ||
return Value.newBuilder().setBooleanValue((Boolean) o).build(); | ||
} else if (o instanceof Integer) { | ||
return (Value.newBuilder().setIntegerValue((long) (Integer) o).build()); | ||
return Value.newBuilder().setIntegerValue((Integer) o).build(); | ||
} else if (o instanceof Long) { | ||
return (Value.newBuilder().setIntegerValue((Long) o).build()); | ||
return Value.newBuilder().setIntegerValue((Long) o).build(); | ||
} else if (o instanceof Double) { | ||
return (Value.newBuilder().setDoubleValue((Double) o).build()); | ||
} else if (o instanceof Boolean) { | ||
return (Value.newBuilder().setBooleanValue((Boolean) o).build()); | ||
return Value.newBuilder().setDoubleValue((Double) o).build(); | ||
} else if (o instanceof Timestamp) { | ||
Timestamp timestamp = (Timestamp) o; | ||
return (Value.newBuilder() | ||
return Value.newBuilder() | ||
.setTimestampValue( | ||
com.google.protobuf.Timestamp.newBuilder() | ||
.setSeconds(timestamp.getSeconds()) | ||
.setNanos(timestamp.getNanoseconds()) | ||
.build()) | ||
.build()); | ||
.setNanos(timestamp.getNanoseconds())) | ||
.build(); | ||
} else if (o instanceof String) { | ||
return Value.newBuilder().setStringValue((String) o).build(); | ||
} else if (o instanceof Blob) { | ||
return Value.newBuilder().setBytesValue(((Blob) o).toByteString()).build(); | ||
|
||
} else if (o instanceof DocumentReference) { | ||
return Value.newBuilder() | ||
.setReferenceValue( | ||
"projects/project/databases/(default)/documents/" + ((DocumentReference) o).getPath()) | ||
.build(); | ||
} else if (o instanceof GeoPoint) { | ||
GeoPoint geoPoint = (GeoPoint) o; | ||
return (Value.newBuilder() | ||
return Value.newBuilder() | ||
.setGeoPointValue( | ||
LatLng.newBuilder() | ||
.setLatitude(geoPoint.getLatitude()) | ||
.setLongitude(geoPoint.getLongitude()) | ||
.build()) | ||
.build()); | ||
} else if (o instanceof Blob) { | ||
return (Value.newBuilder().setBytesValue(((Blob) o).toByteString()).build()); | ||
} else if (o instanceof DocumentReference) { | ||
return (Value.newBuilder() | ||
.setReferenceValue( | ||
"projects/projectId/databases/(default)/documents/" | ||
+ ((DocumentReference) o).getPath()) | ||
.build()); | ||
.setLongitude(geoPoint.getLongitude())) | ||
.build(); | ||
|
||
} else if (o instanceof List) { | ||
ArrayValue.Builder list = ArrayValue.newBuilder(); | ||
for (Object element : (List) o) { | ||
list.addValues(valueOf(element)); | ||
} | ||
return (Value.newBuilder().setArrayValue(list).build()); | ||
} else if (o == null) { | ||
return (Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build()); | ||
return Value.newBuilder().setArrayValue(list).build(); | ||
} else if (o instanceof Map) { | ||
com.google.firestore.v1.MapValue.Builder builder = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: import MapValue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
com.google.firestore.v1.MapValue.newBuilder(); | ||
for (Map.Entry<String, Object> entry : ((Map<String, Object>) o).entrySet()) { | ||
builder.putFields(entry.getKey(), valueOf(entry.getValue())); | ||
} | ||
return Value.newBuilder().setMapValue(builder).build(); | ||
} | ||
|
||
throw new UnsupportedOperationException(); | ||
throw new UnsupportedOperationException("Failed to serialize object: " + o); | ||
} | ||
|
||
/** Creates a MapValue from a list of key/value arguments. */ | ||
public static Value map(Object... entries) { | ||
com.google.firestore.v1.MapValue.Builder builder = | ||
com.google.firestore.v1.MapValue.newBuilder(); | ||
|
@@ -87,7 +96,7 @@ public static Value map(Object... entries) { | |
return Value.newBuilder().setMapValue(builder).build(); | ||
} | ||
|
||
public static Value wrapRef(DatabaseId dbId, DocumentKey key) { | ||
public static Value refValue(DatabaseId dbId, DocumentKey key) { | ||
return Value.newBuilder() | ||
.setReferenceValue( | ||
String.format( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: excess newline.
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.
Sorry - I forgot to address this before merging. I fixed it in the follow up PR.