Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method to set emulator host programmatically (#319) #336

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -60,6 +60,7 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
private final String databaseId;
private final TransportChannelProvider channelProvider;
private final CredentialsProvider credentialsProvider;
private final String emulatorHost;

public static class DefaultFirestoreFactory implements FirestoreFactory {

Expand Down Expand Up @@ -114,11 +115,16 @@ public TransportChannelProvider getTransportChannelProvider() {
return channelProvider;
}

public String getEmulatorHost() {
return emulatorHost;
}

public static class Builder extends ServiceOptions.Builder<Firestore, FirestoreOptions, Builder> {

@Nullable private String databaseId = null;
@Nullable private TransportChannelProvider channelProvider = null;
@Nullable private CredentialsProvider credentialsProvider = null;
@Nullable private String emulatorHost = null;

private Builder() {}

Expand All @@ -127,6 +133,7 @@ private Builder(FirestoreOptions options) {
this.databaseId = options.databaseId;
this.channelProvider = options.channelProvider;
this.credentialsProvider = options.credentialsProvider;
this.emulatorHost = options.emulatorHost;
}

/**
Expand Down Expand Up @@ -174,6 +181,17 @@ public Builder setCredentialsProvider(@Nonnull CredentialsProvider credentialsPr
return this;
}

/**
* Sets the emulator host to use with this Firestore client. The value passed to this method
* will take precedent if the {@code FIRESTORE_EMULATOR_HOST} environment variable is also set.
*
* @param emulatorHost The Firestore emulator host to use with this client.
*/
public Builder setEmulatorHost(@Nonnull String emulatorHost) {
this.emulatorHost = emulatorHost;
return this;
}

/**
* Sets the database ID to use with this Firestore client.
*
Expand All @@ -196,7 +214,9 @@ public FirestoreOptions build() {
}

// 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 {
Expand Down Expand Up @@ -280,6 +300,8 @@ protected FirestoreOptions(Builder builder) {
builder.credentialsProvider != null
? builder.credentialsProvider
: GrpcTransportOptions.setUpCredentialsProvider(this);

this.emulatorHost = builder.emulatorHost;
}

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