Golang中結構體映射mapstructure庫深入詳解
在數(shù)據(jù)傳遞時,需要先編解碼;常用的方式是JSON編解碼(參見《golang之JSON處理》)。但有時卻需要讀取部分字段后,才能知道具體類型,此時就可借助mapstructure庫了。
mapstructure庫
mapstructure可方便地實現(xiàn)map[string]interface{}與struct間的轉換;使用前,需要先導入庫:
go get github.com/mitchellh/mapstructure
字段標簽
默認情況下,mapstructure使用字段的名稱做匹配映射(即在map中以字段名為鍵值查找字段值);注意匹配時是忽略大小寫的。也可通過標簽來設定字段映射名稱:
type Person struct {
Name string `mapstructure:"userName"`
}
內(nèi)嵌結構
go中結構體是可以任意嵌套的;嵌套后即認為擁有對應的字段。但是,默認情況下mapstructure只處理當前結構定義的字段,若要自動處理內(nèi)嵌字段需要添加標簽squash:
type Student struct {
Person `mapstructure:",squash"`
Age int
}
未映射字段
若源數(shù)據(jù)中有未映射的值(即結構體中無對應的字段),mapstructure默認會忽略它??梢栽诮Y構體中定義一個特殊字段(類型為map[string]interface{},且標簽要設置為mapstructure:",remain"),來存放所有未能映射的字段中。
type Student struct {
Name string
Age int
Other map[string]interface{} `mapstructure:",remain"`
}
Metadata
mapstructure中可以使用Metadata收集一些解碼時會產(chǎn)生的有用信息。
// mapstructure.go
type Metadata struct {
Keys []string // 解碼成功的鍵
Unused []string // 源數(shù)據(jù)中存在,但目標結構中不存在的鍵
Unset []string // 未設定的(源數(shù)據(jù)中缺失的)鍵
}
為了獲取這些信息,需要使用DecodeMetadata來解碼:
var metadata mapstructure.Metadata
err := mapstructure.DecodeMetadata(m, &p, &metadata)
弱類型輸入
有時候,并不想對結構體字段類型和map[string]interface{}的對應鍵值做強類型一致的校驗。這時可以使用WeakDecode/WeakDecodeMetadata方法,它們會嘗試做類型轉換:
- 布爾轉字符串:true = “1”, false = “0”;
- 布爾轉數(shù)字:true = 1, false = 0;
- 數(shù)字轉布爾:true if value != 0;
- 字符串轉布爾:可接受,
- 真:1, t, T, TRUE, true, True
- 假:0, f, F, FALSE, false, False
- 數(shù)字轉字符串:自動base10轉換;
- 負數(shù)轉為無符號數(shù)(上溢);
- 字符串轉數(shù)字:根據(jù)前綴(如0x等)轉換;
- 空數(shù)組與空map間互轉;
- 單個值轉為切片;
逆向轉換
除將map轉換為結構體外,mapstructure也可以將結構體反向解碼為map[string]interface{}。在反向解碼時,我們可以為某些字段設置mapstructure:“,omitempty”,當這些字段為默認值時,就不會出現(xiàn)在map中:
p := &Student{
Name: "Mike",
Age: 12,
}
var m map[string]interface{}
mapstructure.Decode(p, &m)
解碼器
mapstructure提供了解碼器(Decoder),可靈活方便地控制解碼:
type DecoderConfig struct {
// 若設定,則在任何解碼或類型轉換(設定了WeaklyTypedInput)前調(diào)用;對于設定了squash的內(nèi)嵌字段,整體調(diào)用一次;若返回錯誤,則整個解碼失敗
DecodeHook DecodeHookFunc
// 若設定,則源數(shù)據(jù)中存在未使用字段時,報錯
ErrorUnused bool
// 若設定,則有字段未設定時,報錯
ErrorUnset bool
// 若設定,則在設定字段前先清空(對于map等類型會先清理掉舊數(shù)據(jù))
ZeroFields bool
// 若設定,支持若類型間的轉換
WeaklyTypedInput bool
// Squash will squash embedded structs.
Squash bool
// Metadata is the struct that will contain extra metadata about
// the decoding. If this is nil, then no metadata will be tracked.
Metadata *Metadata
// Result is a pointer to the struct that will contain the decoded
// value.
Result interface{}
// The tag name that mapstructure reads for field names. This
// defaults to "mapstructure"
TagName string
// IgnoreUntaggedFields ignores all struct fields without explicit
// TagName, comparable to `mapstructure:"-"` as default behaviour.
IgnoreUntaggedFields bool
// MatchName is the function used to match the map key to the struct
// field name or tag. Defaults to `strings.EqualFold`. This can be used
// to implement case-sensitive tag values, support snake casing, etc.
MatchName func(mapKey, fieldName string) bool
}一個支持弱類型轉換的示例:要獲取的結果放到config的result中
Name string
Age int
}
func decoderConfig() {
m := map[string]interface{}{
"name": 123,
"age": "12",
"job": "programmer",
}
var p Person
var metadata mapstructure.Metadata
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: &p,
Metadata: &metadata,
})
if err != nil {
log.Fatal(err)
}
err = decoder.Decode(m)
if err == nil {
log.Printf("Result: %#v", p)
log.Printf("keys:%#v, unused:%#v\n", metadata.Keys, metadata.Unused)
} else {
log.Println("decode fail:", err)
}
}
示例
通過一個messageData結構,action會指示最終的data類型。接收到數(shù)據(jù)后,先解析出atcion,再根據(jù)action轉換為真實的類型。
因time.Time是一個結構體(json序列化時會轉換為時間字符串),mapstructure無法正確處理,所以推薦使用時間戳。
為了能正確解析內(nèi)嵌的DataBasic,需要標記為squash。
import "github.com/mitchellh/mapstructure"
type DataBasic struct {
DataId string `json:"dataId"`
UpdateTime int64 `json:"updateTime"`
}
type AddedData struct {
DataBasic `mapstructure:",squash"`
Tag string `json:"tag"`
AddParams map[string]any `json:"addParams"`
}
type messageData struct {
Action int `json:"action"`
SeqId uint64 `json:"seqId"`
Data any `json:"data"`
}
func decodeData() {
add := &AddedData{
DataBasic: DataBasic{
DataId: "a2",
UpdateTime: time.Now().UnixMilli(),
},
Tag: "tag",
AddParams: map[string]any{"dataId": "c2", "otherId": "t2"},
}
data := &messageData{
Action: 1,
Data: add,
}
js, err := json.Marshal(data)
if err != nil {
log.Printf("marshal fail: %v", err)
return
}
got := &messageData{}
err = json.Unmarshal(js, got)
if err != nil {
log.Printf("unmarshal fail: %v", err)
return
}
param := new(AddedData)
err = mapstructure.Decode(got.Data, param)
if err != nil {
log.Printf("unmarshal fail: %v", err)
return
}
log.Printf("param: %+v", param)
}
到此這篇關于Golang中結構體映射mapstructure庫深入詳解的文章就介紹到這了,更多相關Go mapstructure內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MacOS中 VSCode 安裝 GO 插件失敗問題的快速解決方法
這篇文章主要介紹了MacOS中 VSCode 安裝 GO 插件失敗問題的快速解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05

