I have a Postgres database which stores JSON as one of its fields. The structure of the table is:
type Table struct {
ID int
VehicleType string
Vehicle Vehicle `gorm:"type:jsonb"`
// ......
}
Now, Vehicle is an interface
type Vehicle interface {
GetEngineModel() string
}
It has many implementation, I will share one of them - Car
type Car struct {
CarEngineModel string //the attributes will be different for different
//implementation
}
func (car Car) GetEngineModel() string {
return car.CarEngineModel
}
In order to parse the attributes in a specific struct i.e. Car, Bike, .., I can implement Scan and Value interface of all the implementations something like this :-
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)
}
Is there a way to tell which implementation of Scan to use based on other table columns or an alternate way to do the same using GORM? I only want one table(genetic json type) so don't want to use different tables for different implementations using polymorphic association.
func (*Vehicle) Scan(value interface{})will not compile. - mkopriva