Search in sources :

Example 1 with FieldValue

use of com.google.firebase.firestore.FieldValue in project firebase-android-sdk by firebase.

the class MutationTest method testAppliesLocalServerTimestampTransformsToDocuments.

@Test
public void testAppliesLocalServerTimestampTransformsToDocuments() {
    Map<String, Object> data = map("foo", map("bar", "bar-value"), "baz", "baz-value");
    MutableDocument transformedDoc = doc("collection/key", 1, data);
    Timestamp timestamp = Timestamp.now();
    Mutation transform = patchMutation("collection/key", map("foo.bar", FieldValue.serverTimestamp()));
    transform.applyToLocalView(transformedDoc, /* previousMask= */
    null, timestamp);
    // Server timestamps aren't parsed, so we manually insert it.
    ObjectValue expectedData = wrapObject(map("foo", map("bar", "<server-timestamp>"), "baz", "baz-value"));
    Value fieldValue = ServerTimestamps.valueOf(timestamp, wrap("bar-value"));
    expectedData.set(field("foo.bar"), fieldValue);
    MutableDocument expectedDoc = doc("collection/key", 1, expectedData).setHasLocalMutations();
    assertEquals(expectedDoc, transformedDoc);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldValue(com.google.firebase.firestore.FieldValue) Value(com.google.firestore.v1.Value) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 2 with FieldValue

use of com.google.firebase.firestore.FieldValue in project firebase-android-sdk by firebase.

the class CustomClassMapper method serialize.

@SuppressWarnings("unchecked")
private static <T> Object serialize(T o, ErrorPath path) {
    if (path.getLength() > MAX_DEPTH) {
        throw serializeError(path, "Exceeded maximum depth of " + MAX_DEPTH + ", which likely indicates there's an object cycle");
    }
    if (o == null) {
        return null;
    } else if (o instanceof Number) {
        if (o instanceof Long || o instanceof Integer || o instanceof Double || o instanceof Float) {
            return o;
        } else {
            throw serializeError(path, String.format("Numbers of type %s are not supported, please use an int, long, float or double", o.getClass().getSimpleName()));
        }
    } else if (o instanceof String) {
        return o;
    } else if (o instanceof Boolean) {
        return o;
    } else if (o instanceof Character) {
        throw serializeError(path, "Characters are not supported, please use Strings");
    } else if (o instanceof Map) {
        Map<String, Object> result = new HashMap<>();
        for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) o).entrySet()) {
            Object key = entry.getKey();
            if (key instanceof String) {
                String keyString = (String) key;
                result.put(keyString, serialize(entry.getValue(), path.child(keyString)));
            } else {
                throw serializeError(path, "Maps with non-string keys are not supported");
            }
        }
        return result;
    } else if (o instanceof Collection) {
        if (o instanceof List) {
            List<Object> list = (List<Object>) o;
            List<Object> result = new ArrayList<>(list.size());
            for (int i = 0; i < list.size(); i++) {
                result.add(serialize(list.get(i), path.child("[" + i + "]")));
            }
            return result;
        } else {
            throw serializeError(path, "Serializing Collections is not supported, please use Lists instead");
        }
    } else if (o.getClass().isArray()) {
        throw serializeError(path, "Serializing Arrays is not supported, please use Lists instead");
    } else if (o instanceof Enum) {
        String enumName = ((Enum<?>) o).name();
        try {
            Field enumField = o.getClass().getField(enumName);
            return BeanMapper.propertyName(enumField);
        } catch (NoSuchFieldException ex) {
            return enumName;
        }
    } else if (o instanceof Date || o instanceof Timestamp || o instanceof GeoPoint || o instanceof Blob || o instanceof DocumentReference || o instanceof FieldValue) {
        return o;
    } else {
        Class<T> clazz = (Class<T>) o.getClass();
        BeanMapper<T> mapper = loadOrCreateBeanMapperForClass(clazz);
        return mapper.serialize(o, path);
    }
}
Also used : ArrayList(java.util.ArrayList) ServerTimestamp(com.google.firebase.firestore.ServerTimestamp) Timestamp(com.google.firebase.Timestamp) Field(java.lang.reflect.Field) GeoPoint(com.google.firebase.firestore.GeoPoint) ArrayList(java.util.ArrayList) List(java.util.List) FieldValue(com.google.firebase.firestore.FieldValue) DocumentReference(com.google.firebase.firestore.DocumentReference) Blob(com.google.firebase.firestore.Blob) GeoPoint(com.google.firebase.firestore.GeoPoint) Date(java.util.Date) Collection(java.util.Collection) AccessibleObject(java.lang.reflect.AccessibleObject) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Timestamp (com.google.firebase.Timestamp)2 FieldValue (com.google.firebase.firestore.FieldValue)2 Blob (com.google.firebase.firestore.Blob)1 DocumentReference (com.google.firebase.firestore.DocumentReference)1 GeoPoint (com.google.firebase.firestore.GeoPoint)1 ServerTimestamp (com.google.firebase.firestore.ServerTimestamp)1 MutableDocument (com.google.firebase.firestore.model.MutableDocument)1 ObjectValue (com.google.firebase.firestore.model.ObjectValue)1 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)1 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)1 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)1 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)1 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)1 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)1 Value (com.google.firestore.v1.Value)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1