Skip to content

Provide a setter for emulator host #319

@Gerschtli

Description

@Gerschtli

Is your feature request related to a problem? Please describe.

When I write an integration test and I use a new emulator instance every test with different port numbers, I need to copy the setup logic from FirestoreOptions.Builder.build to configure the connection.

Describe the solution you'd like

I would like to see a setter for the emulator host value, which overrides the value of FIRESTORE_EMULATOR_HOST.
I tried to implement it to clarify my intention:

commit d5e6586e3ba78f3cda6dec0b6a0fb9dcec807cfb
Author: Tobias Happ <[email protected]>
Date:   2020-08-06 21:31:12 +0200

    feat: add getEmulatorHost for FirestoreOptions
---
 .../google/cloud/firestore/FirestoreOptions.java   | 23 +++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java
index a65abca..4542504 100644
--- a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java
+++ b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java
@@ -63,6 +63,7 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
   private final boolean timestampsInSnapshotsEnabled;
   private final TransportChannelProvider channelProvider;
   private final CredentialsProvider credentialsProvider;
+  private final String emulatorHost;
 
   public static class DefaultFirestoreFactory implements FirestoreFactory {
 
@@ -117,12 +118,17 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
     return channelProvider;
   }
 
+  public String getEmulatorHost() {
+    return emulatorHost;
+  }
+
   public static class Builder extends ServiceOptions.Builder<Firestore, FirestoreOptions, Builder> {
 
     @Nullable private String databaseId = null;
     @Nullable private Boolean timestampsInSnapshotsEnabled = null;
     @Nullable private TransportChannelProvider channelProvider = null;
     @Nullable private CredentialsProvider credentialsProvider = null;
+    @Nullable private String emulatorHost = null;
 
     private Builder() {}
 
@@ -132,6 +138,7 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
       this.timestampsInSnapshotsEnabled = options.timestampsInSnapshotsEnabled;
       this.channelProvider = options.channelProvider;
       this.credentialsProvider = options.credentialsProvider;
+      this.emulatorHost = options.emulatorHost;
     }
 
     /**
@@ -189,6 +196,16 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
       return this;
     }
 
+    /**
+     * Sets the emulator host to use with this Firestore client.
+     *
+     * @param emulatorHost The Firestore emulator host to use with this client.
+     */
+    public Builder setEmulatorHost(@Nonnull String emulatorHost) {
+      this.emulatorHost = emulatorHost;
+      return this;
+    }
+
     @Override
     @Nonnull
     public FirestoreOptions build() {
@@ -201,7 +218,9 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
       }
 
       // Override credentials and channel provider if we are using the emulator.
-      String emulatorHost = System.getenv(FIRESTORE_EMULATOR_SYSTEM_VARIABLE);
+      if (emulatorHost == null) {
+        emulatorHost = System.getenv(FIRESTORE_EMULATOR_SYSTEM_VARIABLE);
+      }
       if (emulatorHost != null) {
         // Try creating a host in order to validate that the host name is valid.
         try {
@@ -290,6 +309,8 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
         builder.credentialsProvider != null
             ? builder.credentialsProvider
             : GrpcTransportOptions.setUpCredentialsProvider(this);
+
+    this.emulatorHost = builder.emulatorHost;;
   }
 
   private static class FirestoreDefaults implements ServiceDefaults<Firestore, FirestoreOptions> {

Describe alternatives you've considered

The alternative is to copy the setup logic used in FirestoreOptions.Builder.build but this does unnecessarily increase code duplication and complexity.

Additional information

I would like to contribute to help this feature request get implemented. I am looking forward for your feedback!

Metadata

Metadata

Assignees

Labels

api: firestoreIssues related to the googleapis/java-firestore API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions