dongxie9448 2017-11-16 17:23
浏览 148
已采纳

golang在嵌套结构中解码JSON请求并将其作为blob插入DB

I have a nested struct which I am using to decode JSON request.

type Service struct {
    ID       string `json:"id,omitempty" db:"id"`
    Name     string `json:"name" db:"name"`
    Contract struct {
        ServiceTime int    `json:"service_time"`
        Region      string `json:"region"`
    } `json:"contract" db:"contract"`
}

I am using blob type to store Contract in MySQL. To make it work , I would be needing to create a different struct with Contract as string to insert in DB. Can this be done in better way by using single struct only or is there any other elegant way ?

  • 写回答

1条回答 默认 最新

  • douciping4283 2017-11-16 18:25
    关注

    This depends on what you use to talk to the database, but if you're using database/sql and a driver that provides support for this you can have your Contract type implement the Valuer interface.

    type Service struct {
        ID       string    `json:"id,omitempty" db:"id"`
        Name     string    `json:"name" db:"name"`
        Contract Contract `json:"contract" db:"contract"`
    }
    
    type Contract struct {
        ServiceTime int    `json:"service_time"`
        Region      string `json:"region"`
    }
    
    func (c *Contract) Value() (driver.Value, error) {
        if c != nil {
            b, err := json.Marshal(c)
            if err != nil {
                return nil, err
            }
            return string(b), nil
        }
        return nil, nil
    }
    

    And then when you're executing the query just pass in the Service.Contract field and the driver should call the Value method.

    sql := // INSERT INTO ...
    svc := // &Service{ ...
    db.Exec(sql, svc.ID, svc.Name, svc.Contract)
    

    And to be able to retrieve the blob back from db and unmarshal it back into Contract you can have it implement the Scanner interface, again, if a type implements this the driver is expected to call it.

    func (c *Contract) Scan(src interface{}) error {
        var data []byte
        if b, ok := src.([]byte); ok {
            data = b
        } else if s, ok := src.(string); ok {
            data = []byte(s)
        }
        return json.Unmarshal(data, c)
    }
    
    // ...
    
    sql := // SELECT * FROM ...
    svc := // &Service{ ...
    db.QueryRow(sql, 123).Scan(&svc.ID, &svc.Name, &svc.Contract)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算