浏览代码

Merge pull request #2017 from xiang90/pbutil

pbutil: move getbool to pbutil
Xiang Li 11 年之前
父节点
当前提交
2c21ac656b
共有 6 个文件被更改,包括 57 次插入38 次删除
  1. 2 0
      etcdserver/cluster_test.go
  2. 1 8
      etcdserver/server.go
  3. 4 29
      etcdserver/server_test.go
  4. 9 0
      pkg/pbutil/pbutil.go
  5. 40 0
      pkg/pbutil/pbutil_test.go
  6. 1 1
      test

+ 2 - 0
etcdserver/cluster_test.go

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

+ 1 - 8
etcdserver/server.go

@@ -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
-}

+ 4 - 29
etcdserver/server_test.go

@@ -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{}

+ 9 - 0
pkg/pbutil/pbutil.go

@@ -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 - 0
pkg/pbutil/pbutil_test.go

@@ -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)
+		}
+	}
+}

+ 1 - 1
test

@@ -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