改进gdb.Result与gdb.List, gdb.Record与gdb.Map之间的类型转换,便于业务层数据编码处理(如json/xml)

This commit is contained in:
John
2018-05-02 18:52:04 +08:00
parent 26956ffa71
commit cc42a572fe
4 changed files with 63 additions and 8 deletions

View File

@ -10,6 +10,15 @@ import (
"gitee.com/johng/gf/g/util/gutil"
)
// 将Record转换为Map其中最主要的区别是里面的键值被强制转换为string类型方便json处理
func (r Record) ToMap() Map {
m := make(map[string]interface{})
for k, v := range r {
m[k] = v.String()
}
return m
}
// 将Map变量映射到指定的struct对象中注意参数应当是一个对象的指针
func (r Record) ToStruct(obj interface{}) error {
m := make(map[string]interface{})

View File

@ -10,8 +10,50 @@ import (
"gitee.com/johng/gf/g/util/gconv"
)
// 将结果集转换为List类型返回便于json处理
func (r Result) ToList() List {
l := make(List, len(r))
for k, v := range r {
l[k] = v.ToMap()
}
return l
}
// 将结果列表按照指定的字段值做map[string]Map
func (r Result) ToStringMap(key string) map[string]Record {
func (r Result) ToStringMap(key string) map[string]Map {
m := make(map[string]Map)
for _, item := range r {
if v, ok := item[key]; ok {
m[gconv.String(v)] = item.ToMap()
}
}
return m
}
// 将结果列表按照指定的字段值做map[int]Map
func (r Result) ToIntMap(key string) map[int]Map {
m := make(map[int]Map)
for _, item := range r {
if v, ok := item[key]; ok {
m[gconv.Int(v)] = item.ToMap()
}
}
return m
}
// 将结果列表按照指定的字段值做map[uint]Map
func (r Result) ToUintMap(key string) map[uint]Map {
m := make(map[uint]Map)
for _, item := range r {
if v, ok := item[key]; ok {
m[gconv.Uint(v)] = item.ToMap()
}
}
return m
}
// 将结果列表按照指定的字段值做map[string]Record
func (r Result) ToStringRecord(key string) map[string]Record {
m := make(map[string]Record)
for _, item := range r {
if v, ok := item[key]; ok {
@ -21,8 +63,8 @@ func (r Result) ToStringMap(key string) map[string]Record {
return m
}
// 将结果列表按照指定的字段值做map[int]Map
func (r Result) ToIntMap(key string) map[int]Record {
// 将结果列表按照指定的字段值做map[int]Record
func (r Result) ToIntRecord(key string) map[int]Record {
m := make(map[int]Record)
for _, item := range r {
if v, ok := item[key]; ok {
@ -32,8 +74,8 @@ func (r Result) ToIntMap(key string) map[int]Record {
return m
}
// 将结果列表按照指定的字段值做map[uint]Map
func (r Result) ToUintMap(key string) map[uint]Record {
// 将结果列表按照指定的字段值做map[uint]Record
func (r Result) ToUintRecord(key string) map[uint]Record {
m := make(map[uint]Record)
for _, item := range r {
if v, ok := item[key]; ok {

View File

@ -6,6 +6,7 @@ import (
"gitee.com/johng/gf/g/database/gdb"
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/frame/gins"
"gitee.com/johng/gf/g/encoding/gparser"
)
// 本文件用于gf框架的mysql数据库操作示例不作为单元测试使用
@ -477,7 +478,10 @@ func mapToStruct() {
}
func main() {
mapToStruct()
r, _ := db.Table("user").Select()
j, _ := gparser.VarToJson(r.ToList())
fmt.Println(string(j))
fmt.Println(r)
//create()
//create()
//insert()

View File

@ -6,7 +6,7 @@ database:
- host: 127.0.0.1
port: 3306
user: root
pass: "8692651"
pass: "123456"
name: test
type: mysql
role: master
@ -15,7 +15,7 @@ database:
- host: 127.0.0.1
port: 3306
user: root
pass: "8692651"
pass: "123456"
name: test
type: mysql
role: master