-
Notifications
You must be signed in to change notification settings - Fork 621
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 all 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
106 changes: 106 additions & 0 deletions
106
firebase-firestore/src/roboUtil/java/com/google/firebase/firestore/Values.java
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// 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.firebase.firestore; | ||
|
||
import com.google.firebase.Timestamp; | ||
import com.google.firebase.firestore.model.DatabaseId; | ||
import com.google.firebase.firestore.model.DocumentKey; | ||
import com.google.firestore.v1.ArrayValue; | ||
import com.google.firestore.v1.MapValue; | ||
import com.google.firestore.v1.Value; | ||
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 Values { | ||
|
||
// TODO(mrschmidt): Move into UserDataConverter | ||
public static Value valueOf(Object o) { | ||
if (o instanceof Value) { | ||
return (Value) o; | ||
} 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((Integer) o).build(); | ||
} else if (o instanceof Long) { | ||
return Value.newBuilder().setIntegerValue((Long) o).build(); | ||
} else if (o instanceof Double) { | ||
return Value.newBuilder().setDoubleValue((Double) o).build(); | ||
} else if (o instanceof Timestamp) { | ||
Timestamp timestamp = (Timestamp) o; | ||
return Value.newBuilder() | ||
.setTimestampValue( | ||
com.google.protobuf.Timestamp.newBuilder() | ||
.setSeconds(timestamp.getSeconds()) | ||
.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() | ||
.setGeoPointValue( | ||
LatLng.newBuilder() | ||
.setLatitude(geoPoint.getLatitude()) | ||
.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 instanceof Map) { | ||
MapValue.Builder builder = 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("Failed to serialize object: " + o); | ||
} | ||
|
||
/** Creates a MapValue from a list of key/value arguments. */ | ||
public static Value map(Object... entries) { | ||
MapValue.Builder builder = MapValue.newBuilder(); | ||
for (int i = 0; i < entries.length; i += 2) { | ||
builder.putFields((String) entries[i], valueOf(entries[i + 1])); | ||
} | ||
return Value.newBuilder().setMapValue(builder).build(); | ||
} | ||
|
||
public static Value refValue(DatabaseId dbId, DocumentKey key) { | ||
return Value.newBuilder() | ||
.setReferenceValue( | ||
String.format( | ||
"projects/%s/databases/%s/documents/%s", | ||
dbId.getProjectId(), dbId.getDatabaseId(), key.toString())) | ||
.build(); | ||
} | ||
} |
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.