From 4fef7fcb9022156e41b395fff08045b1e53dedba Mon Sep 17 00:00:00 2001 From: vivekpatani <9080894+vivekpatani@users.noreply.github.com> Date: Tue, 19 Jul 2022 15:57:21 -0700 Subject: [PATCH] clientv3: fix isOptsWithFromKey/isOptsWithPrefix - Addressing: https://github.com/etcd-io/etcd/issues/13332 - Backporting: https://github.com/etcd-io/etcd/pull/13334 Signed-off-by: vivekpatani <9080894+vivekpatani@users.noreply.github.com> --- clientv3/op.go | 27 +++++++++++++++++++++++++-- clientv3/utils.go | 18 ------------------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/clientv3/op.go b/clientv3/op.go index 81ae31fd8..778617d4d 100644 --- a/clientv3/op.go +++ b/clientv3/op.go @@ -77,6 +77,9 @@ type Op struct { cmps []Cmp thenOps []Op elseOps []Op + + isOptsWithFromKey bool + isOptsWithPrefix bool } // accessors / mutators @@ -216,6 +219,10 @@ func (op Op) isWrite() bool { return op.t != tRange } +func NewOp() *Op { + return &Op{key: []byte("")} +} + // OpGet returns "get" operation based on given key and operation options. func OpGet(key string, opts ...OpOption) Op { // WithPrefix and WithFromKey are not supported together @@ -387,6 +394,7 @@ func WithPrefix() OpOption { return } op.end = getPrefix(op.key) + op.isOptsWithPrefix = true } } @@ -406,6 +414,7 @@ func WithFromKey() OpOption { op.key = []byte{0} } op.end = []byte("\x00") + op.isOptsWithFromKey = true } } @@ -554,7 +563,21 @@ func toLeaseTimeToLiveRequest(id LeaseID, opts ...LeaseOption) *pb.LeaseTimeToLi } // isWithPrefix returns true if WithPrefix is being called in the op -func isWithPrefix(opts []OpOption) bool { return isOpFuncCalled("WithPrefix", opts) } +func isWithPrefix(opts []OpOption) bool { + ret := NewOp() + for _, opt := range opts { + opt(ret) + } + + return ret.isOptsWithPrefix +} // isWithFromKey returns true if WithFromKey is being called in the op -func isWithFromKey(opts []OpOption) bool { return isOpFuncCalled("WithFromKey", opts) } +func isWithFromKey(opts []OpOption) bool { + ret := NewOp() + for _, opt := range opts { + opt(ret) + } + + return ret.isOptsWithFromKey +} diff --git a/clientv3/utils.go b/clientv3/utils.go index b998c41b9..850275877 100644 --- a/clientv3/utils.go +++ b/clientv3/utils.go @@ -16,9 +16,6 @@ package clientv3 import ( "math/rand" - "reflect" - "runtime" - "strings" "time" ) @@ -32,18 +29,3 @@ func jitterUp(duration time.Duration, jitter float64) time.Duration { multiplier := jitter * (rand.Float64()*2 - 1) return time.Duration(float64(duration) * (1 + multiplier)) } - -// Check if the provided function is being called in the op options. -func isOpFuncCalled(op string, opts []OpOption) bool { - for _, opt := range opts { - v := reflect.ValueOf(opt) - if v.Kind() == reflect.Func { - if opFunc := runtime.FuncForPC(v.Pointer()); opFunc != nil { - if strings.Contains(opFunc.Name(), op) { - return true - } - } - } - } - return false -}