Skip to content

Commit 3a6018b

Browse files
committed
Firestore: update javadocs of Exclude, PropertyName, and ServerTimestamp to explicitly document using the @get and/or @set use-site targets on Kotlin properties.
1 parent 320a3ed commit 3a6018b

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/Exclude.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,31 @@
1919
import java.lang.annotation.RetentionPolicy;
2020
import java.lang.annotation.Target;
2121

22-
/** Marks a field as excluded from the database instance. */
22+
/**
23+
* Marks a field as excluded from the database instance.
24+
*
25+
* <h3>Kotlin Note</h3>
26+
* When applying this annotation to a property of a Kotlin class, the <code>@get</code> use-site
27+
* target should always be used. There is no need to use the <code>@set</code> use-site target as
28+
* this annotation is <em>only</em> considered when <em>writing</em> instances into Firestore, and
29+
* is ignored when <em>reading</em> instances from Firestore.
30+
* <p>
31+
* Here is an example of a class that can both be written into and read from Firestore whose
32+
* <code>bar</code> property will never be written into Firestore:
33+
* <pre>
34+
* data class Pojo(var foo: String? = null, @get:Exclude var bar: String? = null) {
35+
* constructor() : this(null, null) // Used by Firestore to create new instances
36+
* }
37+
* </pre>
38+
* <p>
39+
* If the class only needs to be <em>written</em> into Firestore (and not read from Firestore) then
40+
* the class can be simplified as follows:
41+
* <pre>
42+
* data class Pojo(val foo: String? = null, @get:Exclude val bar: String? = null)
43+
* </pre>
44+
* That is, <code>var</code> can be tightened to <code>val</code> and the secondary no-argument
45+
* constructor can be omitted.
46+
*/
2347
@Retention(RetentionPolicy.RUNTIME)
2448
@Target({ElementType.METHOD, ElementType.FIELD})
2549
public @interface Exclude {}

firebase-firestore/src/main/java/com/google/firebase/firestore/PropertyName.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,34 @@
1919
import java.lang.annotation.RetentionPolicy;
2020
import java.lang.annotation.Target;
2121

22-
/** Marks a field to be renamed when serialized. */
22+
/**
23+
* Marks a field to be renamed when serialized.
24+
*
25+
* <h3>Kotlin Note</h3>
26+
* When applying this annotation to a property of a Kotlin class, both the <code>@get</code> and
27+
* <code>@set</code> use-site targets should be used.
28+
* <p>
29+
* Here is an example of a class that can both be written into and read from Firestore whose
30+
* <code>foo</code> property will be stored into and read from a field named "my_foo" in the
31+
* Firestore document:
32+
* <pre>
33+
* data class Pojo(
34+
* @get:PropertyName("my_foo")
35+
* @set:PropertyName("my_foo")
36+
* var foo: String? = null
37+
* ) {
38+
* constructor() : this(null) // Used by Firestore to create new instances
39+
* }
40+
* </pre>
41+
* <p>
42+
* If the class only needs to be <em>written</em> into Firestore (and not read from Firestore) then
43+
* the class can be simplified as follows:
44+
* <pre>
45+
* data class Pojo(@get:PropertyName("my_foo") val foo: String? = null)
46+
* </pre>
47+
* That is, <code>var</code> can be tightened to <code>val</code>, the secondary no-argument
48+
* constructor can be omitted, and the <code>@set</code> use-site target can be omitted.
49+
*/
2350
@Retention(RetentionPolicy.RUNTIME)
2451
@Target({ElementType.METHOD, ElementType.FIELD})
2552
public @interface PropertyName {

firebase-firestore/src/main/java/com/google/firebase/firestore/ServerTimestamp.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@
2323
* Annotation used to mark a timestamp field to be populated with a server timestamp. If a POJO
2424
* being written contains {@code null} for a @ServerTimestamp-annotated field, it will be replaced
2525
* with a server-generated timestamp.
26+
*
27+
* <h3>Kotlin Note</h3>
28+
* When applying this annotation to a property of a Kotlin class, the <code>@get</code> use-site
29+
* target should always be used. There is no need to use the <code>@set</code> use-site target as
30+
* this annotation is <em>only</em> considered when <em>writing</em> instances into Firestore, and
31+
* is ignored when <em>reading</em> instances from Firestore.
32+
* <p>
33+
* Here is an example of a class that can both be written into and read from Firestore whose
34+
* <code>foo</code> property will be populated with the server timestamp in Firestore if its value
35+
* is null:
36+
* <pre>
37+
* data class Pojo(@get:ServerTimestamp var foo: Timestamp? = null) {
38+
* constructor() : this(null) // Used by Firestore to create new instances
39+
* }
40+
* </pre>
41+
* <p>
42+
* If the class only needs to be <em>written</em> into Firestore (and not read from Firestore) then
43+
* the class can be simplified as follows:
44+
* <pre>
45+
* data class Pojo(@get:ServerTimestamp val foo: Timestamp? = null)
46+
* </pre>
47+
* That is, <code>var</code> can be tightened to <code>val</code> and the secondary no-argument
48+
* constructor can be omitted.
2649
*/
2750
@Retention(RetentionPolicy.RUNTIME)
2851
@Target({ElementType.METHOD, ElementType.FIELD})

0 commit comments

Comments
 (0)