net/proto2/go: make a slight change to how we handle []byte fields
This is meant to fix https://github.com/golang/protobuf/pull/188. There
are no tests because we don't guarantee that we're going to maintain
this behavior in the future.
Fixes #188
diff --git a/jsonpb/jsonpb.go b/jsonpb/jsonpb.go
index 71985e6..0ad60eb 100644
--- a/jsonpb/jsonpb.go
+++ b/jsonpb/jsonpb.go
@@ -53,10 +53,6 @@
"github.com/golang/protobuf/proto"
)
-var (
- byteArrayType = reflect.TypeOf([]byte{})
-)
-
// Marshaler is a configurable object for converting between
// protocol buffer objects and a JSON representation for them.
type Marshaler struct {
@@ -374,7 +370,7 @@
v = reflect.Indirect(v)
// Handle repeated elements.
- if v.Type() != byteArrayType && v.Kind() == reflect.Slice {
+ if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
out.write("[")
comma := ""
for i := 0; i < v.Len(); i++ {
@@ -684,7 +680,7 @@
}
// Handle arrays (which aren't encoded bytes)
- if targetType != byteArrayType && targetType.Kind() == reflect.Slice {
+ if targetType.Kind() == reflect.Slice && targetType.Elem().Kind() != reflect.Uint8 {
var slc []json.RawMessage
if err := json.Unmarshal(inputValue, &slc); err != nil {
return err
diff --git a/proto/text.go b/proto/text.go
index bd6f1ae..8214ce3 100644
--- a/proto/text.go
+++ b/proto/text.go
@@ -513,7 +513,7 @@
switch v.Kind() {
case reflect.Slice:
// Should only be a []byte; repeated fields are handled in writeStruct.
- if err := writeString(w, string(v.Interface().([]byte))); err != nil {
+ if err := writeString(w, string(v.Bytes())); err != nil {
return err
}
case reflect.String: