Skip to content

Commit

Permalink
Merge pull request #4 from nanmu42/develop
Browse files Browse the repository at this point in the history
Repair nil check of compose()
  • Loading branch information
nanmu42 authored Sep 8, 2018
2 parents 19383e3 + 06e346a commit 2cff1af
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
17 changes: 13 additions & 4 deletions account_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,27 @@ func TestClient_InternalTxByAddress(t *testing.T) {
}

func TestClient_ERC20Transfers(t *testing.T) {
const wantLen = 3
const (
wantLen1 = 3
wantLen2 = 458
)

var a, b = 3273004, 3328071
var contract, address = "0xe0b7927c4af23765cb51314a0e0521a9645f0e2a", "0x4e83362442b8d1bec281594cea3050c8eb01311c"
txs, err := api.ERC20Transfers(&contract, &address, &a, &b, 1, 500)
noError(t, err, "api.ERC20Transfers")
noError(t, err, "api.ERC20Transfers 1")

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

if len(txs) != wantLen {
t.Errorf("got txs length %v, want %v", len(txs), wantLen)
if len(txs) != wantLen1 {
t.Errorf("got txs length %v, want %v", len(txs), wantLen1)
}

txs, err = api.ERC20Transfers(nil, &address, nil, &b, 1, 500)
noError(t, err, "api.ERC20Transfers 2")
if len(txs) != wantLen2 {
t.Errorf("got txs length %v, want %v", len(txs), wantLen2)
}
}

Expand Down
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (c *Client) call(module, action string, param map[string]interface{}, outco
defer c.AfterRequest(module, action, param, outcome, err)
}

// recover if there shall be an panic
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("[ouch! panic recovered] please report this with what you did and what you expected, panic detail: %v", r)
}
}()

req, err := http.NewRequest(http.MethodGet, c.craftURL(module, action, param), http.NoBody)
if err != nil {
err = wrapErr(err, "http.NewRequest")
Expand Down
14 changes: 13 additions & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,28 @@ package etherscan

import (
"math/big"
"reflect"
"strconv"
"time"
)

// compose adds input to param, whose key is tag
// if input is nil, compose is a no-op.
// if input is nil or nil of some type, compose is a no-op.
func compose(param map[string]interface{}, tag string, input interface{}) {
// simple situation
if input == nil {
return
}

// needs dig further
v := reflect.ValueOf(input)
switch v.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Interface:
if v.IsNil() {
return
}
}

param[tag] = input
}

Expand Down
2 changes: 1 addition & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func valueToStr(v reflect.Value) (str string) {
case reflect.Int:
str = strconv.FormatInt(v.Int(), 10)
default:
panic(fmt.Sprintf("valueToStr: %v is of unexpected kind %q", v.Interface(), v.Kind()))
panic(fmt.Sprintf("valueToStr: %v is of unexpected kind %q", v, v.Kind()))
}
return
}
Expand Down

0 comments on commit 2cff1af

Please sign in to comment.