Browse Source

codec: fix MapBySlice encoding

Fast-Path does not support MapBySlice.
Consequently, we should not downgrade to the "underlying" type if the
type is a MapBySlice.

Also, we fixed a mistake where we referenced the
loop maximum threshold instead of the loop variable.

Fixes #124
Ugorji Nwoke 10 years ago
parent
commit
3f41379a4b
1 changed files with 3 additions and 2 deletions
  1. 3 2
      codec/encode.go

+ 3 - 2
codec/encode.go

@@ -473,7 +473,7 @@ func (f *encFnInfo) kSlice(rv reflect.Value) {
 		for j := 0; j < l; j++ {
 		for j := 0; j < l; j++ {
 			if cr != nil {
 			if cr != nil {
 				if ti.mbs {
 				if ti.mbs {
-					if l%2 == 0 {
+					if j%2 == 0 {
 						cr.sendContainerState(containerMapKey)
 						cr.sendContainerState(containerMapKey)
 					} else {
 					} else {
 						cr.sendContainerState(containerMapValue)
 						cr.sendContainerState(containerMapValue)
@@ -1265,10 +1265,11 @@ func (e *Encoder) getEncFn(rtid uintptr, rt reflect.Type, checkFastpath, checkCo
 	} else {
 	} else {
 		rk := rt.Kind()
 		rk := rt.Kind()
 		if fastpathEnabled && checkFastpath && (rk == reflect.Map || rk == reflect.Slice) {
 		if fastpathEnabled && checkFastpath && (rk == reflect.Map || rk == reflect.Slice) {
-			if rt.PkgPath() == "" {
+			if rt.PkgPath() == "" { // un-named slice or map
 				if idx := fastpathAV.index(rtid); idx != -1 {
 				if idx := fastpathAV.index(rtid); idx != -1 {
 					fn.f = fastpathAV[idx].encfn
 					fn.f = fastpathAV[idx].encfn
 				}
 				}
+			} else if ti.mbs { // map by slice. Do not use underlying type AS-IS.
 			} else {
 			} else {
 				ok = false
 				ok = false
 				// use mapping for underlying type if there
 				// use mapping for underlying type if there