Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1157987916 opened this issue Jun 13, 2023 · 1 comment
Open

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

1157987916 opened this issue Jun 13, 2023 · 1 comment

Comments

@1157987916
Copy link

1157987916 commented Jun 13, 2023

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

@caibirdme
Copy link
Contributor

因为

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants