Search in sources :

Example 21 with Value

use of com.google.firestore.v1beta1.Value in project java-firestore by googleapis.

the class DocumentSnapshot method extractField.

/**
 * Returns the Value Proto at 'fieldPath'. Returns null if the field was not found.
 */
@Nullable
Value extractField(@Nonnull FieldPath fieldPath) {
    Value value = null;
    if (fields != null) {
        Iterator<String> components = fieldPath.getSegments().iterator();
        value = fields.get(components.next());
        while (value != null && components.hasNext()) {
            if (value.getValueTypeCase() != Value.ValueTypeCase.MAP_VALUE) {
                return null;
            }
            value = value.getMapValue().getFieldsOrDefault(components.next(), null);
        }
    }
    return value;
}
Also used : Value(com.google.firestore.v1.Value) Nullable(javax.annotation.Nullable)

Example 22 with Value

use of com.google.firestore.v1beta1.Value in project java-firestore by googleapis.

the class DocumentReferenceTest method updateNestedDocument.

@Test
public void updateNestedDocument() throws Exception {
    doReturn(SINGLE_WRITE_COMMIT_RESPONSE).when(firestoreMock).sendRequest(commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
    Map<String, Object> nestedObject = new HashMap<>();
    nestedObject.put("a", "b");
    nestedObject.put("c.d", "e");
    nestedObject.put("f.g.h", "i");
    Map<String, Value> expandedObject = new HashMap<>();
    expandedObject.put("a", string("b"));
    expandedObject.put("c", object("d", string("e")));
    expandedObject.put("f", object("g", object("h", string("i"))));
    documentReference.update(nestedObject).get();
    CommitRequest expectedCommit = commit(update(expandedObject, new ArrayList<>(nestedObject.keySet())));
    assertCommitEquals(expectedCommit, commitCapture.getValue());
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) HashMap(java.util.HashMap) Value(com.google.firestore.v1.Value) MapValue(com.google.firestore.v1.MapValue) ArrayValue(com.google.firestore.v1.ArrayValue) ArrayList(java.util.ArrayList) CommitResponse(com.google.firestore.v1.CommitResponse) Test(org.junit.Test)

Example 23 with Value

use of com.google.firestore.v1beta1.Value in project java-firestore by googleapis.

the class DocumentReferenceTest method updateNestedMap.

@Test
public void updateNestedMap() throws Exception {
    doReturn(SINGLE_WRITE_COMMIT_RESPONSE).when(firestoreMock).sendRequest(commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
    documentReference.update("a.b", "foo", "a.c", FieldValue.delete()).get();
    Map<String, Value> nestedUpdate = new HashMap<>();
    Value.Builder valueProto = Value.newBuilder();
    valueProto.getMapValueBuilder().putFields("b", Value.newBuilder().setStringValue("foo").build());
    nestedUpdate.put("a", valueProto.build());
    CommitRequest expectedCommit = commit(update(nestedUpdate, Arrays.asList("a.b", "a.c")));
    assertCommitEquals(expectedCommit, commitCapture.getValue());
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) HashMap(java.util.HashMap) Value(com.google.firestore.v1.Value) MapValue(com.google.firestore.v1.MapValue) ArrayValue(com.google.firestore.v1.ArrayValue) CommitResponse(com.google.firestore.v1.CommitResponse) Test(org.junit.Test)

Example 24 with Value

use of com.google.firestore.v1beta1.Value in project java-firestore by googleapis.

the class DocumentReferenceTest method deserializeCustomList.

@Test
public void deserializeCustomList() throws ExecutionException, InterruptedException {
    ImmutableMap CUSTOM_LIST_PROTO = ImmutableMap.<String, Value>builder().put("fooList", Value.newBuilder().setArrayValue(ArrayValue.newBuilder().addValues(Value.newBuilder().setMapValue(MapValue.newBuilder().putAllFields(SINGLE_FIELD_PROTO)).build())).build()).build();
    doAnswer(getAllResponse(CUSTOM_LIST_PROTO)).when(firestoreMock).streamRequest(getAllCapture.capture(), streamObserverCapture.capture(), Matchers.<ServerStreamingCallable>any());
    DocumentSnapshot snapshot = documentReference.get().get();
    LocalFirestoreHelper.CustomList customList = snapshot.toObject(LocalFirestoreHelper.CustomList.class);
    assertEquals(FOO_LIST, customList.fooList);
    assertEquals(SINGLE_FIELD_OBJECT, customList.fooList.get(0));
}
Also used : Value(com.google.firestore.v1.Value) MapValue(com.google.firestore.v1.MapValue) ArrayValue(com.google.firestore.v1.ArrayValue) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 25 with Value

use of com.google.firestore.v1beta1.Value in project java-firestore by googleapis.

the class DocumentReferenceTest method deleteField.

@Test
public void deleteField() throws Exception {
    doReturn(SINGLE_WRITE_COMMIT_RESPONSE).when(firestoreMock).sendRequest(commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
    documentReference.update("foo", "bar", "bar.foo", FieldValue.delete()).get();
    Value.Builder emptyMap = Value.newBuilder();
    emptyMap.getMapValueBuilder();
    Map<String, Value> fieldMap = new HashMap<>();
    fieldMap.put("foo", string("bar"));
    CommitRequest expectedCommit = commit(update(fieldMap, Arrays.asList("foo", "bar.foo")));
    assertCommitEquals(expectedCommit, commitCapture.getValue());
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) HashMap(java.util.HashMap) Value(com.google.firestore.v1.Value) MapValue(com.google.firestore.v1.MapValue) ArrayValue(com.google.firestore.v1.ArrayValue) CommitResponse(com.google.firestore.v1.CommitResponse) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)126 Value (com.google.firestore.v1.Value)108 ArrayValue (com.google.firestore.v1.ArrayValue)73 LinkedHashSet (java.util.LinkedHashSet)71 ObjectValue (com.google.firebase.firestore.model.ObjectValue)53 NullValue (com.google.protobuf.NullValue)50 MapValue (com.google.firestore.v1.MapValue)47 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)25 Value (com.google.datastore.v1.Value)20 Map (java.util.Map)20 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)17 List (java.util.List)17 Record (org.apache.avro.generic.GenericData.Record)16 SchemaAndRecord (org.apache.beam.sdk.io.gcp.bigquery.SchemaAndRecord)16 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)16 Set (java.util.Set)14 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)13 Nullable (androidx.annotation.Nullable)10 Value (com.google.privacy.dlp.v2.Value)9