From fa413a39cdf8ffe0890a1b0a4cf5e06e0c915592 Mon Sep 17 00:00:00 2001 From: Cory Schwartz Date: Wed, 22 Nov 2023 13:00:37 -0800 Subject: [PATCH] docstore/gcpfilestore: add support for non-default databases. (#3345) --- docstore/gcpfirestore/fs.go | 8 +++++++- docstore/gcpfirestore/fs_test.go | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docstore/gcpfirestore/fs.go b/docstore/gcpfirestore/fs.go index c7527d0c92..5469927778 100644 --- a/docstore/gcpfirestore/fs.go +++ b/docstore/gcpfirestore/fs.go @@ -149,6 +149,12 @@ func CollectionResourceID(projectID, collPath string) string { return fmt.Sprintf("projects/%s/databases/(default)/documents/%s", projectID, collPath) } +// CollectResoureceIDWithDatabase constructs a resource ID for a collection from the project ID, database ID, and the collection path. +// See the OpenCollection example for use. +func CollectionResourceIDWithDatabase(projectID, databaseID, collPath string) string { + return fmt.Sprintf("projects/%s/databases/%s/documents/%s", projectID, databaseID, collPath) +} + // OpenCollection creates a *docstore.Collection representing a Firestore collection. // // collResourceID must be of the form "project//databases/(default)/documents/". @@ -193,7 +199,7 @@ func OpenCollectionWithNameFunc(client *vkit.Client, collResourceID string, name return docstore.NewCollection(c), nil } -var resourceIDRE = regexp.MustCompile(`^(projects/[^/]+/databases/\(default\))/documents/.+`) +var resourceIDRE = regexp.MustCompile(`^(projects/[^/]+/databases/[^/]+)/documents/.+`) func newCollection(client *vkit.Client, collResourceID, nameField string, nameFunc func(docstore.Document) string, opts *Options) (*collection, error) { if nameField == "" && nameFunc == nil { diff --git a/docstore/gcpfirestore/fs_test.go b/docstore/gcpfirestore/fs_test.go index 3e3bb1a172..fb0b588dfe 100644 --- a/docstore/gcpfirestore/fs_test.go +++ b/docstore/gcpfirestore/fs_test.go @@ -183,6 +183,7 @@ func TestResourceIDRegexp(t *testing.T) { for _, good := range []string{ "projects/abc-_.309/databases/(default)/documents/C", "projects/P/databases/(default)/documents/C/D/E", + "projects/P/databases/mydb/documents/E/F/G", } { if !resourceIDRE.MatchString(good) { t.Errorf("%q did not match but should have", good) @@ -194,7 +195,6 @@ func TestResourceIDRegexp(t *testing.T) { "Projects/P/databases/(default)/documents/C", "P/databases/(default)/documents/C", "projects/P/Q/databases/(default)/documents/C", - "projects/P/databases/mydb/documents/C", "projects/P/databases/(default)/C", "projects/P/databases/(default)/documents/", "projects/P/databases/(default)",