Browse Source

clientv3: compare helper functions to set range/prefix

Anthony Romano 8 years ago
parent
commit
fcf1abd23b
1 changed files with 12 additions and 0 deletions
  1. 12 0
      clientv3/compare.go

+ 12 - 0
clientv3/compare.go

@@ -99,6 +99,18 @@ func (cmp *Cmp) ValueBytes() []byte {
 // WithValueBytes sets the byte slice for the comparison's value.
 // WithValueBytes sets the byte slice for the comparison's value.
 func (cmp *Cmp) WithValueBytes(v []byte) { cmp.TargetUnion.(*pb.Compare_Value).Value = v }
 func (cmp *Cmp) WithValueBytes(v []byte) { cmp.TargetUnion.(*pb.Compare_Value).Value = v }
 
 
+// WithRange sets the comparison to scan the range [key, end).
+func (cmp Cmp) WithRange(end string) Cmp {
+	cmp.RangeEnd = []byte(end)
+	return cmp
+}
+
+// WithPrefix sets the comparison to scan all keys prefixed by the key.
+func (cmp Cmp) WithPrefix() Cmp {
+	cmp.RangeEnd = getPrefix(cmp.Key)
+	return cmp
+}
+
 func mustInt64(val interface{}) int64 {
 func mustInt64(val interface{}) int64 {
 	if v, ok := val.(int64); ok {
 	if v, ok := val.(int64); ok {
 		return v
 		return v