use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class TestUtil method querySnapshot.
/**
* A convenience method for creating a particular query snapshot for tests.
*
* @param path To be used in constructing the query.
* @param oldDocs Provides the prior set of documents in the QuerySnapshot. Each entry maps to a
* document, with the key being the document id, and the value being the document contents.
* @param docsToAdd Specifies data to be added into the query snapshot as of now. Each entry maps
* to a document, with the key being the document id, and the value being the document
* contents.
* @param isFromCache Whether the query snapshot is cache result.
* @return A query snapshot that consists of both sets of documents.
*/
public static QuerySnapshot querySnapshot(String path, Map<String, ObjectValue> oldDocs, Map<String, ObjectValue> docsToAdd, boolean hasPendingWrites, boolean isFromCache) {
DocumentSet oldDocuments = docSet(Document.KEY_COMPARATOR);
ImmutableSortedSet<DocumentKey> mutatedKeys = DocumentKey.emptyKeySet();
for (Map.Entry<String, ObjectValue> pair : oldDocs.entrySet()) {
String docKey = path + "/" + pair.getKey();
MutableDocument doc = doc(docKey, 1L, pair.getValue());
if (hasPendingWrites) {
doc.setHasCommittedMutations();
mutatedKeys = mutatedKeys.insert(key(docKey));
}
oldDocuments = oldDocuments.add(doc);
}
DocumentSet newDocuments = docSet(Document.KEY_COMPARATOR);
List<DocumentViewChange> documentChanges = new ArrayList<>();
for (Map.Entry<String, ObjectValue> pair : docsToAdd.entrySet()) {
String docKey = path + "/" + pair.getKey();
MutableDocument docToAdd = doc(docKey, 1L, pair.getValue());
if (hasPendingWrites) {
docToAdd.setHasCommittedMutations();
mutatedKeys = mutatedKeys.insert(key(docKey));
}
newDocuments = newDocuments.add(docToAdd);
documentChanges.add(DocumentViewChange.create(Type.ADDED, docToAdd));
}
ViewSnapshot viewSnapshot = new ViewSnapshot(com.google.firebase.firestore.testutil.TestUtil.query(path), newDocuments, oldDocuments, documentChanges, isFromCache, mutatedKeys, /* didSyncStateChange= */
true, /* excludesMetadataChanges= */
false);
return new QuerySnapshot(query(path), viewSnapshot, FIRESTORE);
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class UserDataReader method parseSetData.
/**
* Parse document data from a non-merge {@code set()} call.
*
* @param input A map or POJO object representing document data.
*/
public ParsedSetData parseSetData(Object input) {
ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Set);
ObjectValue updateData = convertAndParseDocumentData(input, accumulator.rootContext());
return accumulator.toSetData(updateData);
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class UserDataReader method parseUpdateData.
/**
* Parse update data from an {@code update()} call.
*/
public ParsedUpdateData parseUpdateData(Map<String, Object> data) {
checkNotNull(data, "Provided update data must not be null.");
ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Update);
ParseContext context = accumulator.rootContext();
ObjectValue updateData = new ObjectValue();
for (Entry<String, Object> entry : data.entrySet()) {
FieldPath fieldPath = com.google.firebase.firestore.FieldPath.fromDotSeparatedPath(entry.getKey()).getInternalPath();
Object fieldValue = entry.getValue();
if (fieldValue instanceof DeleteFieldValue) {
// Add it to the field mask, but don't add anything to updateData.
context.addToFieldMask(fieldPath);
} else {
@Nullable Value parsedValue = convertAndParseFieldData(fieldValue, context.childContext(fieldPath));
if (parsedValue != null) {
context.addToFieldMask(fieldPath);
updateData.set(fieldPath, parsedValue);
}
}
}
return accumulator.toUpdateData(updateData);
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class UserDataReader method parseMergeData.
/**
* Parse document data from a {@code set()} call with {@link SetOptions#merge()} set.
*
* @param input A map or POJO object representing document data.
* @param fieldMask A {@code FieldMask} object representing the fields to be merged.
*/
public ParsedSetData parseMergeData(Object input, @Nullable FieldMask fieldMask) {
ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.MergeSet);
ObjectValue updateData = convertAndParseDocumentData(input, accumulator.rootContext());
if (fieldMask != null) {
// Verify that all elements specified in the field mask are part of the parsed context.
for (FieldPath field : fieldMask.getMask()) {
if (!accumulator.contains(field)) {
throw new IllegalArgumentException("Field '" + field.toString() + "' is specified in your field mask but not in your input data.");
}
}
return accumulator.toMergeData(updateData, fieldMask);
} else {
return accumulator.toMergeData(updateData);
}
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class UserDataReader method convertAndParseDocumentData.
/**
* Converts a POJO to native types and then parses it into model types. It expects the input to
* conform to document data (i.e. it must parse into an ObjectValue model type) and will throw an
* appropriate error otherwise.
*/
private ObjectValue convertAndParseDocumentData(Object input, ParseContext context) {
String badDocReason = "Invalid data. Data must be a Map<String, Object> or a suitable POJO object, but it was ";
// to use List instead, which also won't work in a set().
if (input.getClass().isArray()) {
throw new IllegalArgumentException(badDocReason + "an array");
}
Object converted = CustomClassMapper.convertToPlainJavaTypes(input);
Value parsedValue = parseData(converted, context);
if (parsedValue.getValueTypeCase() != Value.ValueTypeCase.MAP_VALUE) {
throw new IllegalArgumentException(badDocReason + "of type: " + Util.typeName(input));
}
return new ObjectValue(parsedValue);
}
Aggregations