chore: 初始化提交
This commit is contained in:
commit
943c0a0e5a
71 files changed
+7122
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// JSONList 以文本列存储 JSON 数组,序列化到 HTTP 响应时保持数组形态。
|
||||
type JSONList json.RawMessage
|
||||
|
||||
func (j JSONList) MarshalJSON() ([]byte, error) {
|
||||
if len(j) == 0 {
|
||||
return []byte("[]"), nil
|
||||
}
|
||||
return json.RawMessage(j), nil
|
||||
}
|
||||
|
||||
func (j JSONList) Value() (driver.Value, error) {
|
||||
if len(j) == 0 {
|
||||
return "[]", nil
|
||||
}
|
||||
return string(j), nil
|
||||
}
|
||||
|
||||
func (j *JSONList) Scan(src any) error {
|
||||
switch value := src.(type) {
|
||||
case nil:
|
||||
*j = nil
|
||||
case []byte:
|
||||
*j = append((*j)[:0], value...)
|
||||
case string:
|
||||
*j = append((*j)[:0], value...)
|
||||
default:
|
||||
return fmt.Errorf("JSONList 无法解析 %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// JSONMap 以文本列存储 JSON 对象。
|
||||
type JSONMap map[string]any
|
||||
|
||||
func (m JSONMap) Value() (driver.Value, error) {
|
||||
if m == nil {
|
||||
return "null", nil
|
||||
}
|
||||
encoded, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return string(encoded), nil
|
||||
}
|
||||
|
||||
func (m *JSONMap) Scan(src any) error {
|
||||
var raw []byte
|
||||
switch value := src.(type) {
|
||||
case nil:
|
||||
*m = nil
|
||||
return nil
|
||||
case []byte:
|
||||
raw = value
|
||||
case string:
|
||||
raw = []byte(value)
|
||||
default:
|
||||
return fmt.Errorf("JSONMap 无法解析 %T", src)
|
||||
}
|
||||
|
||||
if len(raw) == 0 {
|
||||
*m = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
var decoded map[string]any
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
return err
|
||||
}
|
||||
*m = decoded
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user