Skip to content

Commit bd7a8df

Browse files
authored
Mila/multi db add get instance header (#1267)
1 parent 8b42263 commit bd7a8df

File tree

3 files changed

+117
-12
lines changed

3 files changed

+117
-12
lines changed

firestore/integration_test_internal/src/validation_test.cc

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,37 @@ TEST_F(ValidationTest, DisableSslWithoutSettingHostFails) {
407407
}
408408

409409
TEST_F(ValidationTest, FirestoreGetInstanceWithNullAppFails) {
410-
EXPECT_ERROR(
411-
Firestore::GetInstance(/*app=*/nullptr, /*init_result=*/nullptr),
412-
"firebase::App instance cannot be null. Use "
413-
"firebase::App::GetInstance() without arguments if you'd like to use "
414-
"the default instance.");
410+
EXPECT_ERROR(Firestore::GetInstance(/*app=*/(App*)nullptr,
411+
/*init_result=*/(InitResult*)nullptr),
412+
"firebase::App instance cannot be null. Use other "
413+
"firebase::App::GetInstance() if you'd like to use the default "
414+
"instance.");
415+
}
416+
417+
TEST_F(ValidationTest, FirestoreGetInstanceWithNullDatabaseNameFails) {
418+
EXPECT_ERROR(Firestore::GetInstance(/*db_name=*/(char*)nullptr,
419+
/*init_result=*/(InitResult*)nullptr),
420+
"Provided database ID must not be null. Use other "
421+
"firebase::App::GetInstance() if you'd like to use the default "
422+
"database ID.");
423+
}
424+
425+
TEST_F(ValidationTest,
426+
FirestoreGetInstanceWithNonNullDatabaseIdButNullAppFails) {
427+
EXPECT_ERROR(Firestore::GetInstance(/*app=*/(App*)nullptr, "foo",
428+
/*init_result=*/(InitResult*)nullptr),
429+
"firebase::App instance cannot be null. Use other "
430+
"firebase::App::GetInstance() if you'd like to use the default "
431+
"instance.");
432+
}
433+
434+
TEST_F(ValidationTest,
435+
FirestoreGetInstanceWithNonNullAppButNullDatabaseNameFails) {
436+
EXPECT_ERROR(Firestore::GetInstance(app(), /*db_name=*/(char*)nullptr,
437+
/*init_result=*/(InitResult*)nullptr),
438+
"Provided database ID must not be null. Use other "
439+
"firebase::App::GetInstance() if you'd like to use the default "
440+
"database ID.");
415441
}
416442

417443
TEST_F(ValidationTest,

firestore/src/common/firestore.cc

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,18 @@ InitResult CheckInitialized(const FirestoreInternal& firestore) {
101101
void ValidateApp(App* app) {
102102
if (!app) {
103103
SimpleThrowInvalidArgument(
104-
"firebase::App instance cannot be null. Use "
105-
"firebase::App::GetInstance() without arguments if you'd like to use "
106-
"the default instance.");
104+
"firebase::App instance cannot be null. Use other "
105+
"firebase::App::GetInstance() if you'd like to use the default "
106+
"instance.");
107+
}
108+
}
109+
110+
void ValidateDatabase(const char* db_name) {
111+
if (!db_name) {
112+
SimpleThrowInvalidArgument(
113+
"Provided database ID must not be null. Use other "
114+
"firebase::App::GetInstance() if you'd like to use the default "
115+
"database ID.");
107116
}
108117
}
109118

@@ -133,6 +142,23 @@ Firestore* Firestore::GetInstance(InitResult* init_result_out) {
133142
return Firestore::GetInstance(app, init_result_out);
134143
}
135144

145+
Firestore* Firestore::GetInstance(App* app,
146+
const char* db_name,
147+
InitResult* init_result_out) {
148+
ValidateApp(app);
149+
ValidateDatabase(db_name);
150+
// TODO(Mila): Implement code
151+
return Firestore::GetInstance(app, init_result_out);
152+
}
153+
154+
Firestore* Firestore::GetInstance(const char* db_name,
155+
InitResult* init_result_out) {
156+
ValidateDatabase(db_name);
157+
// TODO(Mila): Implement code
158+
App* app = App::GetInstance();
159+
return Firestore::GetInstance(app, init_result_out);
160+
}
161+
136162
Firestore* Firestore::CreateFirestore(App* app,
137163
FirestoreInternal* internal,
138164
InitResult* init_result_out) {

firestore/src/include/firebase/firestore.h

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ class TransactionManager;
8989
class Firestore {
9090
public:
9191
/**
92-
* @brief Returns an instance of Firestore corresponding to the given App.
92+
* @brief Returns an instance of Firestore corresponding to the given App
93+
* with default database ID.
9394
*
9495
* Firebase Firestore uses firebase::App to communicate with Firebase
9596
* Authentication to authenticate users to the Firestore server backend.
@@ -104,13 +105,15 @@ class Firestore {
104105
* succeeded, or firebase::kInitResultFailedMissingDependency on Android if
105106
* Google Play services is not available on the current device.
106107
*
107-
* @return An instance of Firestore corresponding to the given App.
108+
* @return An instance of Firestore corresponding to the given App with
109+
* default database ID.
108110
*/
109111
static Firestore* GetInstance(::firebase::App* app,
110112
InitResult* init_result_out = nullptr);
111113

112114
/**
113-
* @brief Returns an instance of Firestore corresponding to the default App.
115+
* @brief Returns an instance of Firestore corresponding to the default App
116+
* with default database ID.
114117
*
115118
* Firebase Firestore uses the default App to communicate with Firebase
116119
* Authentication to authenticate users to the Firestore server backend.
@@ -122,10 +125,60 @@ class Firestore {
122125
* succeeded, or firebase::kInitResultFailedMissingDependency on Android if
123126
* Google Play services is not available on the current device.
124127
*
125-
* @return An instance of Firestore corresponding to the default App.
128+
* @return An instance of Firestore corresponding to the default App
129+
* with default database ID.
126130
*/
127131
static Firestore* GetInstance(InitResult* init_result_out = nullptr);
128132

133+
/**
134+
* @brief Returns an instance of Firestore corresponding to the given App with
135+
* the given database ID.
136+
*
137+
* Firebase Firestore uses firebase::App to communicate with Firebase
138+
* Authentication to authenticate users to the Firestore server backend.
139+
*
140+
* If you call GetInstance() multiple times with the same App, you will get
141+
* the same instance of Firestore.
142+
*
143+
* @param[in] app Your instance of firebase::App. Firebase Firestore will use
144+
* this to communicate with Firebase Authentication.
145+
* @param[in] db_name Name of the database. Firebase Firestore will use
146+
* this to communicate with Firebase Authentication.
147+
* @param[out] init_result_out If provided, the initialization result will be
148+
* written here. Will be set to firebase::kInitResultSuccess if initialization
149+
* succeeded, or firebase::kInitResultFailedMissingDependency on Android if
150+
* Google Play services is not available on the current device.
151+
*
152+
* @return An instance of Firestore corresponding to the given App with
153+
* the given database ID.
154+
*/
155+
static Firestore* GetInstance(::firebase::App* app,
156+
const char* db_name,
157+
InitResult* init_result_out = nullptr);
158+
159+
/**
160+
* @brief Returns an instance of Firestore corresponding to the default App
161+
* with the given database ID.
162+
*
163+
* Firebase Firestore uses firebase::App to communicate with Firebase
164+
* Authentication to authenticate users to the Firestore server backend.
165+
*
166+
* If you call GetInstance() multiple times with the same App, you will get
167+
* the same instance of Firestore.
168+
*
169+
* @param[in] db_name Name of the database. Firebase Firestore will use
170+
* this to communicate with Firebase Authentication.
171+
* @param[out] init_result_out If provided, the initialization result will be
172+
* written here. Will be set to firebase::kInitResultSuccess if initialization
173+
* succeeded, or firebase::kInitResultFailedMissingDependency on Android if
174+
* Google Play services is not available on the current device.
175+
*
176+
* @return An instance of Firestore corresponding to the default App with
177+
* the given database ID.
178+
*/
179+
static Firestore* GetInstance(const char* db_name,
180+
InitResult* init_result_out = nullptr);
181+
129182
/**
130183
* @brief Destructor for the Firestore object.
131184
*

0 commit comments

Comments
 (0)