feature: 增加basetype 兼容低版本数据库的json不兼容问题

This commit is contained in:
pixelmaxQM
2024-09-13 21:02:15 +08:00
parent e9e42be139
commit c72ed970fb
2 changed files with 48 additions and 13 deletions
+35
View File
@@ -0,0 +1,35 @@
package common
import (
"database/sql/driver"
"encoding/json"
"errors"
)
type JSONMap map[string]interface{}
// 为了兼容无json类型的数据库实现JSONMap类型的Scan和Value方法
func (m JSONMap) Value() (driver.Value, error) {
if m == nil {
return nil, nil
}
return json.Marshal(m)
}
// Scan makes the JSONMap implement the sql.Scanner interface.
// This method decodes a JSON-encoded value into the map structure.
func (m *JSONMap) Scan(value interface{}) error {
if value == nil {
*m = make(map[string]interface{})
return nil
}
bytes, ok := value.([]byte)
if !ok {
return errors.New("Scan source was not []bytes")
}
err := json.Unmarshal(bytes, m)
if err != nil {
return err
}
return nil
}