state: serialize string as bytes instead of protobuf string.

Protobuf strings have to be UTF-8 encoded or 7-bit ASCII.

PiperOrigin-RevId: 195902557
Change-Id: I9800afd47ecfa6615e28a2cce7f2532f04f10763
This commit is contained in:
Zhaozhong Ni
2018-05-08 17:23:50 -07:00
committed by Shentubot
parent 10a2cfc6a9
commit ad278d6944
5 changed files with 5 additions and 4 deletions
+1 -1
View File
@@ -335,7 +335,7 @@ func (ds *decodeState) decodeObject(os *objectState, obj reflect.Value, object *
case *pb.Object_BoolValue:
obj.SetBool(x.BoolValue)
case *pb.Object_StringValue:
obj.SetString(x.StringValue)
obj.SetString(string(x.StringValue))
case *pb.Object_Int64Value:
obj.SetInt(x.Int64Value)
if obj.Int() != x.Int64Value {
+1 -1
View File
@@ -308,7 +308,7 @@ func (es *encodeState) encodeObject(obj reflect.Value, mapAsValue bool, format s
}}}
}
case reflect.String:
object = &pb.Object{Value: &pb.Object_StringValue{obj.String()}}
object = &pb.Object{Value: &pb.Object_StringValue{[]byte(obj.String())}}
case reflect.Ptr:
if obj.IsNil() {
// Handled specially in decode; store as a nil value.
+1 -1
View File
@@ -114,7 +114,7 @@ message Float32s {
message Object {
oneof value {
bool bool_value = 1;
string string_value = 2;
bytes string_value = 2;
int64 int64_value = 3;
uint64 uint64_value = 4;
double double_value = 5;
+1 -1
View File
@@ -30,7 +30,7 @@ func format(graph uint64, depth int, object *pb.Object, html bool) (string, bool
case *pb.Object_BoolValue:
return fmt.Sprintf("%t", x.BoolValue), x.BoolValue != false
case *pb.Object_StringValue:
return fmt.Sprintf("\"%s\"", x.StringValue), x.StringValue != ""
return fmt.Sprintf("\"%s\"", string(x.StringValue)), len(x.StringValue) != 0
case *pb.Object_Int64Value:
return fmt.Sprintf("%d", x.Int64Value), x.Int64Value != 0
case *pb.Object_Uint64Value:
+1
View File
@@ -430,6 +430,7 @@ func TestTypes(t *testing.T) {
"",
"foo",
"bar",
"\xa0",
},
},
{