Browse Source

Fixed broken tests on pre-go1.8 #356 (#359)

Disable benchmarks using testing.B.Run for pre-go1.7.

Fix unmarshaling Any for pre-1.8.  Unmarshaling code for Any assumed
json.RawMessage value receiver on json.Marshaler interface, but it was a
pointer receiver pre-1.8.
Herbie Ong 8 years ago
parent
commit
63bfc70872
3 changed files with 10 additions and 3 deletions
  1. 6 3
      jsonpb/jsonpb.go
  2. 2 0
      proto/decode_test.go
  3. 2 0
      proto/encode_test.go

+ 6 - 3
jsonpb/jsonpb.go

@@ -624,7 +624,10 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
 			// so we don't have to do any extra work.
 			return u.unmarshalValue(target.Field(0), inputValue, prop)
 		case "Any":
-			var jsonFields map[string]json.RawMessage
+			// Use json.RawMessage pointer type instead of value to support pre-1.8 version.
+			// 1.8 changed RawMessage.MarshalJSON from pointer type to value type, see
+			// https://github.com/golang/go/issues/14493
+			var jsonFields map[string]*json.RawMessage
 			if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
 				return err
 			}
@@ -635,7 +638,7 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
 			}
 
 			var turl string
-			if err := json.Unmarshal([]byte(val), &turl); err != nil {
+			if err := json.Unmarshal([]byte(*val), &turl); err != nil {
 				return fmt.Errorf("can't unmarshal Any's '@type': %q", val)
 			}
 			target.Field(0).SetString(turl)
@@ -656,7 +659,7 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
 					return errors.New("Any JSON doesn't have 'value'")
 				}
 
-				if err := u.unmarshalValue(reflect.ValueOf(m).Elem(), val, nil); err != nil {
+				if err := u.unmarshalValue(reflect.ValueOf(m).Elem(), *val, nil); err != nil {
 					return fmt.Errorf("can't unmarshal Any's WKT: %v", err)
 				}
 			} else {

+ 2 - 0
proto/decode_test.go

@@ -29,6 +29,8 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+// +build go1.7
+
 package proto_test
 
 import (

+ 2 - 0
proto/encode_test.go

@@ -29,6 +29,8 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+// +build go1.7
+
 package proto_test
 
 import (