hidane_core/normalize.rs
1//! What Firestore does to values when it stores them.
2
3use hidane_proto::google::firestore::v1::{Value, value::ValueType};
4
5/// Applies Firestore's write-time normalization in place.
6///
7/// Timestamps keep microsecond precision: the official emulator stores
8/// `1970-01-01T00:00:00.000000999Z` as the epoch and `…00.000001500Z` as one microsecond, so
9/// sub-microsecond digits are truncated, not rounded (`tests/fixtures/value_order.json`).
10pub fn normalize_value(value: &mut Value) {
11 match &mut value.value_type {
12 Some(ValueType::TimestampValue(ts)) => ts.nanos -= ts.nanos % 1_000,
13 Some(ValueType::ArrayValue(array)) => array.values.iter_mut().for_each(normalize_value),
14 Some(ValueType::MapValue(map)) => map.fields.values_mut().for_each(normalize_value),
15 _ => {}
16 }
17}