From b3c9f2554cdef230424da6d5bd5ae55e7d60845a Mon Sep 17 00:00:00 2001 From: Raymond Ho Date: Tue, 31 Dec 2024 23:14:01 -0800 Subject: [PATCH 1/2] feat(firestore): add UsingEmulator field to Client --- firestore/client.go | 20 ++++++++++++-------- firestore/client_test.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/firestore/client.go b/firestore/client.go index 3049e2203117..334c5a569699 100644 --- a/firestore/client.go +++ b/firestore/client.go @@ -69,10 +69,11 @@ const DefaultDatabaseID = "(default)" // A Client provides access to the Firestore service. type Client struct { - c *vkit.Client - projectID string - databaseID string // A client is tied to a single database. - readSettings *readSettings // readSettings allows setting a snapshot time to read the database + c *vkit.Client + projectID string + databaseID string // A client is tied to a single database. + readSettings *readSettings // readSettings allows setting a snapshot time to read the database + UsingEmulator bool // a boolean that indicates if the client is using the emulator } // NewClient creates a new Firestore client that uses the given project. @@ -81,12 +82,14 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio return nil, errors.New("firestore: projectID was empty") } var o []option.ClientOption + var usingEmulator bool // If this environment variable is defined, configure the client to talk to the emulator. if addr := os.Getenv("FIRESTORE_EMULATOR_HOST"); addr != "" { conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithPerRPCCredentials(emulatorCreds{})) if err != nil { return nil, fmt.Errorf("firestore: dialing address from env var FIRESTORE_EMULATOR_HOST: %s", err) } + usingEmulator = true o = []option.ClientOption{option.WithGRPCConn(conn)} projectID, _ = detect.ProjectID(ctx, projectID, "", opts...) if projectID == "" { @@ -107,10 +110,11 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio } vc.SetGoogleClientInfo("gccl", internal.Version) c := &Client{ - c: vc, - projectID: projectID, - databaseID: DefaultDatabaseID, - readSettings: &readSettings{}, + c: vc, + projectID: projectID, + databaseID: DefaultDatabaseID, + readSettings: &readSettings{}, + UsingEmulator: usingEmulator, } return c, nil } diff --git a/firestore/client_test.go b/firestore/client_test.go index f710aaea07bc..2ed7cb1113c5 100644 --- a/firestore/client_test.go +++ b/firestore/client_test.go @@ -16,6 +16,7 @@ package firestore import ( "context" + "os" "testing" "time" @@ -399,3 +400,19 @@ func TestClient_WithReadOptions(t *testing.T) { t.Fatal(err) } } + +func TestClient_UsingEmulator(t *testing.T) { + c, _, cleanup := newMock(t) + defer cleanup() + if c.UsingEmulator { + t.Error("got true, want false") + } + + os.Setenv("FIRESTORE_EMULATOR_HOST", "localhost:8080") + defer os.Unsetenv("FIRESTORE_EMULATOR_HOST") + c, _, cleanup = newMock(t) + defer cleanup() + if !c.UsingEmulator { + t.Error("got false, want true") + } +} \ No newline at end of file From ceaeab40714f9c36529a84b555f0cc4fb3a67945 Mon Sep 17 00:00:00 2001 From: Raymond Ho Date: Fri, 3 Jan 2025 11:14:59 -0800 Subject: [PATCH 2/2] rename to UsesEmulator --- firestore/client.go | 24 ++++++++++++------------ firestore/client_test.go | 8 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/firestore/client.go b/firestore/client.go index 334c5a569699..1cad80581c7c 100644 --- a/firestore/client.go +++ b/firestore/client.go @@ -69,11 +69,11 @@ const DefaultDatabaseID = "(default)" // A Client provides access to the Firestore service. type Client struct { - c *vkit.Client - projectID string - databaseID string // A client is tied to a single database. - readSettings *readSettings // readSettings allows setting a snapshot time to read the database - UsingEmulator bool // a boolean that indicates if the client is using the emulator + c *vkit.Client + projectID string + databaseID string // A client is tied to a single database. + readSettings *readSettings // readSettings allows setting a snapshot time to read the database + UsesEmulator bool // a boolean that indicates if the client is using the emulator } // NewClient creates a new Firestore client that uses the given project. @@ -82,14 +82,14 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio return nil, errors.New("firestore: projectID was empty") } var o []option.ClientOption - var usingEmulator bool + var usesEmulator bool // If this environment variable is defined, configure the client to talk to the emulator. if addr := os.Getenv("FIRESTORE_EMULATOR_HOST"); addr != "" { conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithPerRPCCredentials(emulatorCreds{})) if err != nil { return nil, fmt.Errorf("firestore: dialing address from env var FIRESTORE_EMULATOR_HOST: %s", err) } - usingEmulator = true + usesEmulator = true o = []option.ClientOption{option.WithGRPCConn(conn)} projectID, _ = detect.ProjectID(ctx, projectID, "", opts...) if projectID == "" { @@ -110,11 +110,11 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio } vc.SetGoogleClientInfo("gccl", internal.Version) c := &Client{ - c: vc, - projectID: projectID, - databaseID: DefaultDatabaseID, - readSettings: &readSettings{}, - UsingEmulator: usingEmulator, + c: vc, + projectID: projectID, + databaseID: DefaultDatabaseID, + readSettings: &readSettings{}, + UsesEmulator: usesEmulator, } return c, nil } diff --git a/firestore/client_test.go b/firestore/client_test.go index 2ed7cb1113c5..9909352a5865 100644 --- a/firestore/client_test.go +++ b/firestore/client_test.go @@ -401,10 +401,10 @@ func TestClient_WithReadOptions(t *testing.T) { } } -func TestClient_UsingEmulator(t *testing.T) { +func TestClient_UsesEmulator(t *testing.T) { c, _, cleanup := newMock(t) defer cleanup() - if c.UsingEmulator { + if c.UsesEmulator { t.Error("got true, want false") } @@ -412,7 +412,7 @@ func TestClient_UsingEmulator(t *testing.T) { defer os.Unsetenv("FIRESTORE_EMULATOR_HOST") c, _, cleanup = newMock(t) defer cleanup() - if !c.UsingEmulator { + if !c.UsesEmulator { t.Error("got false, want true") } -} \ No newline at end of file +}