pbutil: add getbool to pbutil

release-2.0
Xiang Li 2014-12-30 14:51:26 -08:00
parent 8088440e1d
commit c3d2f5eea0
6 changed files with 57 additions and 38 deletions

View File

@ -654,3 +654,5 @@ func newTestCluster(membs []*Member) *Cluster {
}
return c
}
func stringp(s string) *string { return &s }

View File

@ -714,7 +714,7 @@ func (s *EtcdServer) applyRequest(r pb.Request) Response {
case "POST":
return f(s.store.Create(r.Path, r.Dir, r.Val, true, expr))
case "PUT":
exists, existsSet := getBool(r.PrevExist)
exists, existsSet := pbutil.GetBool(r.PrevExist)
switch {
case existsSet:
if exists {
@ -989,10 +989,3 @@ func parseCtxErr(err error) error {
return err
}
}
func getBool(v *bool) (vv bool, set bool) {
if v == nil {
return false, false
}
return *v, true
}

View File

@ -208,7 +208,7 @@ func TestApplyRequest(t *testing.T) {
},
// PUT with PrevExist=true ==> Update
{
pb.Request{Method: "PUT", ID: 1, PrevExist: boolp(true)},
pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(true)},
Response{Event: &store.Event{}},
[]action{
action{
@ -219,7 +219,7 @@ func TestApplyRequest(t *testing.T) {
},
// PUT with PrevExist=false ==> Create
{
pb.Request{Method: "PUT", ID: 1, PrevExist: boolp(false)},
pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(false)},
Response{Event: &store.Event{}},
[]action{
action{
@ -231,7 +231,7 @@ func TestApplyRequest(t *testing.T) {
// PUT with PrevExist=true *and* PrevIndex set ==> Update
// TODO(jonboulle): is this expected?!
{
pb.Request{Method: "PUT", ID: 1, PrevExist: boolp(true), PrevIndex: 1},
pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(true), PrevIndex: 1},
Response{Event: &store.Event{}},
[]action{
action{
@ -243,7 +243,7 @@ func TestApplyRequest(t *testing.T) {
// PUT with PrevExist=false *and* PrevIndex set ==> Create
// TODO(jonboulle): is this expected?!
{
pb.Request{Method: "PUT", ID: 1, PrevExist: boolp(false), PrevIndex: 1},
pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(false), PrevIndex: 1},
Response{Event: &store.Event{}},
[]action{
action{
@ -1108,31 +1108,6 @@ func TestGetOtherPeerURLs(t *testing.T) {
}
}
func TestGetBool(t *testing.T) {
tests := []struct {
b *bool
wb bool
wset bool
}{
{nil, false, false},
{boolp(true), true, true},
{boolp(false), false, true},
}
for i, tt := range tests {
b, set := getBool(tt.b)
if b != tt.wb {
t.Errorf("#%d: value = %v, want %v", i, b, tt.wb)
}
if set != tt.wset {
t.Errorf("#%d: set = %v, want %v", i, set, tt.wset)
}
}
}
func boolp(b bool) *bool { return &b }
func stringp(s string) *string { return &s }
type action struct {
name string
params []interface{}

View File

@ -39,3 +39,12 @@ func MustUnmarshal(um Unmarshaler, data []byte) {
log.Panicf("unmarshal protobuf type should never fail: %v", err)
}
}
func GetBool(v *bool) (vv bool, set bool) {
if v == nil {
return false, false
}
return *v, true
}
func Boolp(b bool) *bool { return &b }

40
pkg/pbutil/pbutil_test.go Normal file
View File

@ -0,0 +1,40 @@
/*
Copyright 2014 CoreOS, Inc.
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 pbutil
import "testing"
func TestGetBool(t *testing.T) {
tests := []struct {
b *bool
wb bool
wset bool
}{
{nil, false, false},
{Boolp(true), true, true},
{Boolp(false), false, true},
}
for i, tt := range tests {
b, set := GetBool(tt.b)
if b != tt.wb {
t.Errorf("#%d: value = %v, want %v", i, b, tt.wb)
}
if set != tt.wset {
t.Errorf("#%d: set = %v, want %v", i, set, tt.wset)
}
}
}

2
test
View File

@ -15,7 +15,7 @@ COVER=${COVER:-"-cover"}
source ./build
# Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
TESTABLE_AND_FORMATTABLE="client discovery error etcdctl/command etcdmain etcdserver etcdserver/etcdhttp etcdserver/etcdhttp/httptypes etcdserver/etcdserverpb etcdserver/idutil integration migrate pkg/fileutil pkg/flags pkg/ioutils pkg/netutil pkg/types pkg/transport pkg/wait proxy raft rafthttp snap store wal"
TESTABLE_AND_FORMATTABLE="client discovery error etcdctl/command etcdmain etcdserver etcdserver/etcdhttp etcdserver/etcdhttp/httptypes etcdserver/etcdserverpb etcdserver/idutil integration migrate pkg/fileutil pkg/flags pkg/ioutils pkg/netutil pkg/pbutil pkg/types pkg/transport pkg/wait proxy raft rafthttp snap store wal"
FORMATTABLE="$TESTABLE_AND_FORMATTABLE *.go etcdctl/"
# user has not provided PKG override