From f9b6066dd6be7212c5857db8b582c00dad7e5c82 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Mon, 20 Mar 2017 20:34:16 -0700 Subject: [PATCH] clientv3: make ops and compares non-opaque and mutable Fixes #7250 --- clientv3/compare.go | 17 +++++++++++++++++ clientv3/op.go | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/clientv3/compare.go b/clientv3/compare.go index f89ffb52c4a..c55228cc0b8 100644 --- a/clientv3/compare.go +++ b/clientv3/compare.go @@ -82,6 +82,23 @@ func ModRevision(key string) Cmp { return Cmp{Key: []byte(key), Target: pb.Compare_MOD} } +// KeyBytes returns the byte slice holding with the comparison key. +func (cmp *Cmp) KeyBytes() []byte { return cmp.Key } + +// WithKeyBytes sets the byte slice for the comparison key. +func (cmp *Cmp) WithKeyBytes(key []byte) { cmp.Key = key } + +// ValueBytes returns the byte slice holding the comparison value, if any. +func (cmp *Cmp) ValueBytes() []byte { + if tu, ok := cmp.TargetUnion.(*pb.Compare_Value); ok { + return tu.Value + } + return nil +} + +// WithValueBytes sets the byte slice for the comparison's value. +func (cmp *Cmp) WithValueBytes(v []byte) { cmp.TargetUnion.(*pb.Compare_Value).Value = v } + func mustInt64(val interface{}) int64 { if v, ok := val.(int64); ok { return v diff --git a/clientv3/op.go b/clientv3/op.go index 9f73c50bb4d..e8218924ba3 100644 --- a/clientv3/op.go +++ b/clientv3/op.go @@ -69,6 +69,26 @@ type Op struct { leaseID LeaseID } +// accesors / mutators + +// KeyBytes returns the byte slice holding the Op's key. +func (op Op) KeyBytes() []byte { return op.key } + +// WithKeyBytes sets the byte slice for the Op's key. +func (op *Op) WithKeyBytes(key []byte) { op.key = key } + +// RangeBytes returns the byte slice holding with the Op's range end, if any. +func (op Op) RangeBytes() []byte { return op.end } + +// WithRangeBytes sets the byte slice for the Op's range end. +func (op *Op) WithRangeBytes(end []byte) { op.end = end } + +// ValueBytes returns the byte slice holding the Op's value, if any. +func (op Op) ValueBytes() []byte { return op.val } + +// WithValueBytes sets the byte slice for the Op's value. +func (op *Op) WithValueBytes(v []byte) { op.val = v } + func (op Op) toRangeRequest() *pb.RangeRequest { if op.t != tRange { panic("op.t != tRange")