65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
|
package model
|
||
|
|
||
|
import (
|
||
|
"database/sql/driver"
|
||
|
"errors"
|
||
|
"gorm.io/datatypes"
|
||
|
)
|
||
|
|
||
|
type Integration struct {
|
||
|
ID uint64 `gorm:"primaryKey; autoIncrement;"`
|
||
|
Type IntegrationType `gorm:"column:type; not null"`
|
||
|
ChatID uint64 `gorm:"column:chat_id; not null"`
|
||
|
Params datatypes.JSONMap `gorm:"column:params; not null"`
|
||
|
}
|
||
|
|
||
|
func (Integration) TableName() string {
|
||
|
return "integration"
|
||
|
}
|
||
|
|
||
|
type IntegrationType uint8
|
||
|
|
||
|
const (
|
||
|
InvalidIntegration IntegrationType = iota
|
||
|
RedmineIntegration
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
integrationTypesToIds = map[string]IntegrationType{
|
||
|
"invalid": InvalidIntegration,
|
||
|
"redmine": RedmineIntegration,
|
||
|
}
|
||
|
idsToIntegrationTypes = map[IntegrationType]string{
|
||
|
InvalidIntegration: "invalid",
|
||
|
RedmineIntegration: "redmine",
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func (it *IntegrationType) 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 IntegrationType:
|
||
|
*it = v
|
||
|
case string:
|
||
|
// lookup the value from the map and assign it
|
||
|
val, ok := integrationTypesToIds[v]
|
||
|
if !ok {
|
||
|
return errors.New("invalid IntegrationType")
|
||
|
}
|
||
|
*it = val
|
||
|
default:
|
||
|
return errors.New("invalid type")
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (it IntegrationType) Value() (driver.Value, error) {
|
||
|
val, ok := idsToIntegrationTypes[it]
|
||
|
if ok {
|
||
|
return val, nil
|
||
|
}
|
||
|
return "", errors.New("invalid IntegrationType")
|
||
|
}
|