- 添加Struct2MapByJson
- 去掉将全局structs.DefaultTagName设置为"json"
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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{} {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user