package model import ( "database/sql/driver" "errors" ) type Chat struct { ID uint64 `gorm:"primaryKey; autoIncrement" json:"id"` TelegramID int64 `gorm:"column:telegram_id; not null" json:"telegramId"` KeyboardType KeyboardType `gorm:"column:keyboard_type;"` UserID uint64 `gorm:"column:user_id; not null"` Integrations []Integration `gorm:"foreignKey:ChatID" json:"integrations"` } func (Chat) TableName() string { return "chat" } type KeyboardType uint8 const ( StandardKeyboard KeyboardType = iota StoryPointsKeyboard ) var ( lbTypesToIds = map[string]KeyboardType{ "normal": StandardKeyboard, "sp": StoryPointsKeyboard, } idsToKBTypes = map[KeyboardType]string{ StandardKeyboard: "normal", StoryPointsKeyboard: "sp", } ) func (it *KeyboardType) Scan(value interface{}) error { switch v := value.(type) { case nil: *it = 0 // or whatever default you want to set if the field is null in db return nil case KeyboardType: *it = v case string: // lookup the value from the map and assign it val, ok := lbTypesToIds[v] if !ok { return errors.New("invalid KeyboardType") } *it = val default: return errors.New("invalid type") } return nil } func (it KeyboardType) Value() (driver.Value, error) { val, ok := idsToKBTypes[it] if ok { return val, nil } return InvalidIntegration, nil } func (KeyboardType) GormDataType() string { return "varchar(6)" }