Skip to content

Commit

Permalink
fix bug [#25]
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Sep 16, 2019
1 parent ec74906 commit 37a0fa1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
15 changes: 15 additions & 0 deletions GoMybatis.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,19 @@ func scanStructArgFields(v reflect.Value, tag *TagArg) map[string]interface{} {
if t.Kind() != reflect.Struct {
panic(`[GoMybatis] the scanParamterBean() arg is not a struct type!,type =` + t.String())
}

var structArg = make(map[string]interface{})

//json arg,性能较差
//var vptr=v.Interface()
//var js,_=json.Marshal(vptr)
//json.Unmarshal(js,&structArg)
//
//for key,value:=range structArg {
// parameters[key]=value
//}

//reflect arg,性能较快
for i := 0; i < t.NumField(); i++ {
var typeValue = t.Field(i)
var field = v.Field(i)
Expand All @@ -493,6 +505,9 @@ func scanStructArgFields(v reflect.Value, tag *TagArg) map[string]interface{} {
obj = field.Interface()
}
var jsonKey = typeValue.Tag.Get(`json`)
if strings.Index(jsonKey, ",") != -1 {
jsonKey = strings.Split(jsonKey, ",")[0]
}
if jsonKey != "" {
parameters[jsonKey] = obj
structArg[jsonKey] = obj
Expand Down
48 changes: 48 additions & 0 deletions GoMybatis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package GoMybatis

import (
"github.com/zhuxiujia/GoMybatis/example"
"reflect"
"testing"
"time"
)

func TestScanStructArgFields(ff *testing.T) {
var act = example.Activity{
Id: "123",
Uuid: "uu",
Name: "test",
PcLink: "pc",
H5Link: "h5",
Remark: "remark",
Version: 0,
CreateTime: time.Now(),
DeleteFlag: 1,
}
scanStructArgFields(reflect.ValueOf(act), nil)
var t = reflect.TypeOf(act)
for i := 0; i < t.NumField(); i++ {
var typeValue = t.Field(i)
var jsonKey = typeValue.Tag.Get(`json`)
println(jsonKey)
}
}

func BenchmarkScanStructArgFields(b *testing.B) {
b.StopTimer()
var act = example.Activity{
Id: "123",
Uuid: "uu",
Name: "test",
PcLink: "pc",
H5Link: "h5",
Remark: "remark",
Version: 0,
CreateTime: time.Now(),
DeleteFlag: 1,
}
b.StartTimer()
for i := 0; i < b.N; i++ {
scanStructArgFields(reflect.ValueOf(act), nil)
}
}
4 changes: 2 additions & 2 deletions example/Example_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const MysqlUri = "*"
//定义数据库模型
//例子:Activity 活动数据
type Activity struct {
Id string `json:"id"`
Id string `json:"id,omitempty"`
Uuid string `json:"uuid"`
Name string `json:"name"`
PcLink string `json:"pcLink"`
PcLink string `json:"pcLink"`
H5Link string `json:"h5Link"`
Remark string `json:"remark"`
Version int `json:"version"`
Expand Down

0 comments on commit 37a0fa1

Please sign in to comment.