“Gorm enum” Kod odpowiedzi

Gorm enum

type RoleAllowed string

const (
	admin    RoleAllowed = "admin"
	merchant RoleAllowed = "merchant"
	outlite  RoleAllowed = "outlite"
	supplier RoleAllowed = "supplier"
)

type ModelRole struct {
	ID         string         `json:"id" gorm:"primary_key"`
	RoleName   RoleAllowed    `json:"role_name" sql:"type:role_name"`
	RoleAccess pq.StringArray `json:"role_access" gorm:"type:text[]; not null"`
	CreatedAt  time.Time      `json:"created_at"`
	UpdatedAt  time.Time      `json:"updated_at"`
}

func (m *ModelRole) BeforeCreate(db *gorm.DB) error {
	m.ID = uuid.New().String()
	m.CreatedAt = time.Now().Local()
	return nil
}

func (m *ModelRole) BeforeUpdate(db *gorm.DB) error {
	m.ID = uuid.New().String()
	m.UpdatedAt = time.Now().Local()
	return nil
}
Restu Wahyu Saputra

Gorm enum

type carType string

const (
    SEDAN  carType = "SEDAN"
    HATCHBACK carType = "HATCHBACK"
    MINIVAN carType = "MINIVAN"
)

func (ct *carType) Scan(value interface{}) error {
    *ct = carType(value.([]byte))
    return nil
}

func (ct carType) Value() (driver.Value, error) {
    return string(ct), nil
}

type MyTable struct {
    gorm.Model
    CarType carType `sql:"car_type"`
}

func (MyTable) TableName() string {
    return "my_table"
}
Restu Wahyu Saputra

Odpowiedzi podobne do “Gorm enum”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu