From 04ea89ac622bfe200388c570f162fbb2a005e6fa Mon Sep 17 00:00:00 2001 From: zhuxiujia Date: Thu, 9 Jul 2020 17:03:39 +0800 Subject: [PATCH] up to 6.5.5,fix decode bug --- GoMybatisSqlResultDecoder.go | 36 ++++++++++++++++++++++++------------ README-ch.md | 2 +- README.md | 2 +- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/GoMybatisSqlResultDecoder.go b/GoMybatisSqlResultDecoder.go index f91fbc9..c650efb 100644 --- a/GoMybatisSqlResultDecoder.go +++ b/GoMybatisSqlResultDecoder.go @@ -81,20 +81,20 @@ func (it GoMybatisSqlResultDecoder) Decode(resultMap map[string]*ResultProperty, return e } -func makeStructMap(itemType reflect.Type) (map[string]reflect.Type, error) { +func makeStructMap(itemType reflect.Type) (map[string]*reflect.Type, error) { if itemType.Kind() != reflect.Struct { return nil, nil } - var structMap = map[string]reflect.Type{} + var structMap = map[string]*reflect.Type{} for i := 0; i < itemType.NumField(); i++ { var item = itemType.Field(i) - structMap[strings.ToLower(item.Tag.Get("json"))] = item.Type + structMap[strings.ToLower(item.Tag.Get("json"))] = &item.Type } return structMap, nil } //make an json value -func makeJsonObjBytes(resultMap map[string]*ResultProperty, sqlData map[string][]byte, structMap map[string]reflect.Type) []byte { +func makeJsonObjBytes(resultMap map[string]*ResultProperty, sqlData map[string][]byte, structMap map[string]*reflect.Type) []byte { var jsonData = strings.Builder{} jsonData.WriteString("{") @@ -106,29 +106,41 @@ func makeJsonObjBytes(resultMap map[string]*ResultProperty, sqlData map[string][ jsonData.WriteString("\":") var isStringType = false + var fetched = true if resultMap != nil { var resultMapItem = resultMap[k] if resultMapItem != nil && (resultMapItem.LangType == "string" || resultMapItem.LangType == "time.Time") { isStringType = true } + if resultMapItem == nil { + fetched = false + } } else if structMap != nil { var v = structMap[strings.ToLower(k)] if v != nil { - if v.Kind() == reflect.String || v.String() == "time.Time" { + if (*v).Kind() == reflect.String || (*v).String() == "time.Time" { isStringType = true } } + if v == nil { + fetched = false + } } else { isStringType = true } - if isStringType { - jsonData.WriteString("\"") - jsonData.WriteString(encodeStringValue(sqlV)) - jsonData.WriteString("\"") - } else { - if sqlV == nil || len(sqlV) == 0 { - sqlV = []byte("null") + if fetched { + if isStringType { + jsonData.WriteString("\"") + jsonData.WriteString(encodeStringValue(sqlV)) + jsonData.WriteString("\"") + } else { + if sqlV == nil || len(sqlV) == 0 { + sqlV = []byte("null") + } + jsonData.Write(sqlV) } + } else { + sqlV = []byte("null") jsonData.Write(sqlV) } //write ',' diff --git a/README-ch.md b/README-ch.md index e82a9d9..b869597 100644 --- a/README-ch.md +++ b/README-ch.md @@ -67,7 +67,7 @@ go env -w GOSUMDB=off //go.mod加入依赖 require ( github.com/go-sql-driver/mysql v1.5.0 - github.com/zhuxiujia/GoMybatis v6.5.1+incompatible + github.com/zhuxiujia/GoMybatis v6.5.5+incompatible ) ``` diff --git a/README.md b/README.md index 3d82245..d85ddc9 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ go get github.com/go-sql-driver/mysql //go.mod加入依赖 require ( github.com/go-sql-driver/mysql v1.5.0 - github.com/zhuxiujia/GoMybatis v6.5.1+incompatible + github.com/zhuxiujia/GoMybatis v6.5.5+incompatible ) ```