1 Star 0 Fork 12

小马过河/etherscan-api

forked from 楠木/etherscan-api 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
helper.go 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
楠木 提交于 2018-09-08 21:44 . [fix] repair nil check of compose()
/*
* Copyright (c) 2018 LI Zhennan
*
* Use of this work is governed by a MIT License.
* You may find a license copy in project root.
*/
package etherscan
import (
"math/big"
"reflect"
"strconv"
"time"
)
// compose adds input to param, whose key is tag
// 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
}
// M is a type shorthand for param input
type M map[string]interface{}
// BigInt is a wrapper over big.Int to implement only unmarshalText
// for json decoding.
type BigInt big.Int
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (b *BigInt) UnmarshalText(text []byte) (err error) {
var bigInt = new(big.Int)
err = bigInt.UnmarshalText(text)
if err != nil {
return
}
*b = BigInt(*bigInt)
return nil
}
// MarshalText implements the encoding.TextMarshaler
func (b *BigInt) MarshalText() (text []byte, err error) {
return []byte(b.Int().String()), nil
}
// Int returns b's *big.Int form
func (b *BigInt) Int() *big.Int {
return (*big.Int)(b)
}
// Time is a wrapper over big.Int to implement only unmarshalText
// for json decoding.
type Time time.Time
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (t *Time) UnmarshalText(text []byte) (err error) {
input, err := strconv.ParseInt(string(text), 10, 64)
if err != nil {
err = wrapErr(err, "strconv.ParseInt")
return
}
var timestamp = time.Unix(input, 0)
*t = Time(timestamp)
return nil
}
// Time returns t's time.Time form
func (t Time) Time() time.Time {
return time.Time(t)
}
// MarshalText implements the encoding.TextMarshaler
func (t Time) MarshalText() (text []byte, err error) {
return []byte(strconv.FormatInt(t.Time().Unix(), 10)), nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/andygoo/etherscan-api.git
git@gitee.com:andygoo/etherscan-api.git
andygoo
etherscan-api
etherscan-api
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385