firestore: remove Exists(false) precondition
BREAKING CHANGE: Exists(false) is no longer supported; replace Exists(true) with Exists.
There is never a reason to specify a precondition that requires a
document to not exist. Remove the ability to do so, and change Exists
from a func(bool) to a variable.
Change-Id: I8bca39f982f33479f5a22ece2317c23c8bbc479a
Reviewed-on: https://code-review.googlesource.com/19690
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Michael Darakananda <pongad@google.com>
diff --git a/firestore/cross_language_test.go b/firestore/cross_language_test.go
index d85ec4f..a226e82 100644
--- a/firestore/cross_language_test.go
+++ b/firestore/cross_language_test.go
@@ -226,7 +226,7 @@
var pc Precondition
switch fp := fp.ConditionType.(type) {
case *fspb.Precondition_Exists:
- pc = Exists(fp.Exists)
+ pc = exists(fp.Exists)
case *fspb.Precondition_UpdateTime:
tm, err := ptypes.Timestamp(fp.UpdateTime)
if err != nil {
diff --git a/firestore/docref.go b/firestore/docref.go
index 9da2163..2e48deb 100644
--- a/firestore/docref.go
+++ b/firestore/docref.go
@@ -107,7 +107,7 @@
// the field has the zero value, the server will populate the stored document with
// the time that the request is processed.
func (d *DocumentRef) Create(ctx context.Context, data interface{}) (*WriteResult, error) {
- ws, err := d.newReplaceWrites(data, nil, Exists(false))
+ ws, err := d.newReplaceWrites(data, nil, exists(false))
if err != nil {
return nil, err
}
diff --git a/firestore/options.go b/firestore/options.go
index 089643d..874253b 100644
--- a/firestore/options.go
+++ b/firestore/options.go
@@ -30,9 +30,14 @@
preconditionProto() (*pb.Precondition, error)
}
-// Exists returns a Precondition that checks for the existence or non-existence
-// of a resource before writing to it. If the check fails, the write does not occur.
-func Exists(b bool) Precondition { return exists(b) }
+// Exists is a Precondition that checks for the existence of a resource before
+// writing to it. If the check fails, the write does not occur.
+var Exists Precondition
+
+func init() {
+ // Initialize here so godoc doesn't show the internal value.
+ Exists = exists(true)
+}
type exists bool
@@ -42,7 +47,13 @@
}, nil
}
-func (e exists) String() string { return fmt.Sprintf("Exists(%t)", e) }
+func (e exists) String() string {
+ if e {
+ return "Exists"
+ } else {
+ return "DoesNotExist"
+ }
+}
// LastUpdateTime returns a Precondition that checks that a resource must exist and
// must have last been updated at the given time. If the check fails, the write
diff --git a/firestore/options_test.go b/firestore/options_test.go
index 1b05517..211557b 100644
--- a/firestore/options_test.go
+++ b/firestore/options_test.go
@@ -35,7 +35,7 @@
want: nil,
},
{
- in: []Precondition{Exists(true)},
+ in: []Precondition{Exists},
want: &pb.Precondition{&pb.Precondition_Exists{true}},
},
{
@@ -43,11 +43,11 @@
want: &pb.Precondition{&pb.Precondition_UpdateTime{aTimestamp}},
},
{
- in: []Precondition{Exists(true), LastUpdateTime(aTime)},
+ in: []Precondition{Exists, LastUpdateTime(aTime)},
wantErr: true,
},
{
- in: []Precondition{Exists(true), Exists(true)},
+ in: []Precondition{Exists, Exists},
wantErr: true,
},
} {
@@ -78,7 +78,7 @@
want: nil,
},
{
- in: []Precondition{Exists(true)},
+ in: []Precondition{Exists},
want: &pb.Precondition{&pb.Precondition_Exists{true}},
},
{
@@ -86,11 +86,11 @@
want: &pb.Precondition{&pb.Precondition_UpdateTime{aTimestamp}},
},
{
- in: []Precondition{Exists(true), LastUpdateTime(aTime)},
+ in: []Precondition{Exists, LastUpdateTime(aTime)},
wantErr: true,
},
{
- in: []Precondition{Exists(true), Exists(true)},
+ in: []Precondition{Exists, Exists},
wantErr: true,
},
} {
@@ -122,11 +122,7 @@
},
{
- in: []Precondition{Exists(true)},
- wantErr: true,
- },
- {
- in: []Precondition{Exists(false)},
+ in: []Precondition{Exists},
wantErr: true,
},
{
@@ -134,11 +130,11 @@
want: &pb.Precondition{&pb.Precondition_UpdateTime{aTimestamp}},
},
{
- in: []Precondition{Exists(true), LastUpdateTime(aTime)},
+ in: []Precondition{Exists, LastUpdateTime(aTime)},
wantErr: true,
},
{
- in: []Precondition{Exists(true), Exists(true)},
+ in: []Precondition{Exists, Exists},
wantErr: true,
},
} {
diff --git a/firestore/transaction.go b/firestore/transaction.go
index 505d2d2..a5380f9 100644
--- a/firestore/transaction.go
+++ b/firestore/transaction.go
@@ -234,7 +234,7 @@
// Create adds a Create operation to the Transaction.
// See DocumentRef.Create for details.
func (t *Transaction) Create(dr *DocumentRef, data interface{}) error {
- return t.addWrites(dr.newReplaceWrites(data, nil, Exists(false)))
+ return t.addWrites(dr.newReplaceWrites(data, nil, exists(false)))
}
// Set adds a Set operation to the Transaction.
diff --git a/firestore/writebatch.go b/firestore/writebatch.go
index 7c2363c..beeaf04 100644
--- a/firestore/writebatch.go
+++ b/firestore/writebatch.go
@@ -47,7 +47,7 @@
// Create adds a Create operation to the batch.
// See DocumentRef.Create for details.
func (b *WriteBatch) Create(dr *DocumentRef, data interface{}) *WriteBatch {
- return b.add(dr.newReplaceWrites(data, nil, Exists(false)))
+ return b.add(dr.newReplaceWrites(data, nil, exists(false)))
}
// Set adds a Set operation to the batch.