gendry
gendry copied to clipboard
scan 对于继承的struct不生效
在 scan 时 将 JobDetail 传入时,解析不出来,传入 JobDetail.Job 时可以解析
因为
type JobDetail struct {
Job
}
//等价于
type JobDetail struct {
Job Job
}
目前没有对这种方式做直接处理。解决办法是:自己手动给Job实现ByteUnmarshaler
type ByteUnmarshaler interface {
UnmarshalByte(data []byte) error
}
ByteUnmarshaler参考
type human struct {
Age int `ddb:"ag"`
Extra *extraInfo `ddb:"ext"`
}
type extraInfo struct {
Hobbies []string `json:"hobbies"`
LuckyNumber int `json:"ln"`
}
func (ext *extraInfo) UnmarshalByte(data []byte) error {
return json.Unmarshal(data, ext)
}
//if the type of ext column in a table is varchar(stored legal json string) or json(mysql5.7)
var student human
err := scanner.Scan(rows, &student)
// ...
或者,你先把数据ScanMap,然后再进行处理