Browse Source

codec: msgpack: Honor RawToString when decoding raw bytes into empty interface{}

RawToString says that we can see raw bytes in a stream,
and decode it as string (as opposed to bytes).

There was a situation where we didn't honor that.
When we saw raw bytes but we were using the old spec,
we blindly said that it had to be []byte. However, the
RawToString is there to configure that behaviour.

I need to make time to do a comprehensive set of tests on
RawToString and StringToRaw. That is coming.

Fixes #293
Ugorji Nwoke 6 năm trước cách đây
mục cha
commit
d09a80c1e0
1 tập tin đã thay đổi với 7 bổ sung3 xóa
  1. 7 3
      codec/msgpack.go

+ 7 - 3
codec/msgpack.go

@@ -384,7 +384,11 @@ func (e *msgpackEncDriver) EncodeString(c charEncoding, s string) {
 
 func (e *msgpackEncDriver) EncodeStringEnc(c charEncoding, s string) {
 	slen := len(s)
-	e.writeContainerLen(msgpackContainerStr, slen)
+	if e.h.WriteExt {
+		e.writeContainerLen(msgpackContainerStr, slen)
+	} else {
+		e.writeContainerLen(msgpackContainerRawLegacy, slen)
+	}
 	if slen > 0 {
 		e.w.writestr(s)
 	}
@@ -521,7 +525,7 @@ func (d *msgpackDecDriver) DecodeNaked() {
 			n.v = valueTypeInt
 			n.i = int64(int8(bd))
 		case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
-			if d.h.WriteExt {
+			if d.h.WriteExt || d.h.RawToString {
 				n.v = valueTypeString
 				n.s = d.DecodeString()
 			} else {
@@ -767,7 +771,7 @@ func (d *msgpackDecDriver) ContainerType() (vt valueType) {
 	} else if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 {
 		return valueTypeBytes
 	} else if bd == mpStr8 || bd == mpStr16 || bd == mpStr32 || (bd >= mpFixStrMin && bd <= mpFixStrMax) {
-		if d.h.WriteExt { // UTF-8 string (new spec)
+		if d.h.WriteExt || d.h.RawToString { // UTF-8 string (new spec)
 			return valueTypeString
 		}
 		return valueTypeBytes // raw (old spec)