Skip to content

Commit

Permalink
fixed: 对JSONMap进行多数据库不同断言兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelmaxQm committed Sep 18, 2024
1 parent 66bcd32 commit 0fd9e95
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions server/model/common/basetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@ import (

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")
var err error
switch value.(type) {
case []byte:
err = json.Unmarshal(value.([]byte), m)
case string:
err = json.Unmarshal([]byte(value.(string)), m)
default:
err = errors.New("basetypes.JSONMap.Scan: invalid value type")
}
err := json.Unmarshal(bytes, m)
if err != nil {
return err
}
Expand Down

0 comments on commit 0fd9e95

Please sign in to comment.