add and improve Scan/ScanDeep feature for package gdb/gvar/gjson/gconv

This commit is contained in:
John
2020-06-15 16:46:48 +08:00
parent 49d1e3dbaf
commit 508cb7db88
6 changed files with 283 additions and 8 deletions

View File

@ -329,27 +329,39 @@ func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...ma
return gconv.StructsDeep(j.Get(pattern), pointer, mapping...)
}
// GetMapToMap retrieves the value by specified <pattern> and converts it specified map variable.
// GetScan automatically calls Struct or Structs function according to the type of parameter
// <pointer> to implement the converting..
func (j *Json) GetScan(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.Scan(j.Get(pattern), pointer, mapping...)
}
// GetScanDeep automatically calls StructDeep or StructsDeep function according to the type of
// parameter <pointer> to implement the converting..
func (j *Json) GetScanDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.ScanDeep(j.Get(pattern), pointer, mapping...)
}
// GetMapToMap retrieves the value by specified <pattern> and converts it to specified map variable.
// See gconv.MapToMap.
func (j *Json) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.MapToMap(j.Get(pattern), pointer, mapping...)
}
// GetMapToMapDeep retrieves the value by specified <pattern> and converts it specified map
// GetMapToMapDeep retrieves the value by specified <pattern> and converts it to specified map
// variable recursively.
// See gconv.MapToMapDeep.
func (j *Json) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.MapToMapDeep(j.Get(pattern), pointer, mapping...)
}
// GetMapToMaps retrieves the value by specified <pattern> and converts it specified map slice
// GetMapToMaps retrieves the value by specified <pattern> and converts it to specified map slice
// variable.
// See gconv.MapToMaps.
func (j *Json) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
return gconv.MapToMaps(j.Get(pattern), pointer, mapping...)
}
// GetMapToMapsDeep retrieves the value by specified <pattern> and converts it specified map slice
// GetMapToMapsDeep retrieves the value by specified <pattern> and converts it to specified map slice
// variable recursively.
// See gconv.MapToMapsDeep.
func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
@ -404,6 +416,18 @@ func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string)
return gconv.StructsDeep(*(j.p), pointer, mapping...)
}
// ToScan automatically calls Struct or Structs function according to the type of parameter
// <pointer> to implement the converting..
func (j *Json) ToScan(pointer interface{}, mapping ...map[string]string) error {
return gconv.Scan(*(j.p), pointer, mapping...)
}
// ToScanDeep automatically calls StructDeep or StructsDeep function according to the type of
// parameter <pointer> to implement the converting..
func (j *Json) ToScanDeep(pointer interface{}, mapping ...map[string]string) error {
return gconv.ScanDeep(*(j.p), pointer, mapping...)
}
// ToMapToMap converts current Json object to specified map variable.
// The parameter of <pointer> should be type of *map.
func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {

View File

@ -12,6 +12,116 @@ import (
"testing"
)
func Test_GetScan(t *testing.T) {
type User struct {
Name string
Score float64
}
j := gjson.New(`[{"name":"john", "score":"100"},{"name":"smith", "score":"60"}]`)
gtest.C(t, func(t *gtest.T) {
var user *User
err := j.GetScan("1", &user)
t.Assert(err, nil)
t.Assert(user, &User{
Name: "smith",
Score: 60,
})
})
gtest.C(t, func(t *gtest.T) {
var users []User
err := j.GetScan(".", &users)
t.Assert(err, nil)
t.Assert(users, []User{
{
Name: "john",
Score: 100,
},
{
Name: "smith",
Score: 60,
},
})
})
}
func Test_GetScanDeep(t *testing.T) {
type User struct {
Name string
Score float64
}
j := gjson.New(`[{"name":"john", "score":"100"},{"name":"smith", "score":"60"}]`)
gtest.C(t, func(t *gtest.T) {
var user *User
err := j.GetScanDeep("1", &user)
t.Assert(err, nil)
t.Assert(user, &User{
Name: "smith",
Score: 60,
})
})
gtest.C(t, func(t *gtest.T) {
var users []User
err := j.GetScanDeep(".", &users)
t.Assert(err, nil)
t.Assert(users, []User{
{
Name: "john",
Score: 100,
},
{
Name: "smith",
Score: 60,
},
})
})
}
func Test_ToScan(t *testing.T) {
type User struct {
Name string
Score float64
}
j := gjson.New(`[{"name":"john", "score":"100"},{"name":"smith", "score":"60"}]`)
gtest.C(t, func(t *gtest.T) {
var users []User
err := j.ToScan(&users)
t.Assert(err, nil)
t.Assert(users, []User{
{
Name: "john",
Score: 100,
},
{
Name: "smith",
Score: 60,
},
})
})
}
func Test_ToScanDeep(t *testing.T) {
type User struct {
Name string
Score float64
}
j := gjson.New(`[{"name":"john", "score":"100"},{"name":"smith", "score":"60"}]`)
gtest.C(t, func(t *gtest.T) {
var users []User
err := j.ToScanDeep(&users)
t.Assert(err, nil)
t.Assert(users, []User{
{
Name: "john",
Score: 100,
},
{
Name: "smith",
Score: 60,
},
})
})
}
func Test_ToStruct1(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type BaseInfoItem struct {