Skip to content

Commit 97037f4

Browse files
authored
feat: add method to set emulator host programmatically (#319) (#336)
See for initial discussion: #319 Fixes #319, #210. Relates to #190. Similar issues in other SDKs: googleapis/google-cloud-go#1978, firebase/firebase-admin-node#776.
1 parent 4c2329b commit 97037f4

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
6060
private final String databaseId;
6161
private final TransportChannelProvider channelProvider;
6262
private final CredentialsProvider credentialsProvider;
63+
private final String emulatorHost;
6364

6465
public static class DefaultFirestoreFactory implements FirestoreFactory {
6566

@@ -114,11 +115,16 @@ public TransportChannelProvider getTransportChannelProvider() {
114115
return channelProvider;
115116
}
116117

118+
public String getEmulatorHost() {
119+
return emulatorHost;
120+
}
121+
117122
public static class Builder extends ServiceOptions.Builder<Firestore, FirestoreOptions, Builder> {
118123

119124
@Nullable private String databaseId = null;
120125
@Nullable private TransportChannelProvider channelProvider = null;
121126
@Nullable private CredentialsProvider credentialsProvider = null;
127+
@Nullable private String emulatorHost = null;
122128

123129
private Builder() {}
124130

@@ -127,6 +133,7 @@ private Builder(FirestoreOptions options) {
127133
this.databaseId = options.databaseId;
128134
this.channelProvider = options.channelProvider;
129135
this.credentialsProvider = options.credentialsProvider;
136+
this.emulatorHost = options.emulatorHost;
130137
}
131138

132139
/**
@@ -174,6 +181,17 @@ public Builder setCredentialsProvider(@Nonnull CredentialsProvider credentialsPr
174181
return this;
175182
}
176183

184+
/**
185+
* Sets the emulator host to use with this Firestore client. The value passed to this method
186+
* will take precedent if the {@code FIRESTORE_EMULATOR_HOST} environment variable is also set.
187+
*
188+
* @param emulatorHost The Firestore emulator host to use with this client.
189+
*/
190+
public Builder setEmulatorHost(@Nonnull String emulatorHost) {
191+
this.emulatorHost = emulatorHost;
192+
return this;
193+
}
194+
177195
/**
178196
* Sets the database ID to use with this Firestore client.
179197
*
@@ -196,7 +214,9 @@ public FirestoreOptions build() {
196214
}
197215

198216
// Override credentials and channel provider if we are using the emulator.
199-
String emulatorHost = System.getenv(FIRESTORE_EMULATOR_SYSTEM_VARIABLE);
217+
if (emulatorHost == null) {
218+
emulatorHost = System.getenv(FIRESTORE_EMULATOR_SYSTEM_VARIABLE);
219+
}
200220
if (emulatorHost != null) {
201221
// Try creating a host in order to validate that the host name is valid.
202222
try {
@@ -280,6 +300,8 @@ protected FirestoreOptions(Builder builder) {
280300
builder.credentialsProvider != null
281301
? builder.credentialsProvider
282302
: GrpcTransportOptions.setUpCredentialsProvider(this);
303+
304+
this.emulatorHost = builder.emulatorHost;
283305
}
284306

285307
private static class FirestoreDefaults implements ServiceDefaults<Firestore, FirestoreOptions> {

0 commit comments

Comments
 (0)