Skip to content

Commit

Permalink
put IsNil in utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 25, 2024
1 parent 21c8504 commit c680bbf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions adapters/p2p2core/felt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}

Expand Down
8 changes: 2 additions & 6 deletions jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions utils/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package utils

import "reflect"

func IsNil(i any) bool {
return i == nil || reflect.ValueOf(i).IsNil()
}

0 comments on commit c680bbf

Please sign in to comment.