From 971ea447b7c9cf1f6c460342d86ed988787970b1 Mon Sep 17 00:00:00 2001 From: noot Date: Tue, 27 Apr 2021 16:26:47 -0400 Subject: [PATCH] address comments --- lib/scale/encode.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/scale/encode.go b/lib/scale/encode.go index d2a69506a4..fd54ef0332 100644 --- a/lib/scale/encode.go +++ b/lib/scale/encode.go @@ -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