Skip to content

Commit 2e8b918

Browse files
authored
Port FSTUsageValidation to C++ (#2670)
* Port FSTUsageValidation to C++ * Add an error log level
1 parent 0729c9d commit 2e8b918

27 files changed

+411
-279
lines changed

FirebaseFirestore.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Google Cloud Firestore is a NoSQL document database built for automatic scaling,
4141
'Firestore/third_party/Immutable/Tests/**',
4242

4343
# Exclude alternate implementations for other platforms
44+
'Firestore/core/src/firebase/firestore/api/input_validation_std.cc',
4445
'Firestore/core/src/firebase/firestore/remote/connectivity_monitor_noop.cc',
4546
'Firestore/core/src/firebase/firestore/remote/grpc_root_certificate_finder_generated.cc',
4647
'Firestore/core/src/firebase/firestore/util/filesystem_win.cc',

Firestore/Source/API/FIRCollectionReference.mm

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
#import "Firestore/Source/API/FIRQuery+Internal.h"
2626
#import "Firestore/Source/API/FIRQuery_Init.h"
2727
#import "Firestore/Source/Core/FSTQuery.h"
28-
#import "Firestore/Source/Util/FSTUsageValidation.h"
2928

29+
#include "Firestore/core/src/firebase/firestore/api/input_validation.h"
3030
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
3131
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
3232
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
3333

3434
namespace util = firebase::firestore::util;
35+
using firebase::firestore::api::ThrowInvalidArgument;
3536
using firebase::firestore::model::DocumentKey;
3637
using firebase::firestore::model::ResourcePath;
3738
using firebase::firestore::util::CreateAutoId;
@@ -58,9 +59,9 @@ @implementation FIRCollectionReference
5859

5960
- (instancetype)initWithPath:(const ResourcePath &)path firestore:(FIRFirestore *)firestore {
6061
if (path.size() % 2 != 1) {
61-
FSTThrowInvalidArgument(@"Invalid collection reference. Collection references must have an odd "
62-
"number of segments, but %s has %zu",
63-
path.CanonicalString().c_str(), path.size());
62+
ThrowInvalidArgument("Invalid collection reference. Collection references must have an odd "
63+
"number of segments, but %s has %s",
64+
path.CanonicalString(), path.size());
6465
}
6566
self = [super initWithQuery:[FSTQuery queryWithPath:path] firestore:firestore];
6667
return self;
@@ -112,7 +113,7 @@ - (NSString *)path {
112113

113114
- (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
114115
if (!documentPath) {
115-
FSTThrowInvalidArgument(@"Document path cannot be nil.");
116+
ThrowInvalidArgument("Document path cannot be nil.");
116117
}
117118
const ResourcePath subPath = ResourcePath::FromString(util::MakeString(documentPath));
118119
ResourcePath path = self.query.path.Append(subPath);

Firestore/Source/API/FIRDocumentReference.mm

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
#import "Firestore/Source/Core/FSTEventManager.h"
3131
#import "Firestore/Source/Core/FSTQuery.h"
3232
#import "Firestore/Source/Model/FSTFieldValue.h"
33-
#import "Firestore/Source/Util/FSTUsageValidation.h"
3433

3534
#include "Firestore/core/src/firebase/firestore/api/document_reference.h"
3635
#include "Firestore/core/src/firebase/firestore/api/document_snapshot.h"
36+
#include "Firestore/core/src/firebase/firestore/api/input_validation.h"
3737
#include "Firestore/core/src/firebase/firestore/core/event_listener.h"
3838
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
3939
#include "Firestore/core/src/firebase/firestore/model/document_set.h"
@@ -49,6 +49,7 @@
4949
using firebase::firestore::api::DocumentReference;
5050
using firebase::firestore::api::DocumentSnapshot;
5151
using firebase::firestore::api::Firestore;
52+
using firebase::firestore::api::ThrowInvalidArgument;
5253
using firebase::firestore::core::EventListener;
5354
using firebase::firestore::core::ListenOptions;
5455
using firebase::firestore::core::ParsedSetData;
@@ -77,9 +78,9 @@ - (instancetype)initWithReference:(DocumentReference &&)reference {
7778

7879
- (instancetype)initWithPath:(ResourcePath)path firestore:(Firestore *)firestore {
7980
if (path.size() % 2 != 0) {
80-
FSTThrowInvalidArgument(@"Invalid document reference. Document references must have an even "
81-
"number of segments, but %s has %zu",
82-
path.CanonicalString().c_str(), path.size());
81+
ThrowInvalidArgument("Invalid document reference. Document references must have an even "
82+
"number of segments, but %s has %s",
83+
path.CanonicalString(), path.size());
8384
}
8485
return [self initWithKey:DocumentKey{std::move(path)} firestore:firestore];
8586
}
@@ -125,7 +126,7 @@ - (NSString *)path {
125126

126127
- (FIRCollectionReference *)collectionWithPath:(NSString *)collectionPath {
127128
if (!collectionPath) {
128-
FSTThrowInvalidArgument(@"Collection path cannot be nil.");
129+
ThrowInvalidArgument("Collection path cannot be nil.");
129130
}
130131

131132
ResourcePath subPath = ResourcePath::FromString(util::MakeString(collectionPath));

Firestore/Source/API/FIRDocumentSnapshot.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
#import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
2929
#import "Firestore/Source/Model/FSTDocument.h"
3030
#import "Firestore/Source/Model/FSTFieldValue.h"
31-
#import "Firestore/Source/Util/FSTUsageValidation.h"
3231

3332
#include "Firestore/core/src/firebase/firestore/api/document_snapshot.h"
3433
#include "Firestore/core/src/firebase/firestore/api/firestore.h"
34+
#include "Firestore/core/src/firebase/firestore/api/input_validation.h"
3535
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
3636
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
3737
#include "Firestore/core/src/firebase/firestore/model/field_value.h"
@@ -41,6 +41,7 @@
4141
namespace util = firebase::firestore::util;
4242
using firebase::firestore::api::DocumentSnapshot;
4343
using firebase::firestore::api::Firestore;
44+
using firebase::firestore::api::ThrowInvalidArgument;
4445
using firebase::firestore::model::DatabaseId;
4546
using firebase::firestore::model::DocumentKey;
4647
using firebase::firestore::model::FieldValue;
@@ -163,7 +164,7 @@ - (nullable id)valueForField:(id)field
163164
} else if ([field isKindOfClass:[FIRFieldPath class]]) {
164165
fieldPath = field;
165166
} else {
166-
FSTThrowInvalidArgument(@"Subscript key must be an NSString or FIRFieldPath.");
167+
ThrowInvalidArgument("Subscript key must be an NSString or FIRFieldPath.");
167168
}
168169

169170
FSTFieldValue *fieldValue = _snapshot.GetValue(fieldPath.internalValue);

Firestore/Source/API/FIRFieldPath.mm

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
#include <vector>
2323

2424
#import "Firestore/Source/API/FIRFieldPath+Internal.h"
25-
#import "Firestore/Source/Util/FSTUsageValidation.h"
2625

26+
#include "Firestore/core/src/firebase/firestore/api/input_validation.h"
2727
#include "Firestore/core/src/firebase/firestore/model/field_path.h"
2828
#include "Firestore/core/src/firebase/firestore/util/hashing.h"
2929
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
3030

3131
namespace util = firebase::firestore::util;
32+
using firebase::firestore::api::ThrowInvalidArgument;
3233
using firebase::firestore::model::FieldPath;
3334

3435
NS_ASSUME_NONNULL_BEGIN
@@ -44,14 +45,14 @@ @implementation FIRFieldPath
4445

4546
- (instancetype)initWithFields:(NSArray<NSString *> *)fieldNames {
4647
if (fieldNames.count == 0) {
47-
FSTThrowInvalidArgument(@"Invalid field path. Provided names must not be empty.");
48+
ThrowInvalidArgument("Invalid field path. Provided names must not be empty.");
4849
}
4950

5051
std::vector<std::string> field_names;
5152
field_names.reserve(fieldNames.count);
5253
for (int i = 0; i < fieldNames.count; ++i) {
5354
if (fieldNames[i].length == 0) {
54-
FSTThrowInvalidArgument(@"Invalid field name at index %d. Field names must not be empty.", i);
55+
ThrowInvalidArgument("Invalid field name at index %s. Field names must not be empty.", i);
5556
}
5657
field_names.emplace_back(util::MakeString(fieldNames[i]));
5758
}
@@ -75,15 +76,15 @@ + (instancetype)pathWithDotSeparatedString:(NSString *)path {
7576
numberOfMatchesInString:path
7677
options:0
7778
range:NSMakeRange(0, path.length)] > 0) {
78-
FSTThrowInvalidArgument(
79-
@"Invalid field path (%@). Paths must not contain '~', '*', '/', '[', or ']'", path);
79+
ThrowInvalidArgument(
80+
"Invalid field path (%s). Paths must not contain '~', '*', '/', '[', or ']'", path);
8081
}
8182
@try {
8283
return [[FIRFieldPath alloc] initWithFields:[path componentsSeparatedByString:@"."]];
8384
} @catch (NSException *exception) {
84-
FSTThrowInvalidArgument(
85-
@"Invalid field path (%@). Paths must not be empty, begin with '.', end with '.', or "
86-
@"contain '..'",
85+
ThrowInvalidArgument(
86+
"Invalid field path (%s). Paths must not be empty, begin with '.', end with '.', or "
87+
"contain '..'",
8788
path);
8889
}
8990
}

Firestore/Source/API/FIRFirestore.mm

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
#import "Firestore/Source/API/FIRFirestore+Internal.h"
3333
#import "Firestore/Source/API/FSTFirestoreComponent.h"
3434
#import "Firestore/Source/API/FSTUserDataConverter.h"
35-
#import "Firestore/Source/Util/FSTUsageValidation.h"
3635

3736
#include "Firestore/core/src/firebase/firestore/api/firestore.h"
37+
#include "Firestore/core/src/firebase/firestore/api/input_validation.h"
3838
#include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
3939
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
4040
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
@@ -46,6 +46,8 @@
4646
namespace util = firebase::firestore::util;
4747
using firebase::firestore::api::DocumentReference;
4848
using firebase::firestore::api::Firestore;
49+
using firebase::firestore::api::ThrowIllegalState;
50+
using firebase::firestore::api::ThrowInvalidArgument;
4951
using firebase::firestore::auth::CredentialsProvider;
5052
using firebase::firestore::model::DatabaseId;
5153
using firebase::firestore::util::AsyncQueue;
@@ -109,9 +111,8 @@ + (void)initialize {
109111
+ (instancetype)firestore {
110112
FIRApp *app = [FIRApp defaultApp];
111113
if (!app) {
112-
FSTThrowInvalidUsage(@"FIRAppNotConfiguredException",
113-
@"Failed to get FirebaseApp instance. Please call FirebaseApp.configure() "
114-
@"before using Firestore");
114+
ThrowIllegalState("Failed to get FirebaseApp instance. Please call FirebaseApp.configure() "
115+
"before using Firestore");
115116
}
116117
return [self firestoreForApp:app database:util::WrapNSString(DatabaseId::kDefault)];
117118
}
@@ -123,13 +124,13 @@ + (instancetype)firestoreForApp:(FIRApp *)app {
123124
// TODO(b/62410906): make this public
124125
+ (instancetype)firestoreForApp:(FIRApp *)app database:(NSString *)database {
125126
if (!app) {
126-
FSTThrowInvalidArgument(@"FirebaseApp instance may not be nil. Use FirebaseApp.app() if you'd "
127-
"like to use the default FirebaseApp instance.");
127+
ThrowInvalidArgument("FirebaseApp instance may not be nil. Use FirebaseApp.app() if you'd like "
128+
"to use the default FirebaseApp instance.");
128129
}
129130
if (!database) {
130-
FSTThrowInvalidArgument(@"database identifier may not be nil. Use '%s' if you want the default "
131-
"database",
132-
DatabaseId::kDefault);
131+
ThrowInvalidArgument("Database identifier may not be nil. Use '%s' if you want the default "
132+
"database",
133+
DatabaseId::kDefault);
133134
}
134135

135136
id<FSTFirestoreMultiDBProvider> provider =
@@ -182,22 +183,21 @@ - (FSTFirestoreClient *)client {
182183

183184
- (FIRCollectionReference *)collectionWithPath:(NSString *)collectionPath {
184185
if (!collectionPath) {
185-
FSTThrowInvalidArgument(@"Collection path cannot be nil.");
186+
ThrowInvalidArgument("Collection path cannot be nil.");
186187
}
187188
if ([collectionPath containsString:@"//"]) {
188-
FSTThrowInvalidArgument(@"Invalid path (%@). Paths must not contain // in them.",
189-
collectionPath);
189+
ThrowInvalidArgument("Invalid path (%s). Paths must not contain // in them.", collectionPath);
190190
}
191191

192192
return _firestore->GetCollection(util::MakeString(collectionPath));
193193
}
194194

195195
- (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
196196
if (!documentPath) {
197-
FSTThrowInvalidArgument(@"Document path cannot be nil.");
197+
ThrowInvalidArgument("Document path cannot be nil.");
198198
}
199199
if ([documentPath containsString:@"//"]) {
200-
FSTThrowInvalidArgument(@"Invalid path (%@). Paths must not contain // in them.", documentPath);
200+
ThrowInvalidArgument("Invalid path (%s). Paths must not contain // in them.", documentPath);
201201
}
202202

203203
DocumentReference documentReference = _firestore->GetDocument(util::MakeString(documentPath));
@@ -215,9 +215,9 @@ - (void)runTransactionWithBlock:(id _Nullable (^)(FIRTransaction *, NSError **))
215215
// We wrap the function they provide in order to use internal implementation classes for
216216
// transaction, and to run the user callback block on the proper queue.
217217
if (!updateBlock) {
218-
FSTThrowInvalidArgument(@"Transaction block cannot be nil.");
218+
ThrowInvalidArgument("Transaction block cannot be nil.");
219219
} else if (!completion) {
220-
FSTThrowInvalidArgument(@"Transaction completion block cannot be nil.");
220+
ThrowInvalidArgument("Transaction completion block cannot be nil.");
221221
}
222222

223223
_firestore->RunTransaction(updateBlock, queue, completion);
@@ -275,11 +275,11 @@ + (FIRFirestore *)recoverFromFirestore:(Firestore *)firestore {
275275

276276
- (FIRQuery *)collectionGroupWithID:(NSString *)collectionID {
277277
if (!collectionID) {
278-
FSTThrowInvalidArgument(@"Collection ID cannot be nil.");
278+
ThrowInvalidArgument("Collection ID cannot be nil.");
279279
}
280280
if ([collectionID containsString:@"/"]) {
281-
FSTThrowInvalidArgument(
282-
@"Invalid collection ID (%@). Collection IDs must not contain / in them.", collectionID);
281+
ThrowInvalidArgument("Invalid collection ID (%s). Collection IDs must not contain / in them.",
282+
collectionID);
283283
}
284284

285285
return _firestore->GetCollectionGroup(collectionID);

Firestore/Source/API/FIRFirestoreSettings.mm

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "Firestore/core/src/firebase/firestore/util/warnings.h"
18-
1917
#import "FIRFirestoreSettings.h"
2018

21-
#import "Firestore/Source/Util/FSTUsageValidation.h"
19+
#include "Firestore/core/src/firebase/firestore/api/input_validation.h"
20+
#include "Firestore/core/src/firebase/firestore/util/warnings.h"
2221

2322
NS_ASSUME_NONNULL_BEGIN
2423

24+
using firebase::firestore::api::ThrowInvalidArgument;
25+
2526
static NSString *const kDefaultHost = @"firestore.googleapis.com";
2627
static const BOOL kDefaultSSLEnabled = YES;
2728
static const BOOL kDefaultPersistenceEnabled = YES;
@@ -88,28 +89,27 @@ - (id)copyWithZone:(nullable NSZone *)zone {
8889

8990
- (void)setHost:(NSString *)host {
9091
if (!host) {
91-
FSTThrowInvalidArgument(
92-
@"host setting may not be nil. You should generally just use the default value "
93-
"(which is %@)",
94-
kDefaultHost);
92+
ThrowInvalidArgument("Host setting may not be nil. You should generally just use the default "
93+
"value (which is %s)",
94+
kDefaultHost);
9595
}
9696
_host = [host mutableCopy];
9797
}
9898

9999
- (void)setDispatchQueue:(dispatch_queue_t)dispatchQueue {
100100
if (!dispatchQueue) {
101-
FSTThrowInvalidArgument(
102-
@"dispatch queue setting may not be nil. Create a new dispatch queue with "
103-
"dispatch_queue_create(\"com.example.MyQueue\", NULL) or just use the default "
104-
"(which is the main queue, returned from dispatch_get_main_queue())");
101+
ThrowInvalidArgument(
102+
"Dispatch queue setting may not be nil. Create a new dispatch queue with "
103+
"dispatch_queue_create(\"com.example.MyQueue\", NULL) or just use the default (which is "
104+
"the main queue, returned from dispatch_get_main_queue())");
105105
}
106106
_dispatchQueue = dispatchQueue;
107107
}
108108

109109
- (void)setCacheSizeBytes:(int64_t)cacheSizeBytes {
110110
if (cacheSizeBytes != kFIRFirestoreCacheSizeUnlimited &&
111111
cacheSizeBytes < kMinimumCacheSizeBytes) {
112-
FSTThrowInvalidArgument(@"Cache size must be set to at least %i bytes", kMinimumCacheSizeBytes);
112+
ThrowInvalidArgument("Cache size must be set to at least %s bytes", kMinimumCacheSizeBytes);
113113
}
114114
_cacheSizeBytes = cacheSizeBytes;
115115
}

Firestore/Source/API/FIRGeoPoint.mm

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
#import "Firestore/Source/API/FIRGeoPoint+Internal.h"
1818

19-
#import "Firestore/core/src/firebase/firestore/util/comparison.h"
20-
21-
#import "Firestore/Source/Util/FSTUsageValidation.h"
19+
#include "Firestore/core/src/firebase/firestore/api/input_validation.h"
20+
#include "Firestore/core/src/firebase/firestore/util/comparison.h"
2221

22+
using firebase::firestore::api::ThrowInvalidArgument;
2323
using firebase::firestore::util::DoubleBitwiseEquals;
2424
using firebase::firestore::util::DoubleBitwiseHash;
2525
using firebase::firestore::util::WrapCompare;
@@ -31,14 +31,14 @@ @implementation FIRGeoPoint
3131
- (instancetype)initWithLatitude:(double)latitude longitude:(double)longitude {
3232
if (self = [super init]) {
3333
if (latitude < -90 || latitude > 90 || !isfinite(latitude)) {
34-
FSTThrowInvalidArgument(@"GeoPoint requires a latitude value in the range of [-90, 90], "
35-
"but was %f",
36-
latitude);
34+
ThrowInvalidArgument("GeoPoint requires a latitude value in the range of [-90, 90], "
35+
"but was %s",
36+
latitude);
3737
}
3838
if (longitude < -180 || longitude > 180 || !isfinite(longitude)) {
39-
FSTThrowInvalidArgument(@"GeoPoint requires a longitude value in the range of [-180, 180], "
40-
"but was %f",
41-
longitude);
39+
ThrowInvalidArgument("GeoPoint requires a longitude value in the range of [-180, 180], "
40+
"but was %s",
41+
longitude);
4242
}
4343

4444
_latitude = latitude;

0 commit comments

Comments
 (0)