- 添加Struct2MapByJson

- 去掉将全局structs.DefaultTagName设置为"json"
This commit is contained in:
gazebo
2019-03-27 22:42:35 +08:00
parent 11c6386331
commit ea13a07fea
10 changed files with 26 additions and 23 deletions

View File

@@ -50,7 +50,7 @@ func TryUnmarshalUseNumber(data []byte, result interface{}) error {
func Unmarshal2Map(data []byte, structObj interface{}) (resultMap map[string]interface{}, err error) {
if err = json.Unmarshal(data, structObj); err == nil {
if err = json.Unmarshal(data, &resultMap); err == nil {
m := structs.Map(structObj)
m := Struct2MapByJson(structObj)
for k := range resultMap {
if value, ok := m[k]; ok {
resultMap[k] = value
@@ -423,3 +423,9 @@ func Map2KeySlice(flagMap map[string]int) (keyList []string) {
}
return keyList
}
func Struct2MapByJson(obj interface{}) (mapData map[string]interface{}) {
structsObj := structs.New(obj)
structsObj.TagName = "json"
return structsObj.Map()
}

View File

@@ -11,7 +11,7 @@ import (
"unicode/utf8"
"git.rosy.net.cn/baseapi"
"github.com/satori/go.uuid"
uuid "github.com/satori/go.uuid"
)
func DictKeysMan(data interface{}, keysToRemove []string, keysToKeep []string) interface{} {

View File

@@ -174,3 +174,14 @@ func TestTrimBlanChar(t *testing.T) {
t.Fatal("TrimBlanChar doesn't work")
}
}
func TestStruct2MapByJson(t *testing.T) {
mapData := Struct2MapByJson(&struct {
IntData int `structs:"dataInt"`
StrData string `json:"dataStr"`
}{
IntData: 1,
StrData: "2",
})
t.Log(mapData)
}