- refactor
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
package elmapi
|
package elmapi
|
||||||
|
|
||||||
const (
|
const (
|
||||||
OrderStatusFake = "fake"
|
|
||||||
|
|
||||||
OrderStatusPending = "pending"
|
OrderStatusPending = "pending"
|
||||||
OrderStatusUnprocessed = "unprocessed"
|
OrderStatusUnprocessed = "unprocessed"
|
||||||
OrderStatusRefunding = "refunding"
|
OrderStatusRefunding = "refunding"
|
||||||
|
|||||||
209
utils/typeconv.go
Normal file
209
utils/typeconv.go
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.rosy.net.cn/baseapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetConcretValue(value reflect.Value) reflect.Value {
|
||||||
|
for {
|
||||||
|
if value.Kind() == reflect.Interface || value.Kind() == reflect.Ptr {
|
||||||
|
value = value.Elem()
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnmarshalUseNumber(data []byte, result interface{}) error {
|
||||||
|
d := json.NewDecoder(bytes.NewReader(data))
|
||||||
|
d.UseNumber()
|
||||||
|
err := d.Decode(result)
|
||||||
|
if err != nil {
|
||||||
|
baseapi.SugarLogger.Errorf("decode data:%v, error:%v", string(data), err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func MustMarshal(obj interface{}) []byte {
|
||||||
|
byteArr, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("err when Marshal obj:%v", obj))
|
||||||
|
}
|
||||||
|
|
||||||
|
return byteArr
|
||||||
|
}
|
||||||
|
|
||||||
|
func Bool2String(value bool) string {
|
||||||
|
if value {
|
||||||
|
return "true"
|
||||||
|
}
|
||||||
|
return "false"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Str2Int64WithDefault(str string, defValue int64) int64 {
|
||||||
|
retVal, err := strconv.ParseInt(str, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return defValue
|
||||||
|
}
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func Str2Int64(str string) int64 {
|
||||||
|
retVal, err := strconv.ParseInt(str, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
baseapi.SugarLogger.Errorf("error when convert %s to int64", str)
|
||||||
|
}
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func Str2Float64(str string) float64 {
|
||||||
|
retVal, err := strconv.ParseFloat(str, 64)
|
||||||
|
if err != nil {
|
||||||
|
baseapi.SugarLogger.Errorf("error when convert %s to float64", str)
|
||||||
|
}
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func Int64ToStr(value int64) string {
|
||||||
|
return strconv.FormatInt(value, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Int2Str(value int) string {
|
||||||
|
return strconv.Itoa(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MustInterface2Int64(data interface{}) int64 {
|
||||||
|
dataNumber, ok := data.(json.Number)
|
||||||
|
if !ok {
|
||||||
|
panic(fmt.Sprintf("error when cast:%v to int64", data))
|
||||||
|
}
|
||||||
|
retVal, err := dataNumber.Int64()
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func MustInterface2Float64(data interface{}) float64 {
|
||||||
|
dataNumber, ok := data.(json.Number)
|
||||||
|
if !ok {
|
||||||
|
panic(fmt.Sprintf("error when convert:%v", data))
|
||||||
|
}
|
||||||
|
retVal, err := dataNumber.Float64()
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func Interface2Float64(data interface{}) (retVal float64) {
|
||||||
|
if dataNumber, ok := data.(json.Number); ok {
|
||||||
|
retVal, _ = dataNumber.Float64()
|
||||||
|
}
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func Interface2String(data interface{}) string {
|
||||||
|
if data == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return data.(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// timestamp is in second
|
||||||
|
func Timestamp2Str(timestamp int64) string {
|
||||||
|
return Time2Str(time.Unix(timestamp, 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Timestamp2Time(timestamp int64) time.Time {
|
||||||
|
return time.Unix(timestamp, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Time2Str(t time.Time) string {
|
||||||
|
return t.Format("2006-01-02 15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Str2Time(timeStr string) time.Time {
|
||||||
|
timeStr = strings.Replace(timeStr, "T", " ", 1)
|
||||||
|
retVal, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
|
||||||
|
if err != nil {
|
||||||
|
baseapi.SugarLogger.Errorf("ParseInLocation failed, timeStr:%v, error:%v", timeStr, err)
|
||||||
|
}
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func HTTPResponse2Json(response *http.Response) (map[string]interface{}, error) {
|
||||||
|
var jsonResult map[string]interface{}
|
||||||
|
bodyData, err := ioutil.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
baseapi.SugarLogger.Errorf("ioutil.ReadAll error:%v, response:%v", err, response)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = UnmarshalUseNumber(bodyData, &jsonResult); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return jsonResult, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func HTTPBody2Values(data []byte, needDecode bool) (url.Values, error) {
|
||||||
|
bodyStr := string(data)
|
||||||
|
if needDecode {
|
||||||
|
bodyStr1, err := url.QueryUnescape(bodyStr)
|
||||||
|
if err != nil {
|
||||||
|
baseapi.SugarLogger.Errorf("QueryUnescape error:%v, bodyStr:%v", err, bodyStr)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
bodyStr = bodyStr1
|
||||||
|
}
|
||||||
|
result, err := url.ParseQuery(bodyStr)
|
||||||
|
if err != nil {
|
||||||
|
baseapi.SugarLogger.Errorf("ParseQuery error:%v, bodyStr:%v", err, bodyStr)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Params2Map(key1, value1 interface{}, kv ...interface{}) (retVal map[string]interface{}) {
|
||||||
|
retVal = make(map[string]interface{})
|
||||||
|
retVal[key1.(string)] = value1
|
||||||
|
|
||||||
|
key := ""
|
||||||
|
for index, v := range kv {
|
||||||
|
if index%2 == 0 {
|
||||||
|
key = v.(string)
|
||||||
|
} else {
|
||||||
|
retVal[key] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func URLValues2Map(values url.Values) (retVal map[string]interface{}) {
|
||||||
|
retVal = make(map[string]interface{})
|
||||||
|
for k := range values {
|
||||||
|
retVal[k] = values.Get(k)
|
||||||
|
}
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func Map2URLValues(mapData map[string]interface{}) (retVal url.Values) {
|
||||||
|
retVal = make(url.Values)
|
||||||
|
for k, v := range mapData {
|
||||||
|
retVal.Set(k, fmt.Sprint(v))
|
||||||
|
}
|
||||||
|
return retVal
|
||||||
|
}
|
||||||
183
utils/utils.go
183
utils/utils.go
@@ -1,33 +1,17 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi"
|
"git.rosy.net.cn/baseapi"
|
||||||
|
|
||||||
"github.com/satori/go.uuid"
|
"github.com/satori/go.uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetConcretValue(value reflect.Value) reflect.Value {
|
|
||||||
for {
|
|
||||||
if value.Kind() == reflect.Interface || value.Kind() == reflect.Ptr {
|
|
||||||
value = value.Elem()
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
func DictKeysMan(data interface{}, keysToRemove []string, keysToKeep []string) interface{} {
|
func DictKeysMan(data interface{}, keysToRemove []string, keysToKeep []string) interface{} {
|
||||||
if data == nil || (keysToKeep == nil && keysToRemove == nil) {
|
if data == nil || (keysToKeep == nil && keysToRemove == nil) {
|
||||||
return data
|
return data
|
||||||
@@ -90,56 +74,6 @@ func DictKeysMan(data interface{}, keysToRemove []string, keysToKeep []string) i
|
|||||||
return retVal
|
return retVal
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnmarshalUseNumber(data []byte, result interface{}) error {
|
|
||||||
d := json.NewDecoder(bytes.NewReader(data))
|
|
||||||
d.UseNumber()
|
|
||||||
err := d.Decode(result)
|
|
||||||
if err != nil {
|
|
||||||
baseapi.SugarLogger.Errorf("decode data:%v, error:%v", string(data), err)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustMarshal(obj interface{}) []byte {
|
|
||||||
byteArr, err := json.Marshal(obj)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("err when Marshal obj:%v", obj))
|
|
||||||
}
|
|
||||||
|
|
||||||
return byteArr
|
|
||||||
}
|
|
||||||
|
|
||||||
func Bool2String(value bool) string {
|
|
||||||
if value {
|
|
||||||
return "true"
|
|
||||||
}
|
|
||||||
return "false"
|
|
||||||
}
|
|
||||||
|
|
||||||
func Str2Int64WithDefault(str string, defValue int64) int64 {
|
|
||||||
retVal, err := strconv.ParseInt(str, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return defValue
|
|
||||||
}
|
|
||||||
return retVal
|
|
||||||
}
|
|
||||||
|
|
||||||
func Str2Int64(str string) int64 {
|
|
||||||
retVal, err := strconv.ParseInt(str, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
baseapi.SugarLogger.Errorf("error when convert %s to int64", str)
|
|
||||||
}
|
|
||||||
return retVal
|
|
||||||
}
|
|
||||||
|
|
||||||
func Int64ToStr(value int64) string {
|
|
||||||
return strconv.FormatInt(value, 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Int2Str(value int) string {
|
|
||||||
return strconv.Itoa(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 去除-号,全部大写,比如:929ADB626EB911E893E452540009DAB3
|
// 去除-号,全部大写,比如:929ADB626EB911E893E452540009DAB3
|
||||||
func GetUUID() string {
|
func GetUUID() string {
|
||||||
return strings.ToUpper(strings.Replace(uuid.Must(uuid.NewV1()).String(), "-", "", -1))
|
return strings.ToUpper(strings.Replace(uuid.Must(uuid.NewV1()).String(), "-", "", -1))
|
||||||
@@ -158,91 +92,6 @@ func GetAPIOperator() string {
|
|||||||
return "jxc4-" + Time2Str(time.Now())
|
return "jxc4-" + Time2Str(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
// timestamp is in second
|
|
||||||
func Timestamp2Str(timestamp int64) string {
|
|
||||||
return Time2Str(time.Unix(timestamp, 0))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Timestamp2Time(timestamp int64) time.Time {
|
|
||||||
return time.Unix(timestamp, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Time2Str(t time.Time) string {
|
|
||||||
return t.Format("2006-01-02 15:04:05")
|
|
||||||
}
|
|
||||||
|
|
||||||
func Str2Time(timeStr string) time.Time {
|
|
||||||
timeStr = strings.Replace(timeStr, "T", " ", 1)
|
|
||||||
retVal, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
|
|
||||||
if err != nil {
|
|
||||||
baseapi.SugarLogger.Errorf("ParseInLocation failed, timeStr:%v, error:%v", timeStr, err)
|
|
||||||
}
|
|
||||||
return retVal
|
|
||||||
}
|
|
||||||
|
|
||||||
func HTTPResponse2Json(response *http.Response) (map[string]interface{}, error) {
|
|
||||||
var jsonResult map[string]interface{}
|
|
||||||
bodyData, err := ioutil.ReadAll(response.Body)
|
|
||||||
if err != nil {
|
|
||||||
baseapi.SugarLogger.Errorf("ioutil.ReadAll error:%v, response:%v", err, response)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = UnmarshalUseNumber(bodyData, &jsonResult); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return jsonResult, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func HTTPBody2Values(data []byte, needDecode bool) (url.Values, error) {
|
|
||||||
bodyStr := string(data)
|
|
||||||
if needDecode {
|
|
||||||
bodyStr1, err := url.QueryUnescape(bodyStr)
|
|
||||||
if err != nil {
|
|
||||||
baseapi.SugarLogger.Errorf("QueryUnescape error:%v, bodyStr:%v", err, bodyStr)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
bodyStr = bodyStr1
|
|
||||||
}
|
|
||||||
result, err := url.ParseQuery(bodyStr)
|
|
||||||
if err != nil {
|
|
||||||
baseapi.SugarLogger.Errorf("ParseQuery error:%v, bodyStr:%v", err, bodyStr)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustInterface2Int64(data interface{}) int64 {
|
|
||||||
dataNumber, ok := data.(json.Number)
|
|
||||||
if !ok {
|
|
||||||
panic(fmt.Sprintf("error when cast:%v to int64", data))
|
|
||||||
}
|
|
||||||
retVal, err := dataNumber.Int64()
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
return retVal
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustInterface2Float64(data interface{}) float64 {
|
|
||||||
dataNumber, ok := data.(json.Number)
|
|
||||||
if !ok {
|
|
||||||
panic(fmt.Sprintf("error when convert:%v", data))
|
|
||||||
}
|
|
||||||
retVal, err := dataNumber.Float64()
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
return retVal
|
|
||||||
}
|
|
||||||
|
|
||||||
func Interface2String(data interface{}) string {
|
|
||||||
if data == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return data.(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
func MergeMaps(firstMap map[string]interface{}, otherMaps ...map[string]interface{}) (retVal map[string]interface{}) {
|
func MergeMaps(firstMap map[string]interface{}, otherMaps ...map[string]interface{}) (retVal map[string]interface{}) {
|
||||||
retVal = make(map[string]interface{})
|
retVal = make(map[string]interface{})
|
||||||
allMaps := append(otherMaps, firstMap)
|
allMaps := append(otherMaps, firstMap)
|
||||||
@@ -274,38 +123,6 @@ func CallFuncAsync(funcToCall func()) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Params2Map(key1, value1 interface{}, kv ...interface{}) (retVal map[string]interface{}) {
|
|
||||||
retVal = make(map[string]interface{})
|
|
||||||
retVal[key1.(string)] = value1
|
|
||||||
|
|
||||||
key := ""
|
|
||||||
for index, v := range kv {
|
|
||||||
if index%2 == 0 {
|
|
||||||
key = v.(string)
|
|
||||||
} else {
|
|
||||||
retVal[key] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return retVal
|
|
||||||
}
|
|
||||||
|
|
||||||
func URLValues2Map(values url.Values) (retVal map[string]interface{}) {
|
|
||||||
retVal = make(map[string]interface{})
|
|
||||||
for k := range values {
|
|
||||||
retVal[k] = values.Get(k)
|
|
||||||
}
|
|
||||||
return retVal
|
|
||||||
}
|
|
||||||
|
|
||||||
func Map2URLValues(mapData map[string]interface{}) (retVal url.Values) {
|
|
||||||
retVal = make(url.Values)
|
|
||||||
for k, v := range mapData {
|
|
||||||
retVal.Set(k, fmt.Sprint(v))
|
|
||||||
}
|
|
||||||
return retVal
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenerateGetURL(baseURL, apiStr string, params map[string]interface{}) string {
|
func GenerateGetURL(baseURL, apiStr string, params map[string]interface{}) string {
|
||||||
queryString := ""
|
queryString := ""
|
||||||
if params != nil {
|
if params != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user