2016-05-13 06:50:58 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
2016-01-28 06:46:41 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package clientv3
|
|
|
|
|
2016-01-29 00:58:12 +03:00
|
|
|
import (
|
|
|
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CompareTarget int
|
|
|
|
type CompareResult int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CompareVersion CompareTarget = iota
|
|
|
|
CompareCreated
|
|
|
|
CompareModified
|
|
|
|
CompareValue
|
|
|
|
)
|
|
|
|
|
|
|
|
type Cmp pb.Compare
|
|
|
|
|
2016-02-09 00:35:59 +03:00
|
|
|
func Compare(cmp Cmp, result string, v interface{}) Cmp {
|
2016-01-29 00:58:12 +03:00
|
|
|
var r pb.Compare_CompareResult
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case "=":
|
|
|
|
r = pb.Compare_EQUAL
|
|
|
|
case ">":
|
|
|
|
r = pb.Compare_GREATER
|
|
|
|
case "<":
|
|
|
|
r = pb.Compare_LESS
|
|
|
|
default:
|
|
|
|
panic("Unknown result op")
|
|
|
|
}
|
|
|
|
|
2016-02-09 00:35:59 +03:00
|
|
|
cmp.Result = r
|
|
|
|
switch cmp.Target {
|
2016-01-29 00:58:12 +03:00
|
|
|
case pb.Compare_VALUE:
|
|
|
|
val, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
panic("bad compare value")
|
|
|
|
}
|
2016-02-09 00:35:59 +03:00
|
|
|
cmp.TargetUnion = &pb.Compare_Value{Value: []byte(val)}
|
2016-01-29 00:58:12 +03:00
|
|
|
case pb.Compare_VERSION:
|
2016-02-09 00:35:59 +03:00
|
|
|
cmp.TargetUnion = &pb.Compare_Version{Version: mustInt64(v)}
|
2016-01-29 00:58:12 +03:00
|
|
|
case pb.Compare_CREATE:
|
2016-02-09 00:35:59 +03:00
|
|
|
cmp.TargetUnion = &pb.Compare_CreateRevision{CreateRevision: mustInt64(v)}
|
2016-01-29 00:58:12 +03:00
|
|
|
case pb.Compare_MOD:
|
2016-02-09 00:35:59 +03:00
|
|
|
cmp.TargetUnion = &pb.Compare_ModRevision{ModRevision: mustInt64(v)}
|
2016-01-29 00:58:12 +03:00
|
|
|
default:
|
|
|
|
panic("Unknown compare type")
|
|
|
|
}
|
2016-02-09 00:35:59 +03:00
|
|
|
return cmp
|
2016-01-29 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 00:35:59 +03:00
|
|
|
func Value(key string) Cmp {
|
|
|
|
return Cmp{Key: []byte(key), Target: pb.Compare_VALUE}
|
2016-01-29 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 00:35:59 +03:00
|
|
|
func Version(key string) Cmp {
|
|
|
|
return Cmp{Key: []byte(key), Target: pb.Compare_VERSION}
|
2016-01-29 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:40:16 +03:00
|
|
|
func CreateRevision(key string) Cmp {
|
2016-02-09 00:35:59 +03:00
|
|
|
return Cmp{Key: []byte(key), Target: pb.Compare_CREATE}
|
2016-01-29 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
2016-03-23 00:58:11 +03:00
|
|
|
func ModRevision(key string) Cmp {
|
2016-02-09 00:35:59 +03:00
|
|
|
return Cmp{Key: []byte(key), Target: pb.Compare_MOD}
|
2016-01-29 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func mustInt64(val interface{}) int64 {
|
|
|
|
if v, ok := val.(int64); ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
if v, ok := val.(int); ok {
|
|
|
|
return int64(v)
|
|
|
|
}
|
|
|
|
panic("bad value")
|
2016-01-28 06:46:41 +03:00
|
|
|
}
|