Skip to content

Commit 4f537e7

Browse files
Add FieldValue helpers (#7642)
1 parent 5f6feba commit 4f537e7

File tree

11 files changed

+1054
-29
lines changed

11 files changed

+1054
-29
lines changed

FirebaseFirestore.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Google Cloud Firestore is a NoSQL document database built for automatic scaling,
8989
abseil_version = '0.20200225.0'
9090
s.dependency 'abseil/algorithm', abseil_version
9191
s.dependency 'abseil/base', abseil_version
92+
s.dependency 'abseil/container/flat_hash_map', abseil_version
9293
s.dependency 'abseil/memory', abseil_version
9394
s.dependency 'abseil/meta', abseil_version
9495
s.dependency 'abseil/strings/strings', abseil_version

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@
745745
91AEFFEE35FBE15FEC42A1F4 /* memory_local_store_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = F6CA0C5638AB6627CB5B4CF4 /* memory_local_store_test.cc */; };
746746
920B6ABF76FDB3547F1CCD84 /* firestore.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 544129D421C2DDC800EFB9CC /* firestore.pb.cc */; };
747747
925BE64990449E93242A00A2 /* memory_mutation_queue_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 74FBEFA4FE4B12C435011763 /* memory_mutation_queue_test.cc */; };
748+
92C3733EF8D9E4596B43010E /* value_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 050C1BC58C2A6C470C9E0F35 /* value_util_test.cc */; };
748749
92D7081085679497DC112EDB /* persistence_testing.cc in Sources */ = {isa = PBXBuildFile; fileRef = 9113B6F513D0473AEABBAF1F /* persistence_testing.cc */; };
749750
92EFF0CC2993B43CBC7A61FF /* grpc_streaming_reader_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B6D964922154AB8F00EB9CFB /* grpc_streaming_reader_test.cc */; };
750751
9382BE7190E7750EE7CCCE7C /* write_spec_test.json in Resources */ = {isa = PBXBuildFile; fileRef = 54DA12A51F315EE100DD57A1 /* write_spec_test.json */; };
@@ -956,6 +957,7 @@
956957
C21B3A1CCB3AD42E57EA14FC /* Pods_Firestore_Tests_macOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 759E964B6A03E6775C992710 /* Pods_Firestore_Tests_macOS.framework */; };
957958
C23552A6D9FB0557962870C2 /* local_store_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 307FF03D0297024D59348EBD /* local_store_test.cc */; };
958959
C25F321AC9BF8D1CFC8543AF /* reference_set_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 132E32997D781B896672D30A /* reference_set_test.cc */; };
960+
C343CA82954A852D899619B6 /* value_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 050C1BC58C2A6C470C9E0F35 /* value_util_test.cc */; };
959961
C393D6984614D8E4D8C336A2 /* mutation.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 618BBE8220B89AAC00B5BCE7 /* mutation.pb.cc */; };
960962
C39CBADA58F442C8D66C3DA2 /* FIRFieldPathTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E04C202154AA00B64F25 /* FIRFieldPathTests.mm */; };
961963
C3E4EE9615367213A71FEECF /* filesystem_testing.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA02DA2FCD0001CFC6EB08DA /* filesystem_testing.cc */; };
@@ -1064,6 +1066,7 @@
10641066
DE17D9D0C486E1817E9E11F9 /* status.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 618BBE9920B89AAC00B5BCE7 /* status.pb.cc */; };
10651067
DE435F33CE563E238868D318 /* query_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B9C261C26C5D311E1E3C0CB9 /* query_test.cc */; };
10661068
DE50F1D39D34F867BC750957 /* grpc_stream_tester.cc in Sources */ = {isa = PBXBuildFile; fileRef = 87553338E42B8ECA05BA987E /* grpc_stream_tester.cc */; };
1069+
DE7B9A2DE0427C4F4221B4EA /* value_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 050C1BC58C2A6C470C9E0F35 /* value_util_test.cc */; };
10671070
DE8C47B973526A20D88F785D /* token_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABC1D7DF2023A3EF00BA84F0 /* token_test.cc */; };
10681071
DEF4BF5FAA83C37100408F89 /* bundle_spec_test.json in Resources */ = {isa = PBXBuildFile; fileRef = 79EAA9F7B1B9592B5F053923 /* bundle_spec_test.json */; };
10691072
DF27137C8EA7D095D68851B4 /* field_filter_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = E8551D6C6FB0B1BACE9E5BAD /* field_filter_test.cc */; };
@@ -1121,6 +1124,7 @@
11211124
EC80A217F3D66EB0272B36B0 /* FSTLevelDBSpecTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E02C20213FFB00B64F25 /* FSTLevelDBSpecTests.mm */; };
11221125
ECC433628575AE994C621C54 /* create_noop_connectivity_monitor.cc in Sources */ = {isa = PBXBuildFile; fileRef = CF39535F2C41AB0006FA6C0E /* create_noop_connectivity_monitor.cc */; };
11231126
ECED3B60C5718B085AAB14FB /* to_string_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B696858D2214B53900271095 /* to_string_test.cc */; };
1127+
ED3DF8288703E89B701AB163 /* value_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 050C1BC58C2A6C470C9E0F35 /* value_util_test.cc */; };
11241128
ED420D8F49DA5C41EEF93913 /* FIRSnapshotMetadataTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E04D202154AA00B64F25 /* FIRSnapshotMetadataTests.mm */; };
11251129
ED4E2AC80CAF2A8FDDAC3DEE /* field_mask_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 549CCA5320A36E1F00BCEB75 /* field_mask_test.cc */; };
11261130
ED9DF1EB20025227B38736EC /* message_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = CE37875365497FFA8687B745 /* message_test.cc */; };
@@ -1139,6 +1143,7 @@
11391143
F3261CBFC169DB375A0D9492 /* FSTMockDatastore.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E02D20213FFC00B64F25 /* FSTMockDatastore.mm */; };
11401144
F386012CAB7F0C0A5564016A /* credentials_provider_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D9342023966E000A432D /* credentials_provider_test.cc */; };
11411145
F3F09BC931A717CEFF4E14B9 /* FIRFieldValueTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E04A202154AA00B64F25 /* FIRFieldValueTests.mm */; };
1146+
F3FC69D44288F28DA3A1B7A6 /* value_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 050C1BC58C2A6C470C9E0F35 /* value_util_test.cc */; };
11421147
F481368DB694B3B4D0C8E4A2 /* query_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B9C261C26C5D311E1E3C0CB9 /* query_test.cc */; };
11431148
F4F00BF4E87D7F0F0F8831DB /* FSTEventAccumulator.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E0392021401F00B64F25 /* FSTEventAccumulator.mm */; };
11441149
F4FAC5A7D40A0A9A3EA77998 /* FSTLevelDBSpecTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E02C20213FFB00B64F25 /* FSTLevelDBSpecTests.mm */; };
@@ -1148,6 +1153,7 @@
11481153
F6079BFC9460B190DA85C2E6 /* pretty_printing_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB323F9553050F4F6490F9FF /* pretty_printing_test.cc */; };
11491154
F609600E9A88A4D44FD1FCEB /* FSTSpecTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E03020213FFC00B64F25 /* FSTSpecTests.mm */; };
11501155
F660788F69B4336AC6CD2720 /* offline_spec_test.json in Resources */ = {isa = PBXBuildFile; fileRef = 54DA12A11F315EE100DD57A1 /* offline_spec_test.json */; };
1156+
F685114BCCE28435948110B8 /* value_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 050C1BC58C2A6C470C9E0F35 /* value_util_test.cc */; };
11511157
F696B7467E80E370FDB3EAA7 /* remote_document_cache_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7EB299CF85034F09CFD6F3FD /* remote_document_cache_test.cc */; };
11521158
F72DF72447EA7AB9D100816A /* FSTHelpers.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E03A2021401F00B64F25 /* FSTHelpers.mm */; };
11531159
F731A0CCD0220B370BC1BE8B /* BasicCompileTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE0761F61F2FE68D003233AF /* BasicCompileTests.swift */; };
@@ -1243,6 +1249,7 @@
12431249
014C60628830D95031574D15 /* random_access_queue_test.cc */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.cpp; path = random_access_queue_test.cc; sourceTree = "<group>"; };
12441250
045D39C4A7D52AF58264240F /* remote_document_cache_test.h */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.h; path = remote_document_cache_test.h; sourceTree = "<group>"; };
12451251
0473AFFF5567E667A125347B /* ordered_code_benchmark.cc */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.cpp; path = ordered_code_benchmark.cc; sourceTree = "<group>"; };
1252+
050C1BC58C2A6C470C9E0F35 /* value_util_test.cc */ = {isa = PBXFileReference; includeInIndex = 1; path = value_util_test.cc; sourceTree = "<group>"; };
12461253
0840319686A223CC4AD3FAB1 /* leveldb_remote_document_cache_test.cc */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.cpp; path = leveldb_remote_document_cache_test.cc; sourceTree = "<group>"; };
12471254
0EE5300F8233D14025EF0456 /* string_apple_test.mm */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.objcpp; path = string_apple_test.mm; sourceTree = "<group>"; };
12481255
11984BA0A99D7A7ABA5B0D90 /* Pods-Firestore_Example_iOS-Firestore_SwiftTests_iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Example_iOS-Firestore_SwiftTests_iOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Example_iOS-Firestore_SwiftTests_iOS/Pods-Firestore_Example_iOS-Firestore_SwiftTests_iOS.release.xcconfig"; sourceTree = "<group>"; };
@@ -2233,6 +2240,7 @@
22332240
B686F2B02024FFD70028D6BE /* resource_path_test.cc */,
22342241
ABA495B9202B7E79008A7851 /* snapshot_version_test.cc */,
22352242
33607A3AE91548BD219EC9C6 /* transform_operation_test.cc */,
2243+
050C1BC58C2A6C470C9E0F35 /* value_util_test.cc */,
22362244
);
22372245
path = model;
22382246
sourceTree = "<group>";
@@ -3451,6 +3459,7 @@
34513459
5D51D8B166D24EFEF73D85A2 /* transform_operation_test.cc in Sources */,
34523460
5F19F66D8B01BA2B97579017 /* tree_sorted_map_test.cc in Sources */,
34533461
16F52ECC6FA8A0587CD779EB /* user_test.cc in Sources */,
3462+
F3FC69D44288F28DA3A1B7A6 /* value_util_test.cc in Sources */,
34543463
A9A9994FB8042838671E8506 /* view_snapshot_test.cc in Sources */,
34553464
AD8F0393B276B2934D251AAC /* view_test.cc in Sources */,
34563465
2D65D31D71A75B046C47B0EB /* view_testing.cc in Sources */,
@@ -3629,6 +3638,7 @@
36293638
5EE21E86159A1911E9503BC1 /* transform_operation_test.cc in Sources */,
36303639
627253FDEC6BB5549FE77F4E /* tree_sorted_map_test.cc in Sources */,
36313640
596C782EFB68131380F8EEF8 /* user_test.cc in Sources */,
3641+
C343CA82954A852D899619B6 /* value_util_test.cc in Sources */,
36323642
1B4794A51F4266556CD0976B /* view_snapshot_test.cc in Sources */,
36333643
C1F196EC5A7C112D2F7C7724 /* view_test.cc in Sources */,
36343644
3451DC1712D7BF5D288339A2 /* view_testing.cc in Sources */,
@@ -3819,6 +3829,7 @@
38193829
15BF63DFF3A7E9A5376C4233 /* transform_operation_test.cc in Sources */,
38203830
54B91B921DA757C64CC67C90 /* tree_sorted_map_test.cc in Sources */,
38213831
8D5A9E6E43B6F47431841FE2 /* user_test.cc in Sources */,
3832+
ED3DF8288703E89B701AB163 /* value_util_test.cc in Sources */,
38223833
3A307F319553A977258BB3D6 /* view_snapshot_test.cc in Sources */,
38233834
89C71AEAA5316836BB1D5A01 /* view_test.cc in Sources */,
38243835
06BCEB9C65DFAA142F3D3F0B /* view_testing.cc in Sources */,
@@ -4009,6 +4020,7 @@
40094020
44EAF3E6EAC0CC4EB2147D16 /* transform_operation_test.cc in Sources */,
40104021
3D22F56C0DE7C7256C75DC06 /* tree_sorted_map_test.cc in Sources */,
40114022
918E3D35942CE493690C45CE /* user_test.cc in Sources */,
4023+
DE7B9A2DE0427C4F4221B4EA /* value_util_test.cc in Sources */,
40124024
81A6B241E63540900F205817 /* view_snapshot_test.cc in Sources */,
40134025
A5B8C273593D1BB6E8AE4CBA /* view_test.cc in Sources */,
40144026
7F771EB980D9CFAAB4764233 /* view_testing.cc in Sources */,
@@ -4197,6 +4209,7 @@
41974209
D3CB03747E34D7C0365638F1 /* transform_operation_test.cc in Sources */,
41984210
549CCA5120A36DBC00BCEB75 /* tree_sorted_map_test.cc in Sources */,
41994211
ABC1D7DE2023A05300BA84F0 /* user_test.cc in Sources */,
4212+
92C3733EF8D9E4596B43010E /* value_util_test.cc in Sources */,
42004213
340987A77D72C80A3E0FDADF /* view_snapshot_test.cc in Sources */,
42014214
17473086EBACB98CDC3CC65C /* view_test.cc in Sources */,
42024215
DDDE74C752E65DE7D39A7166 /* view_testing.cc in Sources */,
@@ -4406,6 +4419,7 @@
44064419
60186935E36CF79E48A0B293 /* transform_operation_test.cc in Sources */,
44074420
5DA343D28AE05B0B2FE9FFB3 /* tree_sorted_map_test.cc in Sources */,
44084421
D43F7601F3F3DE3125346D42 /* user_test.cc in Sources */,
4422+
F685114BCCE28435948110B8 /* value_util_test.cc in Sources */,
44094423
59E89A97A476790E89AFC7E7 /* view_snapshot_test.cc in Sources */,
44104424
B63D84B2980C7DEE7E6E4708 /* view_test.cc in Sources */,
44114425
48D1B38B93D34F1B82320577 /* view_testing.cc in Sources */,

