From e3b5e9b20ea74c32bed7a5464d0efad00a56973b Mon Sep 17 00:00:00 2001 From: weiihann Date: Fri, 25 Oct 2024 19:03:32 +0800 Subject: [PATCH] put IsNil in utils package --- adapters/p2p2core/felt.go | 4 ++-- jsonrpc/server.go | 8 ++------ utils/check.go | 7 +++++++ 3 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 utils/check.go diff --git a/adapters/p2p2core/felt.go b/adapters/p2p2core/felt.go index cd4b3080b2..a823415c42 100644 --- a/adapters/p2p2core/felt.go +++ b/adapters/p2p2core/felt.go @@ -2,10 +2,10 @@ package p2p2core import ( "encoding/binary" - "reflect" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/p2p/starknet/spec" + "github.com/NethermindEth/juno/utils" "github.com/ethereum/go-ethereum/common" ) @@ -28,7 +28,7 @@ func AdaptFelt(f *spec.Felt252) *felt.Felt { func adapt(v interface{ GetElements() []byte }) *felt.Felt { // when passing a nil pointer `v` is boxed to an interface and is not nil // see: https://blog.devtrovert.com/p/go-secret-interface-nil-is-not-nil - if v == nil || reflect.ValueOf(v).IsNil() { + if utils.IsNil(v) { return nil } diff --git a/jsonrpc/server.go b/jsonrpc/server.go index 888c0d687a..863b1594f5 100644 --- a/jsonrpc/server.go +++ b/jsonrpc/server.go @@ -422,12 +422,8 @@ func isBatch(reader *bufio.Reader) bool { return false } -func isNil(i any) bool { - return i == nil || reflect.ValueOf(i).IsNil() -} - func isNilOrEmpty(i any) (bool, error) { - if isNil(i) { + if utils.IsNil(i) { return true, nil } @@ -484,7 +480,7 @@ func (s *Server) handleRequest(ctx context.Context, req *Request) (*response, ht header = (tuple[1].Interface()).(http.Header) } - if errAny := tuple[errorIndex].Interface(); !isNil(errAny) { + if errAny := tuple[errorIndex].Interface(); !utils.IsNil(errAny) { res.Error = errAny.(*Error) if res.Error.Code == InternalError { s.listener.OnRequestFailed(req.Method, res.Error) diff --git a/utils/check.go b/utils/check.go new file mode 100644 index 0000000000..56e5c89185 --- /dev/null +++ b/utils/check.go @@ -0,0 +1,7 @@ +package utils + +import "reflect" + +func IsNil(i any) bool { + return i == nil || reflect.ValueOf(i).IsNil() +}