我有一个嵌套结构,用于解码 JSON 请求。
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"`
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 blob 类型在 MySQL 中存储合同。为了使其工作,我需要创建一个不同的结构,将 Contract 作为字符串插入到数据库中。这可以通过仅使用单个结构以更好的方式完成还是有其他优雅的方式?
这取决于您用来与数据库对话的内容,但如果您使用database/sql的是提供支持的驱动程序,您可以让您的Contract类型实现Valuer接口。
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
}
Run Code Online (Sandbox Code Playgroud)
然后当您执行查询时,只需传入该Service.Contract字段,驱动程序应调用该Value方法。
sql := // INSERT INTO ...
svc := // &Service{ ...
db.Exec(sql, svc.ID, svc.Name, svc.Contract)
Run Code Online (Sandbox Code Playgroud)
并且为了能够从 db 检索 blob 并将其解组回Contract您可以让它实现Scanner接口,同样,如果一个类型实现了这个,驱动程序应该调用它。
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7001 次 |
| 最近记录: |