fix issue converting empty map[string]string to map[string]interface{}

This commit is contained in:
John Guo
2021-12-17 17:22:55 +08:00
parent 3658faafae
commit b27259aded
3 changed files with 21 additions and 6 deletions

View File

@ -115,6 +115,10 @@ func doMapConvert(value interface{}, recursive bool, tags ...string) map[string]
for k, v := range r {
dataMap[k] = v
}
case map[string]string:
for k, v := range r {
dataMap[k] = v
}
case map[string]interface{}:
if recursive {
// A copy of current map.
@ -210,10 +214,8 @@ func doMapConvertForMapOrStructValue(isRoot bool, value interface{}, recursive b
tags...,
)
}
if len(dataMap) == 0 {
return value
}
return dataMap
case reflect.Struct:
// Map converting interface check.
if v, ok := value.(iMapStrAny); ok {

View File

@ -105,7 +105,6 @@ func Scan(params interface{}, pointer interface{}, mapping ...map[string]string)
return doStructs(params, pointer, keyToAttributeNameMapping, "")
default:
return doStruct(params, pointer, keyToAttributeNameMapping, "")
}
}

View File

@ -7,14 +7,15 @@
package gconv_test
import (
"testing"
"time"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
"testing"
"time"
)
func Test_Struct_Basic1(t *testing.T) {
@ -1244,3 +1245,16 @@ func Test_Struct_MapAttribute(t *testing.T) {
t.AssertNil(err)
})
}
func Test_Struct_Empty_MapStringString(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type S struct {
Id int
}
var (
s = &S{}
err = gconv.Struct(map[string]string{}, s)
)
t.AssertNil(err)
})
}