From eeeb31f9eaa262c4c16511316ec9a39fb2e82b18 Mon Sep 17 00:00:00 2001 From: gazebo Date: Sun, 9 Sep 2018 12:14:49 +0800 Subject: [PATCH] - Interface2DirectIntWithDefault added. --- platformapi/jdapi/store_sku_test.go | 27 +++++++++++++++++++++++++++ utils/typeconv.go | 7 +++++++ 2 files changed, 34 insertions(+) diff --git a/platformapi/jdapi/store_sku_test.go b/platformapi/jdapi/store_sku_test.go index 7a3e2f16..6a3f850a 100644 --- a/platformapi/jdapi/store_sku_test.go +++ b/platformapi/jdapi/store_sku_test.go @@ -1,9 +1,12 @@ package jdapi import ( + "fmt" + "reflect" "testing" "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/utils" ) const ( @@ -49,3 +52,27 @@ func TestQueryStockCenter(t *testing.T) { baseapi.SugarLogger.Debug(v) } } + +type TTest struct { + A int +} + +func (t *TTest) Foo(param1 int, param2 string) string { + fmt.Println(param1, param2) + return utils.Int2Str(param1) + "|" + param2 +} + +func TestIt(t *testing.T) { + var param1, param2 interface{} + param1 = 1 + param2 = "bbb" + var tt interface{} + tt = &TTest{} + ttValue := reflect.ValueOf(tt) + funcFoo := ttValue.MethodByName("Foo") + params := []reflect.Value{ + reflect.ValueOf(param1), + reflect.ValueOf(param2), + } + fmt.Println(funcFoo.Call(params)[0]) +} diff --git a/utils/typeconv.go b/utils/typeconv.go index 2f8b57b3..49100f98 100644 --- a/utils/typeconv.go +++ b/utils/typeconv.go @@ -67,6 +67,13 @@ func Interface2Int64WithDefault(data interface{}, defValue int64) int64 { return MustInterface2Int64(data) } +func Interface2DirectIntWithDefault(data interface{}, defValue int) int { + if data == nil { + return defValue + } + return data.(int) +} + func MustInterface2Float64(data interface{}) float64 { dataNumber, ok := data.(json.Number) if !ok {