Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Apr 27, 2021
1 parent 3d3f054 commit 971ea44
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/scale/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,24 @@ func (se *Encoder) EncodeCustom(in interface{}) (int, error) {
someType := reflect.TypeOf(in)
// TODO: if not a pointer, check if type pointer has Encode method
_, ok := someType.MethodByName("Encode")
if ok {
res := reflect.ValueOf(in).MethodByName("Encode").Call([]reflect.Value{})
val := res[0].Interface()
if len(res) < 2 {
return se.Writer.Write(val.([]byte))
}
if !ok {
return 0, fmt.Errorf("cannot call EncodeCustom")
}

err := res[1].Interface()
if err != nil {
return 0, err.(error)
}
res := reflect.ValueOf(in).MethodByName("Encode").Call([]reflect.Value{})
if len(res) == 0 {
return 0, fmt.Errorf("method Encode does not have any return values")
}
val := res[0].Interface()
if len(res) < 2 {
return se.Writer.Write(val.([]byte))
}
return 0, fmt.Errorf("cannot call EncodeCustom")

err := res[1].Interface()
if err != nil {
return 0, err.(error)
}
return se.Writer.Write(val.([]byte))
}

// encodeCustomOrEncode tries to use EncodeCustom, if that fails, it reverts to Encode
Expand Down

0 comments on commit 971ea44

Please sign in to comment.