Skip to content

feat: Instant support #6235

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions firebase-firestore/firebase-firestore.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ dependencies {
implementation libs.grpc.stub
implementation libs.kotlin.stdlib
implementation libs.kotlinx.coroutines.core
implementation libs.kotlinx.datetime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this dependency to "compileOnly" so we don't unnecessarily pull in a dependency on the kotlinx.datetime library if it's not used by the downstream dependent.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


compileOnly libs.autovalue.annotations
compileOnly libs.javax.annotation.jsr250
Expand Down
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 @@ -42,6 +44,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 @@ -177,6 +180,13 @@ private static <T> Object serialize(T o, ErrorPath path) {
|| o instanceof FieldValue
|| o instanceof VectorValue) {
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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& o instanceof kotlinx.datetime.Instant) {
kotlinx.datetime.Instant instant = (kotlinx.datetime.Instant) o;
return new Timestamp(instant.getEpochSeconds(), instant.getNanosecondsOfSecond());
} else if (o instanceof Uri || o instanceof URI || o instanceof URL) {
return o.toString();
} else {
Expand Down Expand Up @@ -237,6 +247,12 @@ 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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& kotlinx.datetime.Instant.class.isAssignableFrom(clazz)) {
return (T) new kotlinx.datetime.Instant(convertInstant(o, context));
} else if (Blob.class.isAssignableFrom(clazz)) {
return (T) convertBlob(o, context);
} else if (GeoPoint.class.isAssignableFrom(clazz)) {
Expand Down Expand Up @@ -512,6 +528,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 @@ -933,13 +963,16 @@ 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 || fieldType == kotlinx.datetime.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 @@ -954,13 +987,16 @@ 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 || returnType == kotlinx.datetime.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
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import static org.junit.Assert.fail;

import androidx.annotation.Nullable;
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.DocumentId;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.Exclude;
import com.google.firebase.firestore.PropertyName;
import com.google.firebase.firestore.TestUtil;
import com.google.firebase.firestore.ThrowOnExtraProperties;
import java.io.Serializable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -37,6 +39,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.junit.Test;
import org.robolectric.annotation.Config;
Expand Down Expand Up @@ -95,6 +98,44 @@ public boolean isValue() {
}
}

private static class TimeBean {
public Timestamp timestamp;
public Date date;
public Instant instant;
public kotlinx.datetime.Instant instantKt;

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
TimeBean timeBean = (TimeBean) o;
return Objects.equals(timestamp, timeBean.timestamp)
&& Objects.equals(date, timeBean.date)
&& Objects.equals(instant, timeBean.instant)
&& Objects.equals(instantKt, timeBean.instantKt);
}

@Override
public int hashCode() {
return Objects.hash(timestamp, date, instant, instantKt);
}

@Override
public String toString() {
return "TimeBean{"
+ "_date="
+ date
+ ", _timestamp="
+ timestamp
+ ", _instant="
+ instant
+ ", _instantKt="
+ instantKt
+ '}';
}
}

private static class ShortBean {
private short value;

Expand Down Expand Up @@ -1476,6 +1517,48 @@ public void serializeBooleanBean() {
assertJson("{'value': true}", serialize(bean));
}

@Test
public void serializeTimeBean() {
TimeBean bean = new TimeBean();
bean.instant = Instant.ofEpochSecond(1234, 5678);
bean.timestamp = new Timestamp(bean.instant);
bean.date = new Date(1234);
bean.instantKt = new kotlinx.datetime.Instant(bean.instant);
assertEquals(
Map.of(
"timestamp",
bean.timestamp,
"date",
bean.date,
"instant",
bean.timestamp,
"instantKt",
bean.timestamp),
serialize(bean));
}

@Test
public void deserializeTimeBean() {
TimeBean bean = new TimeBean();
bean.instant = Instant.ofEpochSecond(1234, 5678);
bean.timestamp = new Timestamp(bean.instant);
bean.date = new Date(1234);
bean.instantKt = new kotlinx.datetime.Instant(bean.instant);
assertEquals(
bean,
convertToCustomClass(
Map.of(
"timestamp",
bean.timestamp,
"date",
bean.date,
"instant",
bean.timestamp,
"instantKt",
bean.timestamp),
TimeBean.class));
}

@Test
public void serializeFloatBean() {
FloatBean bean = new FloatBean();
Expand Down