Browse Source

codec: in fast path, encode nil maps or slices as nil values.

Currently, fast-path mode will encode nil maps and slices as zero-length maps or slices.
However, the generic code path (kMap, kSlice) will encode such as nil.
This CL makes the fast-path behaviour consistent with the generic behaviour.

Fixes #37 .
Ugorji Nwoke 11 years ago
parent
commit
b7f3bcbbaa
3 changed files with 373 additions and 1 deletions
  1. 1 1
      codec/decode.go
  2. 364 0
      codec/fast-path.go
  3. 8 0
      codec/gen-fast-path.go

+ 1 - 1
codec/decode.go

@@ -536,7 +536,7 @@ type Decoder struct {
 
 // NewDecoder returns a Decoder for decoding a stream of bytes from an io.Reader.
 //
-// For efficiency, Users are encouraged to pass in a memory buffered writer
+// For efficiency, Users are encouraged to pass in a memory buffered reader
 // (eg bufio.Reader, bytes.Buffer).
 func NewDecoder(r io.Reader, h Handle) *Decoder {
 	z := ioDecReader{

File diff suppressed because it is too large
+ 364 - 0
codec/fast-path.go


+ 8 - 0
codec/gen-fast-path.go

@@ -77,6 +77,10 @@ func init() {
 
 func (f *encFnInfo) {{ .MethodName true }}(rv reflect.Value) {
 	v := rv.Interface().([]{{ .Elem }})
+	if v == nil {
+		f.ee.encodeNil()
+		return
+	}
 	f.ee.encodeArrayPreamble(len(v))
 	for _, v2 := range v {
 		{{ encmd .Elem "v2"}}
@@ -89,6 +93,10 @@ func (f *encFnInfo) {{ .MethodName true }}(rv reflect.Value) {
 
 func (f *encFnInfo) {{ .MethodName true }}(rv reflect.Value) {
 	v := rv.Interface().(map[{{ .MapKey }}]{{ .Elem }})
+	if v == nil {
+		f.ee.encodeNil()
+		return
+	}
 	f.ee.encodeMapPreamble(len(v))
 	{{if eq .MapKey "string"}}asSymbols := f.e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0{{end}}
 	for k2, v2 := range v {

Some files were not shown because too many files changed in this diff