Firestore/core/CMakeLists.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,12 @@ target_compile_definitions(
131131

132132
target_link_libraries(
133133
firestore_util PUBLIC
134-
absl_base
135-
absl_memory
136-
absl_meta
137-
absl_optional
138-
absl_strings
134+
absl::base
135+
absl::flat_hash_map
136+
absl::memory
137+
absl::meta
138+
absl::optional
139+
absl::strings
139140
)
140141

141142
if(HAVE_OPENSSL_RAND_H)
@@ -232,11 +233,12 @@ target_include_directories(
232233
target_link_libraries(
233234
firestore_core PUBLIC
234235
LevelDB::LevelDB
235-
absl_base
236-
absl_memory
237-
absl_meta
238-
absl_optional
239-
absl_strings
236+
absl::base
237+
absl::flat_hash_map
238+
absl::memory
239+
absl::meta
240+
absl::optional
241+
absl::strings
240242
firestore_nanopb
241243
firestore_protos_nanopb
242244
firestore_util
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/model/server_timestamp_util.h"
18+
#include "Firestore/core/src/nanopb/nanopb_util.h"
19+
#include "Firestore/core/src/util/hard_assert.h"
20+
#include "absl/strings/string_view.h"
21+
22+
namespace firebase {
23+
namespace firestore {
24+
namespace model {
25+
26+
const char kTypeKey[] = "__type__";
27+
const char kLocalWriteTimeKey[] = "__local_write_time__";
28+
const char kServerTimestampSentinel[] = "server_timestamp";
29+
30+
bool IsServerTimestamp(const google_firestore_v1_Value& value) {
31+
if (value.which_value_type != google_firestore_v1_Value_map_value_tag) {
32+
return false;
33+
}
34+
35+
if (value.map_value.fields_count > 3) {
36+
return false;
37+
}
38+
39+
for (size_t i = 0; i < value.map_value.fields_count; ++i) {
40+
const auto& field = value.map_value.fields[i];
41+
absl::string_view key = nanopb::MakeStringView(field.key);
42+
if (key == kTypeKey) {
43+
return field.value.which_value_type ==
44+
google_firestore_v1_Value_string_value_tag &&
45+
nanopb::MakeStringView(field.value.string_value) ==
46+
kServerTimestampSentinel;
47+
}
48+
}
49+
50+
return false;
51+
}
52+
53+
const google_firestore_v1_Value& GetLocalWriteTime(
54+
const firebase::firestore::google_firestore_v1_Value& value) {
55+
for (size_t i = 0; i < value.map_value.fields_count; ++i) {
56+
const auto& field = value.map_value.fields[i];
57+
absl::string_view key = nanopb::MakeStringView(field.key);
58+
if (key == kLocalWriteTimeKey) {
59+
return field.value;
60+
}
61+
}
62+
63+
HARD_FAIL("LocalWriteTime not found");
64+
}
65+
66+
} // namespace model
67+
} // namespace firestore
68+
} // namespace firebase
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_MODEL_SERVER_TIMESTAMP_UTIL_H_
18+
#define FIRESTORE_CORE_SRC_MODEL_SERVER_TIMESTAMP_UTIL_H_
19+
20+
#include "Firestore/Protos/nanopb/google/firestore/v1/document.nanopb.h"
21+
22+
namespace firebase {
23+
namespace firestore {
24+
namespace model {
25+
26+
// Utility methods to handle ServerTimestamps, which are stored using special
27+
// sentinel fields in MapValues.
28+
29+
/**
30+
* Returns whether the provided value is a field map that contains the
31+
* sentinel values of a ServerTimestamp.
32+
*/
33+
bool IsServerTimestamp(const google_firestore_v1_Value& value);
34+
35+
/**
36+
* Returns the local time at which the timestamp was written to the document.
37+
*/
38+
const google_firestore_v1_Value& GetLocalWriteTime(
39+
const google_firestore_v1_Value& value);
40+
41+
} // namespace model
42+
} // namespace firestore
43+
} // namespace firebase
44+
45+
#endif // FIRESTORE_CORE_SRC_MODEL_SERVER_TIMESTAMP_UTIL_H_

0 commit comments

Comments
 (0)