Просмотр исходного кода

goprotobuf: Equal and String support for RawMessage.

R=r
CC=golang-dev
http://codereview.appspot.com/5889043
David Symonds 14 лет назад
Родитель
Сommit
e9e7aafdeb
2 измененных файлов с 31 добавлено и 1 удалено
  1. 10 1
      proto/equal.go
  2. 21 0
      proto/text.go

+ 10 - 1
proto/equal.go

@@ -30,7 +30,7 @@
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 
 // Protocol buffer comparison.
 // Protocol buffer comparison.
-// TODO: MessageSet and RawMessage.
+// TODO: MessageSet.
 
 
 package proto
 package proto
 
 
@@ -95,6 +95,15 @@ func equalStruct(v1, v2 reflect.Value) bool {
 				// set/unset mismatch
 				// set/unset mismatch
 				return false
 				return false
 			}
 			}
+			b1, ok := f1.Interface().(raw)
+			if ok {
+				b2 := f2.Interface().(raw)
+				// RawMessage
+				if !bytes.Equal(b1.Bytes(), b2.Bytes()) {
+					return false
+				}
+				continue
+			}
 			f1, f2 = f1.Elem(), f2.Elem()
 			f1, f2 = f1.Elem(), f2.Elem()
 		}
 		}
 		if !equalAny(f1, f2) {
 		if !equalAny(f1, f2) {

+ 21 - 0
proto/text.go

@@ -109,6 +109,11 @@ var (
 	extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem()
 	extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem()
 )
 )
 
 
+// raw is the interface satisfied by RawMessage.
+type raw interface {
+	Bytes() []byte
+}
+
 func writeStruct(w *textWriter, sv reflect.Value) {
 func writeStruct(w *textWriter, sv reflect.Value) {
 	if sv.Type() == messageSetType {
 	if sv.Type() == messageSetType {
 		writeMessageSet(w, sv.Addr().Interface().(*MessageSet))
 		writeMessageSet(w, sv.Addr().Interface().(*MessageSet))
@@ -159,6 +164,10 @@ func writeStruct(w *textWriter, sv reflect.Value) {
 		if !w.compact {
 		if !w.compact {
 			w.WriteByte(' ')
 			w.WriteByte(' ')
 		}
 		}
+		if b, ok := fv.Interface().(raw); ok {
+			writeRaw(w, b.Bytes())
+			continue
+		}
 		if props.Enum != "" && tryWriteEnum(w, props.Enum, fv) {
 		if props.Enum != "" && tryWriteEnum(w, props.Enum, fv) {
 			// Enum written.
 			// Enum written.
 		} else {
 		} else {
@@ -174,6 +183,18 @@ func writeStruct(w *textWriter, sv reflect.Value) {
 	}
 	}
 }
 }
 
 
+// writeRaw writes an uninterpreted raw message.
+func writeRaw(w *textWriter, b []byte) {
+	w.WriteByte('<')
+	if !w.compact {
+		w.WriteByte('\n')
+	}
+	w.indent()
+	writeUnknownStruct(w, b)
+	w.unindent()
+	w.WriteByte('>')
+}
+
 // tryWriteEnum attempts to write an enum value as a symbolic constant.
 // tryWriteEnum attempts to write an enum value as a symbolic constant.
 // If the enum is unregistered, nothing is written and false is returned.
 // If the enum is unregistered, nothing is written and false is returned.
 func tryWriteEnum(w *textWriter, enum string, v reflect.Value) bool {
 func tryWriteEnum(w *textWriter, enum string, v reflect.Value) bool {