From c312d6efad2629e7da968f5040488bb4cd812e18 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Fri, 22 Aug 2014 17:00:42 -0700 Subject: [PATCH] raft: use Equal in example --- raft2/example_test.go | 3 +-- raft2/state.pb.go | 39 +++++++++++++++++++++++++++++++++++++++ raft2/state.proto | 1 + 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/raft2/example_test.go b/raft2/example_test.go index 91619fc01..71579e4f4 100644 --- a/raft2/example_test.go +++ b/raft2/example_test.go @@ -10,7 +10,6 @@ func applyToStore(ents []Entry) {} func sendMessages(msgs []Message) {} func saveStateToDisk(st State) {} func saveToDisk(ents []Entry) {} -func stateChanged(prev, st State) bool { return false } func Example_Node() { n := Start(context.Background(), "", 0, 0) @@ -26,7 +25,7 @@ func Example_Node() { log.Fatal(err) } - if stateChanged(prev, st) { + if prev.Equal(st) { saveStateToDisk(st) prev = st } diff --git a/raft2/state.pb.go b/raft2/state.pb.go index 4152a7a84..d9c34f9e0 100644 --- a/raft2/state.pb.go +++ b/raft2/state.pb.go @@ -22,6 +22,8 @@ import math "math" import io "io" import code_google_com_p_gogoprotobuf_proto "code.google.com/p/gogoprotobuf/proto" +import bytes "bytes" + // Reference proto, json, and math imports to suppress error if they are not otherwise used. var _ = proto.Marshal var _ = &json.SyntaxError{} @@ -228,3 +230,40 @@ func encodeVarintState(data []byte, offset int, v uint64) int { data[offset] = uint8(v) return offset + 1 } +func (this *State) Equal(that interface{}) bool { + if that == nil { + if this == nil { + return true + } + return false + } + + that1, ok := that.(*State) + if !ok { + return false + } + if that1 == nil { + if this == nil { + return true + } + return false + } else if this == nil { + return false + } + if this.Term != that1.Term { + return false + } + if this.Vote != that1.Vote { + return false + } + if this.Commit != that1.Commit { + return false + } + if this.LastIndex != that1.LastIndex { + return false + } + if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { + return false + } + return true +} diff --git a/raft2/state.proto b/raft2/state.proto index bfdfcab1d..1999b9294 100644 --- a/raft2/state.proto +++ b/raft2/state.proto @@ -6,6 +6,7 @@ option (gogoproto.marshaler_all) = true; option (gogoproto.sizer_all) = true; option (gogoproto.unmarshaler_all) = true; option (gogoproto.goproto_getters_all) = false; +option (gogoproto.equal_all) = true; message State { required int64 term = 1 [(gogoproto.nullable) = false];