- 添加Struct2MapByJson

- 去掉将全局structs.DefaultTagName设置为"json"
This commit is contained in:
gazebo
2019-03-27 22:42:35 +08:00
parent 11c6386331
commit ea13a07fea
10 changed files with 26 additions and 23 deletions

View File

@@ -6,8 +6,6 @@ import (
"sort"
"strings"
"github.com/fatih/structs"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
)
@@ -82,7 +80,7 @@ func (a *API) GetOrderCallbackMsg(data []byte) (msg *CallbackMsg, callbackRespon
return nil, FailedResponse
}
mapData := structs.Map(msg)
mapData := utils.Struct2MapByJson(msg)
callbackResponse = a.CheckCallbackValidation(mapData)
return msg, callbackResponse
}

View File

@@ -2,7 +2,6 @@ package dadaapi
import (
"git.rosy.net.cn/baseapi/utils"
"github.com/fatih/structs"
)
const (
@@ -89,7 +88,7 @@ func map2CreateOrderResponse(mapData map[string]interface{}) *CreateOrderRespons
}
func (a *API) operateOrder(action string, orderInfo *OperateOrderRequiredParams, addParams map[string]interface{}) (retVal *CreateOrderResponse, err error) {
params := structs.Map(orderInfo)
params := utils.Struct2MapByJson(orderInfo)
params["callback"] = a.callbackURL
allParams := utils.MergeMaps(params, addParams)
result, err := a.AccessAPI(action, allParams)

View File

@@ -13,7 +13,6 @@ import (
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
"github.com/fatih/structs"
)
const (
@@ -65,7 +64,7 @@ func (a *API) PackCallbackResult(result string) (response *CallbackResponse) {
Timestamp: utils.Int64ToStr(time.Now().Unix()),
Nonce: utils.GetUUID(),
}
response.MsgSignature = a.calculateCallbackSign(structs.Map(response))
response.MsgSignature = a.calculateCallbackSign(utils.Struct2MapByJson(response))
}
return response
}

View File

@@ -6,7 +6,6 @@ import (
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
"github.com/fatih/structs"
)
// https://open.shop.ele.me/openapi/documents/callback
@@ -187,7 +186,7 @@ func (a *API) GetCallbackMsg(data []byte) (msg *CallbackMsg, callbackResponse *C
return nil, callbackResponse
}
mapData := structs.Map(msg)
mapData := utils.Struct2MapByJson(msg)
callbackResponse = a.CheckCallbackValidation(mapData)
return msg, callbackResponse
}

View File

@@ -9,8 +9,6 @@ import (
"strconv"
"strings"
"github.com/fatih/structs"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
@@ -244,7 +242,7 @@ func (a *API) result2OrderResponse(result *ResponseResult) (order *OrderResponse
}
func (a *API) CreateOrderByShop(basicParams *CreateOrderByShopParam, addParams map[string]interface{}) (order *OrderResponse, err error) {
params := structs.Map(basicParams)
params := utils.Struct2MapByJson(basicParams)
params["goods_value"] = strconv.FormatFloat(basicParams.GoodsValue, 'f', 2, 64)
params["goods_weight"] = strconv.FormatFloat(basicParams.GoodsWeight, 'f', 2, 64)
allParams := utils.MergeMaps(params, addParams)

View File

@@ -14,7 +14,6 @@ import (
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
"github.com/fatih/structs"
)
const (
@@ -79,10 +78,6 @@ var (
ErrStrCallbackSignatureIsWrong = "wrong signature"
)
func init() {
structs.DefaultTagName = "json"
}
func getClonedData(requestURL *url.URL, r *bytes.Buffer) string {
if strings.Index(requestURL.String(), "uploadImg") >= 0 {
return "binary content"

View File

@@ -1,8 +1,6 @@
package weimobapi
import (
"github.com/fatih/structs"
)
import "git.rosy.net.cn/baseapi/utils"
const (
OrderStatusWait4Pay = 0 // 待支付
@@ -52,7 +50,7 @@ func (a *API) CancelOrder(orderNo int64, specificCancelReason string) (err error
}
func (a *API) DeliveryOrder(orderDeliveryInfo *DeliveryOrder) (err error) {
apiParams := structs.Map(orderDeliveryInfo)
apiParams := utils.Struct2MapByJson(orderDeliveryInfo)
// baseapi.SugarLogger.Debug(utils.Format4Output(apiParams, false))
_, err = a.AccessAPI("order/deliveryOrder", apiParams)
return err

View File

@@ -50,7 +50,7 @@ func TryUnmarshalUseNumber(data []byte, result interface{}) error {
func Unmarshal2Map(data []byte, structObj interface{}) (resultMap map[string]interface{}, err error) {
if err = json.Unmarshal(data, structObj); err == nil {
if err = json.Unmarshal(data, &resultMap); err == nil {
m := structs.Map(structObj)
m := Struct2MapByJson(structObj)
for k := range resultMap {
if value, ok := m[k]; ok {
resultMap[k] = value
@@ -423,3 +423,9 @@ func Map2KeySlice(flagMap map[string]int) (keyList []string) {
}
return keyList
}
func Struct2MapByJson(obj interface{}) (mapData map[string]interface{}) {
structsObj := structs.New(obj)
structsObj.TagName = "json"
return structsObj.Map()
}

View File

@@ -11,7 +11,7 @@ import (
"unicode/utf8"
"git.rosy.net.cn/baseapi"
"github.com/satori/go.uuid"
uuid "github.com/satori/go.uuid"
)
func DictKeysMan(data interface{}, keysToRemove []string, keysToKeep []string) interface{} {

View File

@@ -174,3 +174,14 @@ func TestTrimBlanChar(t *testing.T) {
t.Fatal("TrimBlanChar doesn't work")
}
}
func TestStruct2MapByJson(t *testing.T) {
mapData := Struct2MapByJson(&struct {
IntData int `structs:"dataInt"`
StrData string `json:"dataStr"`
}{
IntData: 1,
StrData: "2",
})
t.Log(mapData)
}