فهرست منبع

goprotobuf: Further changes after the reflect API changes.

R=r, r2
CC=golang-dev
http://codereview.appspot.com/4434041
David Symonds 15 سال پیش
والد
کامیت
a9cda21316
2فایلهای تغییر یافته به همراه9 افزوده شده و 14 حذف شده
  1. 2 2
      proto/text.go
  2. 7 12
      proto/text_parser.go

+ 2 - 2
proto/text.go

@@ -202,8 +202,8 @@ func marshalText(w io.Writer, pb interface{}, compact bool) {
 	// We should normally be passed a struct, or a pointer to a struct,
 	// and we don't want the outer < and > in that case.
 	v = reflect.Indirect(v)
-	if sv := v; sv.Kind() == reflect.Struct {
-		writeStruct(aw, sv)
+	if v.Kind() == reflect.Struct {
+		writeStruct(aw, v)
 	} else {
 		writeAny(aw, v)
 	}

+ 7 - 12
proto/text_parser.go

@@ -294,14 +294,14 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) *ParseError
 				// those three become *T, *string and []T respectively, so we can check for
 				// this field being a pointer to a non-string.
 				typ := st.Field(fi).Type
-				if pt := typ; pt.Kind() == reflect.Ptr {
+				if typ.Kind() == reflect.Ptr {
 					// *T or *string
-					if pt.Elem().Kind() == reflect.String {
+					if typ.Elem().Kind() == reflect.String {
 						break
 					}
-				} else if st := typ; st.Kind() == reflect.Slice {
+				} else if typ.Kind() == reflect.Slice {
 					// []T or []*T
-					if st.Elem().Kind() != reflect.Ptr {
+					if typ.Elem().Kind() != reflect.Ptr {
 						break
 					}
 				}
@@ -448,16 +448,11 @@ var notPtrStruct os.Error = &ParseError{"destination is not a pointer to a struc
 
 // UnmarshalText reads a protobuffer in Text format.
 func UnmarshalText(s string, pb interface{}) os.Error {
-	pv := reflect.NewValue(pb)
-	ok := pv.Kind() == reflect.Ptr
-	if !ok {
+	v := reflect.NewValue(pb)
+	if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
 		return notPtrStruct
 	}
-	sv := pv.Elem()
-	if sv.Kind() != reflect.Struct {
-		return notPtrStruct
-	}
-	if pe := newTextParser(s).readStruct(sv, ""); pe != nil {
+	if pe := newTextParser(s).readStruct(v.Elem(), ""); pe != nil {
 		return pe
 	}
 	return nil