Skip to content

Commit d07a1f9

Browse files
committed
[fix] repair nil check of compose()
An interface is nil only when both its type and value are nil. Detail: https://golang.org/doc/faq#nil_error A big thank you to llya Evdokimov(@engenegr) for finding this bug! ;)
1 parent 70ef53d commit d07a1f9

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

account_e2e_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,27 @@ func TestClient_InternalTxByAddress(t *testing.T) {
7070
}
7171

7272
func TestClient_ERC20Transfers(t *testing.T) {
73-
const wantLen = 3
73+
const (
74+
wantLen1 = 3
75+
wantLen2 = 458
76+
)
7477

7578
var a, b = 3273004, 3328071
7679
var contract, address = "0xe0b7927c4af23765cb51314a0e0521a9645f0e2a", "0x4e83362442b8d1bec281594cea3050c8eb01311c"
7780
txs, err := api.ERC20Transfers(&contract, &address, &a, &b, 1, 500)
78-
noError(t, err, "api.ERC20Transfers")
81+
noError(t, err, "api.ERC20Transfers 1")
7982

8083
//j, _ := json.MarshalIndent(txs, "", " ")
8184
//fmt.Printf("%s\n", j)
8285

83-
if len(txs) != wantLen {
84-
t.Errorf("got txs length %v, want %v", len(txs), wantLen)
86+
if len(txs) != wantLen1 {
87+
t.Errorf("got txs length %v, want %v", len(txs), wantLen1)
88+
}
89+
90+
txs, err = api.ERC20Transfers(nil, &address, nil, &b, 1, 500)
91+
noError(t, err, "api.ERC20Transfers 2")
92+
if len(txs) != wantLen2 {
93+
t.Errorf("got txs length %v, want %v", len(txs), wantLen2)
8594
}
8695
}
8796

helper.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,28 @@ package etherscan
99

1010
import (
1111
"math/big"
12+
"reflect"
1213
"strconv"
1314
"time"
1415
)
1516

1617
// compose adds input to param, whose key is tag
17-
// if input is nil, compose is a no-op.
18+
// if input is nil or nil of some type, compose is a no-op.
1819
func compose(param map[string]interface{}, tag string, input interface{}) {
20+
// simple situation
1921
if input == nil {
2022
return
2123
}
24+
25+
// needs dig further
26+
v := reflect.ValueOf(input)
27+
switch v.Kind() {
28+
case reflect.Ptr, reflect.Slice, reflect.Interface:
29+
if v.IsNil() {
30+
return
31+
}
32+
}
33+
2234
param[tag] = input
2335
}
2436

0 commit comments

Comments
 (0)