mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-05-04 11:01:26 +08:00
36
kit/model/jsonb.go
Normal file
36
kit/model/jsonb.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type JSONB map[string]interface{}
|
||||
|
||||
func (a JSONB) Value() (driver.Value, error) {
|
||||
return json.Marshal(a)
|
||||
}
|
||||
|
||||
func (a *JSONB) Scan(value interface{}) error {
|
||||
b, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New("type assertion to []byte failed")
|
||||
}
|
||||
return json.Unmarshal(b, &a)
|
||||
}
|
||||
|
||||
// JSONBS Interface for JSONB Field of yourTableName Table
|
||||
type JSONBS []interface{}
|
||||
|
||||
func (a JSONBS) Value() (driver.Value, error) {
|
||||
return json.Marshal(a)
|
||||
}
|
||||
|
||||
func (a *JSONBS) Scan(value interface{}) error {
|
||||
b, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New("type assertion to []byte failed")
|
||||
}
|
||||
return json.Unmarshal(b, &a)
|
||||
}
|
||||
Reference in New Issue
Block a user