firestore: make collref IDs 20 characters
This is to be consistent with other Firestore clients.
This continues to use crypto/rand because it's better than a p-rng and
the performance impact is minimal. See
https://code-review.googlesource.com/c/gocloud/+/44570 for the change
from 20 characters and more discussion about the performance of
crypto/rand here.
Fixes #1715.
Change-Id: I3faa00a23bb1774ab188aeb4830a10e796373218
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/50250
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Cody Oss <codyoss@google.com>
Reviewed-by: Sebastian Schmidt <mrschmidt@google.com>
diff --git a/firestore/collref.go b/firestore/collref.go
index 6dff217..73cecb6 100644
--- a/firestore/collref.go
+++ b/firestore/collref.go
@@ -17,7 +17,6 @@
import (
"context"
"crypto/rand"
- "encoding/base64"
"fmt"
)
@@ -125,10 +124,15 @@
return newDocumentRefIterator(ctx, c, nil)
}
+const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
+
func uniqueID() string {
- b := make([]byte, 32)
+ b := make([]byte, 20)
if _, err := rand.Read(b); err != nil {
panic(fmt.Sprintf("firestore: crypto/rand.Read error: %v", err))
}
- return base64.RawURLEncoding.EncodeToString(b)
+ for i, byt := range b {
+ b[i] = alphanum[int(byt)%len(alphanum)]
+ }
+ return string(b)
}
diff --git a/firestore/collref_test.go b/firestore/collref_test.go
index 4624cbb..410bf4c 100644
--- a/firestore/collref_test.go
+++ b/firestore/collref_test.go
@@ -16,7 +16,6 @@
import (
"context"
- "encoding/base64"
"testing"
"github.com/golang/protobuf/proto"
@@ -44,12 +43,8 @@
if got.Parent != coll {
t.Errorf("NewDoc got %v, want %v", got.Parent, coll)
}
- b, err := base64.RawURLEncoding.DecodeString(got.ID)
- if err != nil {
- t.Fatalf("NewDoc DecodeToString(%q) got err: %v", got.ID, err)
- }
- if len(b) != 32 {
- t.Errorf("NewDoc got %d-byte ID, wanted 32", len(b))
+ if len(got.ID) != 20 {
+ t.Errorf("got %d-char ID, wanted 20", len(got.ID))
}
got2 := coll.NewDoc()