goprotobuf: Introduce new proto.Message type.
Every generated protocol buffer type now implements the proto.Message interface,
which means we can add some compile-time type safety throughout the API
as well as drop a bunch of error cases.
R=r, rsc
CC=golang-dev
http://codereview.appspot.com/6298073
diff --git a/proto/lib.go b/proto/lib.go
index 50f694e..86800e2 100644
--- a/proto/lib.go
+++ b/proto/lib.go
@@ -172,6 +172,13 @@
"sync"
)
+// Message is implemented by generated protocol buffer messages.
+type Message interface {
+ Reset()
+ String() string
+ ProtoMessage()
+}
+
// Stats records allocation details about the protocol buffer encoders
// and decoders. Useful for tuning the library itself.
type Stats struct {
@@ -183,6 +190,9 @@
Cmiss uint64 // number of cache misses
}
+// Set to true to enable stats collection.
+const collectStats = false
+
var stats Stats
// GetStats returns a copy of the global Stats structure.
@@ -545,12 +555,8 @@
// SetDefaults sets unset protocol buffer fields to their default values.
// It only modifies fields that are both unset and have defined defaults.
// It recursively sets default values in any non-nil sub-messages.
-func SetDefaults(pb interface{}) {
- v := reflect.ValueOf(pb)
- if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
- log.Printf("proto: hit non-pointer-to-struct %v", v)
- }
- setDefaults(v, true, false)
+func SetDefaults(pb Message) {
+ setDefaults(reflect.ValueOf(pb), true, false)
}
// v is a pointer to a struct.