1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! The `#[serde(with = "firestore::serialize_as_null")]` attribute for writing an explicit
//! Firestore null instead of the default of omitting a `None` field.
pub const FIRESTORE_NULL_TYPE_TAG_TYPE: &str = "FirestoreNull";
/// Stores an `Option<T>` field as `T`'s ordinary Firestore value when present, or an explicit
/// Firestore null when `None`.
///
/// By default a `None` field is left out of the document entirely; use this attribute wherever a
/// document must instead carry a visible null, for example to clear a field a reader treats as
/// present-but-empty. Pair it with `#[serde(default)]` so a document that omits the field still
/// deserializes to `None`. [`serialize_as_null_timestamp`](crate::serialize_as_null_timestamp)
/// is the same idea specialised for `FirestoreInstant`.
///
/// ```rust
/// use firestore::firestore_document_from_serializable;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Profile {
/// #[serde(default)]
/// #[serde(with = "firestore::serialize_as_null")]
/// nickname: Option<String>,
/// }
///
/// let profile = Profile { nickname: None };
/// let document = firestore_document_from_serializable("profiles/p1", &profile).unwrap();
///
/// assert!(matches!(
/// document.fields["nickname"].value_type,
/// Some(gcloud_sdk::google::firestore::v1::value::ValueType::NullValue(_))
/// ));
/// ```