Describe the feature
问题: 是否能够支持一种注入方法,实现自定义类型解析原生数据类型,不通过Scan和Value方式。
在proto中,定义一个message只能用一些基本类型,比如 repeated map 等对象,假如我有一个消息定义如下
message user{
uint32 iD = 1;
map<string,string> contacts = 2;
repeated string old_names = 3;
}
生产go type后
type User struct{
ID uint32
Contracts map[string]string `gorm:"type:jsobb"`
OldNames []string `gorm:"type:jsobb"`
}
Motivation
那么问题来了
Contracts map[string]string, OldNames []string 内为原生类型,无法在其上设置Valuer和Scan接口,于是,我必须定义两个类
type StringMap map[string]string
func (x *StringMap) Scan(value interface{}) error {...}
func (x *StringMap) Value() (driver.Value, error) {
type StringSlice []string
func (x *StringSlice) Scan(value interface{}) error {...}
func (x *StringSlice) Value() (driver.Value, error) {...}
然后,重新建User类包装一下
type UserWrap struct{
*User
Contracts StringMap `gorm:"type:jsobb"`
OldNames StringSlice `gorm:"type:jsobb"`
}
并修改原来的User类,修gorm相关tag
type User struct{
ID uint32
Contracts map[string]string `gorm:"-"`
OldNames []string `gorm:"-"`
}
这样才能使用gorm
Related Issues
Comment From: labulakalia
你可以使用 https://github.com/favadi/protoc-go-inject-tag 来设置 gorm:"-" tag 来忽略这个字段
Comment From: piyongcai
解决了 timestamppb,但是原生slice,map的问题,还是没有很好的解决。
Comment From: jinzhu
duplicated withhttps://github.com/go-gorm/gorm/issues/4864
Comment From: jinzhu
Added its support, waiting for the release, checkout the test case.
https://github.com/go-gorm/gorm/blob/serializer/tests/serializer_test.go