Skip to content
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

feat: Instant support #6235

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static com.google.firebase.firestore.util.ApiUtil.newInstance;

import android.net.Uri;
import android.os.Build;
import androidx.annotation.RequiresApi;
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.Blob;
import com.google.firebase.firestore.DocumentId;
Expand All @@ -41,6 +43,7 @@
import java.lang.reflect.WildcardType;
import java.net.URI;
import java.net.URL;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -175,6 +178,9 @@ private static <T> Object serialize(T o, ErrorPath path) {
|| o instanceof DocumentReference
|| o instanceof FieldValue) {
return o;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && o instanceof Instant) {
Instant instant = (Instant) o;
return new Timestamp(instant.getEpochSecond(), instant.getNano());
} else if (o instanceof Uri || o instanceof URI || o instanceof URL) {
return o.toString();
} else {
Expand Down Expand Up @@ -235,6 +241,8 @@ private static <T> T deserializeToClass(Object o, Class<T> clazz, DeserializeCon
return (T) convertDate(o, context);
} else if (Timestamp.class.isAssignableFrom(clazz)) {
return (T) convertTimestamp(o, context);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && Instant.class.isAssignableFrom(clazz)) {
return (T) convertInstant(o, context);
} else if (Blob.class.isAssignableFrom(clazz)) {
return (T) convertBlob(o, context);
} else if (GeoPoint.class.isAssignableFrom(clazz)) {
Expand Down Expand Up @@ -508,6 +516,20 @@ private static Timestamp convertTimestamp(Object o, DeserializeContext context)
}
}

@RequiresApi(api = Build.VERSION_CODES.O)
private static Instant convertInstant(Object o, DeserializeContext context) {
if (o instanceof Timestamp) {
Timestamp timestamp = (Timestamp) o;
return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanoseconds());
} else if (o instanceof Date) {
return Instant.ofEpochMilli(((Date) o).getTime());
} else {
throw deserializeError(
context.errorPath,
"Failed to convert value of type " + o.getClass().getName() + " to Instant");
}
}

private static Blob convertBlob(Object o, DeserializeContext context) {
if (o instanceof Blob) {
return (Blob) o;
Expand Down Expand Up @@ -919,13 +941,14 @@ Map<String, Object> serialize(T object, ErrorPath path) {
private void applyFieldAnnotations(Field field) {
if (field.isAnnotationPresent(ServerTimestamp.class)) {
Class<?> fieldType = field.getType();
if (fieldType != Date.class && fieldType != Timestamp.class) {
if (fieldType != Date.class && fieldType != Timestamp.class
&& ! (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && fieldType == Instant.class)) {
throw new IllegalArgumentException(
"Field "
+ field.getName()
+ " is annotated with @ServerTimestamp but is "
+ fieldType
+ " instead of Date or Timestamp.");
+ " instead of Date, Timestamp, or Instant.");
}
serverTimestamps.add(propertyName(field));
}
Expand All @@ -940,13 +963,14 @@ private void applyFieldAnnotations(Field field) {
private void applyGetterAnnotations(Method method) {
if (method.isAnnotationPresent(ServerTimestamp.class)) {
Class<?> returnType = method.getReturnType();
if (returnType != Date.class && returnType != Timestamp.class) {
if (returnType != Date.class && returnType != Timestamp.class
&& ! (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && returnType == Instant.class)) {
throw new IllegalArgumentException(
"Method "
+ method.getName()
+ " is annotated with @ServerTimestamp but returns "
+ returnType
+ " instead of Date or Timestamp.");
+ " instead of Date, Timestamp, or Instant.");
}
serverTimestamps.add(propertyName(method));
}
Expand Down