【问题标题】:Setting Interface as an attribute having many implementations using GORM使用 GORM 将接口设置为具有许多实现的属性
【发布时间】: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 的评论编辑了我的问题

标签: go go-gorm


【解决方案1】:

您可以添加一个单独的字段来保存原始 JSON 数据,然后实现 gorm 特定挂钩来编组/解组该数据。

type Table struct {
    ID          int
    VehicleType string
    Vehicle     Vehicle `gorm:"-"`
    // ...

    VehicleRaw []byte `gorm:"column:vehicle"`
}

func (t *Table) BeforeSave(tx *gorm.DB) (err error) {
    raw, err := json.Marshal(t.Vehicle)
    if err != nil {
        return err
    }
    
    t.VehicleRaw = raw
    return nil
}

func (t *Table) AfterFind(tx *gorm.DB) (err error) {
    switch t.VehicleType {
    case "CAR":
        t.Vehicle = &Car{}
    case "BIKE":
        t.Vehicle = &Bike{}
    }
    return json.Unmarshal(t.VehicleRaw, t.Vehicle)
}

【讨论】:

  • 非常感谢 mkopriva,尝试了您的解决方案,效果很好
猜你喜欢
  • 1970-01-01
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 2021-05-08
相关资源
最近更新 更多