Browse Source

codecgen: special-case (en|de)coding where underlying type is []byte or [N]byte

when we generate a method for a type whose underlying type is slice or array of bytes,
we will call the special-case EncodeStringBytes or DecodeBytes respectively.

Fixes #165
Ugorji Nwoke 9 years ago
parent
commit
cfc713ce5b
1 changed files with 16 additions and 0 deletions
  1. 16 0
      codec/gen.go

+ 16 - 0
codec/gen.go

@@ -987,6 +987,14 @@ func (x *genRunner) encStruct(varname string, rtid uintptr, t reflect.Type) {
 }
 }
 
 
 func (x *genRunner) encListFallback(varname string, t reflect.Type) {
 func (x *genRunner) encListFallback(varname string, t reflect.Type) {
+	if t.AssignableTo(uint8SliceTyp) {
+		x.linef("r.EncodeStringBytes(codecSelferC_RAW%s, []byte(%s))", x.xs, varname)
+		return
+	}
+	if t.Kind() == reflect.Array && t.Elem().Kind() == reflect.Uint8 {
+		x.linef("r.EncodeStringBytes(codecSelferC_RAW%s, ([%v]byte(%s))[:])", x.xs, t.Len(), varname)
+		return
+	}
 	i := x.varsfx()
 	i := x.varsfx()
 	g := genTempVarPfx
 	g := genTempVarPfx
 	x.line("r.EncodeArrayStart(len(" + varname + "))")
 	x.line("r.EncodeArrayStart(len(" + varname + "))")
@@ -1306,6 +1314,14 @@ func (x *genRunner) decTryAssignPrimitive(varname string, t reflect.Type) (tryAs
 }
 }
 
 
 func (x *genRunner) decListFallback(varname string, rtid uintptr, t reflect.Type) {
 func (x *genRunner) decListFallback(varname string, rtid uintptr, t reflect.Type) {
+	if t.AssignableTo(uint8SliceTyp) {
+		x.line("*" + varname + " = r.DecodeBytes(*((*[]byte)(" + varname + ")), false, false)")
+		return
+	}
+	if t.Kind() == reflect.Array && t.Elem().Kind() == reflect.Uint8 {
+		x.linef("r.DecodeBytes( ((*[%s]byte)(%s))[:], false, true)", t.Len(), varname)
+		return
+	}
 	type tstruc struct {
 	type tstruc struct {
 		TempVar   string
 		TempVar   string
 		Rand      string
 		Rand      string