【问题标题】:How to specify a text column's value as a string union?如何将文本列的值指定为字符串联合?
【发布时间】:2021-08-25 18:23:30
【问题描述】:

我有一个结构 Document:

type Document struct {
    ID               entity.EntityID
    Type             string
    ContentType      files.MIMEType
    URL              string
    UploadedByUserID entity.EntityID
    CreatedAt        time.Time
}

type MIMEType string

const (
    MIMETypeJPG MIMEType = "image/jpeg"
    MIMETypePDF MIMEType = "application/pdf"
    MIMETypePNG MIMEType = "image/png"
    MIMETypeTXT MIMEType = "text/plain"
)

它映射到ContentType 列的varchar(1024) 类型的documents 表:

create table documents(
    id bigint not null default nextval('documents_id_seq') PRIMARY KEY,
    url varchar(1024) not null,
    type varchar(1024) not null,
    content_type varchar(1024) not null,
    uploaded_by_user_id bigint,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP not null
);

Create(&d) 其中dDocument 实例给我一个错误:

[2.530ms] [rows:1] INSERT INTO "documents" ("type","content_type","url","uploaded_by_user_id","created_at") VALUES ('other_freight','image/png','https://lateralline-documents-dev.s3.us-west-2.amazonaws.com/doc-other_freight-2021_08_25__11_15_13_379569000.png','253608954016301056','2021-08-25 11:15:13.82') RETURNING "id"
interface conversion: interface {} is files.MIMEType, not string

我想告诉 gorm,当我读写 documents.content_type 字段时,它不仅仅是一个任意字符串,它应该是一个 files.MIMEType 值。有没有办法做到这一点?我在其他 ORM 中看到过这个功能。

【问题讨论】:

    标签: go go-gorm


    【解决方案1】:

    您可能想查看go-gorm 中的custom types(或查看this 答案)。

    简而言之,您需要为您的MIMEType 结构实现ScannerValuer 接口。

    func (m *MIMEType) Scan(value interface{}) error {
      val, ok := value.(string)
      if !ok {
        return errors.New(fmt.Sprint("Failed to unmarshal string value:", value))
      }
      
      *m = MIMEType(val)
      return nil
    }
    
    func (m MIMEType) Value() (driver.Value, error) {
      return string(m), nil
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-07
      • 2022-01-08
      • 2016-01-04
      • 1970-01-01
      • 2021-09-11
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多