Browse Source

codecn: binc: builtin's expect the value (not a pointer to it).

Ugorji Nwoke 8 years ago
parent
commit
f96c3ef853
2 changed files with 14 additions and 18 deletions
  1. 11 15
      codec/binc.go
  2. 3 3
      codec/gen.go

+ 11 - 15
codec/binc.go

@@ -65,24 +65,18 @@ type bincEncDriver struct {
 	encDriverNoopContainerWriter
 	encDriverNoopContainerWriter
 }
 }
 
 
-func (e *bincEncDriver) IsBuiltinType(rt uintptr) bool {
-	return rt == timeTypId
-}
+// func (e *bincEncDriver) IsBuiltinType(rt uintptr) bool {
+// 	return rt == timeTypId
+// }
 
 
 func (e *bincEncDriver) EncodeBuiltin(rt uintptr, v interface{}) {
 func (e *bincEncDriver) EncodeBuiltin(rt uintptr, v interface{}) {
 	if rt == timeTypId {
 	if rt == timeTypId {
-		var bs []byte
-		switch x := v.(type) {
-		case time.Time:
-			bs = encodeTime(x)
-		case *time.Time:
-			bs = encodeTime(*x)
-		default:
-			e.e.errorf("binc error encoding builtin: expect time.Time, received %T", v)
-		}
+		bs := encodeTime(v.(time.Time))
 		e.w.writen1(bincVdTimestamp<<4 | uint8(len(bs)))
 		e.w.writen1(bincVdTimestamp<<4 | uint8(len(bs)))
 		e.w.writeb(bs)
 		e.w.writeb(bs)
+		return
 	}
 	}
+	e.e.errorf("binc error encoding builtin: expect time.Time, received %T", v)
 }
 }
 
 
 func (e *bincEncDriver) EncodeNil() {
 func (e *bincEncDriver) EncodeNil() {
@@ -388,9 +382,9 @@ func (d *bincDecDriver) TryDecodeAsNil() bool {
 	return false
 	return false
 }
 }
 
 
-func (d *bincDecDriver) IsBuiltinType(rt uintptr) bool {
-	return rt == timeTypId
-}
+// func (d *bincDecDriver) IsBuiltinType(rt uintptr) bool {
+// 	return rt == timeTypId
+// }
 
 
 func (d *bincDecDriver) DecodeBuiltin(rt uintptr, v interface{}) {
 func (d *bincDecDriver) DecodeBuiltin(rt uintptr, v interface{}) {
 	if !d.bdRead {
 	if !d.bdRead {
@@ -408,7 +402,9 @@ func (d *bincDecDriver) DecodeBuiltin(rt uintptr, v interface{}) {
 		var vt *time.Time = v.(*time.Time)
 		var vt *time.Time = v.(*time.Time)
 		*vt = tt
 		*vt = tt
 		d.bdRead = false
 		d.bdRead = false
+		return
 	}
 	}
+	d.d.errorf("binc error decoding builtin: expect *time.Time, received %T", v)
 }
 }
 
 
 func (d *bincDecDriver) decFloatPre(vs, defaultLen byte) {
 func (d *bincDecDriver) decFloatPre(vs, defaultLen byte) {

+ 3 - 3
codec/gen.go

@@ -701,11 +701,11 @@ func (x *genRunner) enc(varname string, t reflect.Type) {
 	defer func() { x.line("}") }() //end if block
 	defer func() { x.line("}") }() //end if block
 
 
 	if t == rawTyp {
 	if t == rawTyp {
-		x.linef("} else { z.EncRaw(%v)", varname)
+		x.linef("} else { z.EncRaw(%s)", varname)
 		return
 		return
 	}
 	}
 	if t == rawExtTyp {
 	if t == rawExtTyp {
-		x.linef("} else { r.EncodeRawExt(%v, e)", varname)
+		x.linef("} else { r.EncodeRawExt(%s, e)", varname)
 		return
 		return
 	}
 	}
 	// HACK: Support for Builtins.
 	// HACK: Support for Builtins.
@@ -714,7 +714,7 @@ func (x *genRunner) enc(varname string, t reflect.Type) {
 	if t == timeTyp {
 	if t == timeTyp {
 		vrtid := genTempVarPfx + "m" + x.varsfx()
 		vrtid := genTempVarPfx + "m" + x.varsfx()
 		x.linef("} else if %s := z.TimeRtidIfBinc(); %s != 0 { ", vrtid, vrtid)
 		x.linef("} else if %s := z.TimeRtidIfBinc(); %s != 0 { ", vrtid, vrtid)
-		x.linef("r.EncodeBuiltin(%s, %s)", vrtid, varname)
+		x.linef("r.EncodeBuiltin(%s, *%s)", vrtid, varname)
 	}
 	}
 	// only check for extensions if the type is named, and has a packagePath.
 	// only check for extensions if the type is named, and has a packagePath.
 	if !x.nx && genImportPath(t) != "" && t.Name() != "" {
 	if !x.nx && genImportPath(t) != "" && t.Name() != "" {