Explorar el Código

codecgen: handle encodenil if slice/map value is nil

Ugorji Nwoke hace 6 años
padre
commit
d138090090
Se han modificado 1 ficheros con 2 adiciones y 0 borrados
  1. 2 0
      codec/gen.go

+ 2 - 0
codec/gen.go

@@ -1176,6 +1176,7 @@ func (x *genRunner) encStruct(varname string, rtid uintptr, t reflect.Type) {
 }
 
 func (x *genRunner) encListFallback(varname string, t reflect.Type) {
+	x.linef("if %s == nil { r.EncodeNil(); return }", varname)
 	elemBytes := t.Elem().Kind() == reflect.Uint8
 	if t.AssignableTo(uint8SliceTyp) {
 		x.linef("r.EncodeStringBytesRaw([]byte(%s))", varname)
@@ -1222,6 +1223,7 @@ func (x *genRunner) encListFallback(varname string, t reflect.Type) {
 }
 
 func (x *genRunner) encMapFallback(varname string, t reflect.Type) {
+	x.linef("if %s == nil { r.EncodeNil(); return }", varname)
 	// TODO: expand this to handle canonical.
 	i := x.varsfx()
 	x.line("z.EncWriteMapStart(len(" + varname + "))")