【发布时间】:2021-07-19 07:49:47
【问题描述】:
我有一个 Postgres 数据库,它将 JSON 存储为其字段之一。表结构为:
type Table struct {
ID int
VehicleType string
Vehicle Vehicle `gorm:"type:jsonb"`
// ......
}
现在,Vehicle 是一个接口
type Vehicle interface {
GetEngineModel() string
}
它有很多实现,我将分享其中一个 - Car
type Car struct {
CarEngineModel string //the attributes will be different for different
//implementation
}
func (car Car) GetEngineModel() string {
return car.CarEngineModel
}
为了解析特定结构(即 Car、Bike 等)中的属性,我可以实现所有实现的 Scan 和 Value 接口,如下所示:-
func (car *Car) Scan(value interface{}) error {
//Want to use this implementation of Scan based on VehicleType
//attribute of struct table
b, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
*car = json.Unmarshal(b, &car)
}
有没有办法根据其他表列来判断要使用哪个 Scan 实现,或者使用 GORM 来做同样的事情?我只想要一个表(遗传 json 类型),所以不想使用多态关联为不同的实现使用不同的表。
【问题讨论】:
-
Go 不允许在接口或指向接口类型的指针上声明方法。所以
func (*Vehicle) Scan(value interface{})不会编译。 -
嗨 mkopriva,那么有没有一种方法可以在接口 Vehicle 的所有实现中实现扫描功能,然后以某种方式能够根据表的属性 Vehicletype 判断要使用哪个扫描实现结构
-
我根据 mkopriva 的评论编辑了我的问题