didi / gendry

a golang library for sql builder
Apache License 2.0
1.6k stars 191 forks source link

scan 对于继承的struct不生效 #142

Open 1157987916 opened 1 year ago

1157987916 commented 1 year ago

image image image 在 scan 时 将 JobDetail 传入时,解析不出来,传入 JobDetail.Job 时可以解析

caibirdme commented 1 year ago

因为

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,然后再进行处理