fix typos and removed useless benchmark

This commit is contained in:
Dan Laine 2020-06-15 10:55:09 -04:00
parent 979477d68f
commit a84abacea5
2 changed files with 20 additions and 29 deletions

View File

@ -234,102 +234,102 @@ func (c *codec) unmarshal(p *wrappers.Packer, value reflect.Value) error {
case reflect.Uint8:
value.SetUint(uint64(p.UnpackByte()))
if p.Err != nil {
return fmt.Errorf("couldn't marshal uint8: %s", p.Err)
return fmt.Errorf("couldn't unmarshal uint8: %s", p.Err)
}
return nil
case reflect.Int8:
value.SetInt(int64(p.UnpackByte()))
if p.Err != nil {
return fmt.Errorf("couldn't marshal int8: %s", p.Err)
return fmt.Errorf("couldn't unmarshal int8: %s", p.Err)
}
return nil
case reflect.Uint16:
value.SetUint(uint64(p.UnpackShort()))
if p.Err != nil {
return fmt.Errorf("couldn't marshal uint16: %s", p.Err)
return fmt.Errorf("couldn't unmarshal uint16: %s", p.Err)
}
return nil
case reflect.Int16:
value.SetInt(int64(p.UnpackShort()))
if p.Err != nil {
return fmt.Errorf("couldn't marshal int16: %s", p.Err)
return fmt.Errorf("couldn't unmarshal int16: %s", p.Err)
}
return nil
case reflect.Uint32:
value.SetUint(uint64(p.UnpackInt()))
if p.Err != nil {
return fmt.Errorf("couldn't marshal uint32: %s", p.Err)
return fmt.Errorf("couldn't unmarshal uint32: %s", p.Err)
}
return nil
case reflect.Int32:
value.SetInt(int64(p.UnpackInt()))
if p.Err != nil {
return fmt.Errorf("couldn't marshal int32: %s", p.Err)
return fmt.Errorf("couldn't unmarshal int32: %s", p.Err)
}
return nil
case reflect.Uint64:
value.SetUint(uint64(p.UnpackLong()))
if p.Err != nil {
return fmt.Errorf("couldn't marshal uint64: %s", p.Err)
return fmt.Errorf("couldn't unmarshal uint64: %s", p.Err)
}
return nil
case reflect.Int64:
value.SetInt(int64(p.UnpackLong()))
if p.Err != nil {
return fmt.Errorf("couldn't marshal int64: %s", p.Err)
return fmt.Errorf("couldn't unmarshal int64: %s", p.Err)
}
return nil
case reflect.Bool:
value.SetBool(p.UnpackBool())
if p.Err != nil {
return fmt.Errorf("couldn't marshal bool: %s", p.Err)
return fmt.Errorf("couldn't unmarshal bool: %s", p.Err)
}
return nil
case reflect.Slice:
numElts := int(p.UnpackInt())
if p.Err != nil {
return fmt.Errorf("couldn't marshal slice: %s", p.Err)
return fmt.Errorf("couldn't unmarshal slice: %s", p.Err)
}
// set [value] to be a slice of the appropriate type/capacity (right now it is nil)
value.Set(reflect.MakeSlice(value.Type(), numElts, numElts))
// Unmarshal each element into the appropriate index of the slice
for i := 0; i < numElts; i++ {
if err := c.unmarshal(p, value.Index(i)); err != nil {
return fmt.Errorf("couldn't marshal slice element: %s", err)
return fmt.Errorf("couldn't unmarshal slice element: %s", err)
}
}
return nil
case reflect.Array:
for i := 0; i < value.Len(); i++ {
if err := c.unmarshal(p, value.Index(i)); err != nil {
return fmt.Errorf("couldn't marshal array element: %s", err)
return fmt.Errorf("couldn't unmarshal array element: %s", err)
}
}
return nil
case reflect.String:
value.SetString(p.UnpackStr())
if p.Err != nil {
return fmt.Errorf("couldn't marshal string: %s", p.Err)
return fmt.Errorf("couldn't unmarshal string: %s", p.Err)
}
return nil
case reflect.Interface:
typeID := p.UnpackInt() // Get the type ID
if p.Err != nil {
return fmt.Errorf("couldn't marshal interface: %s", p.Err)
return fmt.Errorf("couldn't unmarshal interface: %s", p.Err)
}
// Get a type that implements the interface
implementingType, ok := c.typeIDToType[typeID]
if !ok {
return fmt.Errorf("couldn't marshal interface: unknown type ID %d", typeID)
return fmt.Errorf("couldn't unmarshal interface: unknown type ID %d", typeID)
}
// Ensure type actually does implement the interface
if valueType := value.Type(); !implementingType.Implements(valueType) {
return fmt.Errorf("couldn't marshal interface: %s does not implement interface %s", implementingType, valueType)
return fmt.Errorf("couldn't unmarshal interface: %s does not implement interface %s", implementingType, valueType)
}
intfImplementor := reflect.New(implementingType).Elem() // instance of the proper type
// Unmarshal into the struct
if err := c.unmarshal(p, intfImplementor); err != nil {
return fmt.Errorf("couldn't marshal interface: %s", err)
return fmt.Errorf("couldn't unmarshal interface: %s", err)
}
// And assign the filled struct to the value
value.Set(intfImplementor)
@ -338,12 +338,12 @@ func (c *codec) unmarshal(p *wrappers.Packer, value reflect.Value) error {
// Get indices of fields that will be unmarshaled into
serializedFieldIndices, err := c.getSerializedFieldIndices(value.Type())
if err != nil {
return fmt.Errorf("couldn't marshal struct: %s", err)
return fmt.Errorf("couldn't unmarshal struct: %s", err)
}
// Go through the fields and umarshal into them
for _, index := range serializedFieldIndices {
if err := c.unmarshal(p, value.Field(index)); err != nil {
return fmt.Errorf("couldn't marshal struct: %s", err)
return fmt.Errorf("couldn't unmarshal struct: %s", err)
}
}
return nil
@ -354,7 +354,7 @@ func (c *codec) unmarshal(p *wrappers.Packer, value reflect.Value) error {
v := reflect.New(t)
// Fill the value
if err := c.unmarshal(p, v.Elem()); err != nil {
return fmt.Errorf("couldn't marshal pointer: %s", err)
return fmt.Errorf("couldn't unmarshal pointer: %s", err)
}
// Assign to the top-level struct's member
value.Set(v)

View File

@ -62,12 +62,3 @@ func BenchmarkMarshalNonCodec(b *testing.B) {
}
}
}
func BenchmarkFoo(b *testing.B) {
arr := make([]int, 10000, 10000)
for n := 0; n < b.N; n++ {
for i := 0; i < 10000; i++ {
arr[i] = i
}
}
}