2 Star 1 Fork 1

mosache / YFrame

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
json.go 656 Bytes
一键复制 编辑 原始数据 按行查看 历史
ヤ沒脩袮兲︶ 提交于 2023-06-09 21:56 . add Y version command
package sql
import (
"database/sql/driver"
"encoding/json"
"errors"
)
type JsonColumn[T any] struct {
Val T
Valid bool
}
func (j *JsonColumn[T]) Value() (driver.Value, error) {
if !j.Valid {
return nil, nil
}
return json.Marshal(j.Val)
}
func (j *JsonColumn[T]) Scan(src any) error {
// int64
// float64
// bool
// []byte
// string
// time.Time
// nil - for NULL values
var bs []byte
switch data := src.(type) {
case string:
bs = []byte(data)
case []byte:
bs = data
default:
return errors.New("unsupported type")
}
err := json.Unmarshal(bs, &j.Val)
if err != nil {
return err
}
return nil
}
Go
1
https://gitee.com/mosache/YFrame.git
git@gitee.com:mosache/YFrame.git
mosache
YFrame
YFrame
v0.1.73

搜索帮助