You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
package gopostgres
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql/driver"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type JSON json.RawMessage
|
|
|
|
|
|
|
|
|
|
// Scan 实现 sql.Scanner 接口,Scan 将 value 扫描至 Jsonb
|
|
|
|
|
func (j *JSON) Scan(value interface{}) error {
|
|
|
|
|
bytes, ok := value.([]byte)
|
|
|
|
|
if !ok {
|
|
|
|
|
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := json.RawMessage{}
|
|
|
|
|
err := json.Unmarshal(bytes, &result)
|
|
|
|
|
*j = JSON(result)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Value 实现 driver.Valuer 接口,Value 返回 json value
|
|
|
|
|
func (j JSON) Value() (driver.Value, error) {
|
|
|
|
|
if len(j) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return json.RawMessage(j).MarshalJSON()
|
|
|
|
|
}
|