mirror of https://github.com/go-gorm/gorm.git
add JSONB type (#1626)
* add JSONB type * add comments to satisfy gofmt
This commit is contained in:
parent
9c9de89686
commit
0a51f6cdc5
|
@ -6,6 +6,9 @@ import (
|
|||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/lib/pq/hstore"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Hstore map[string]*string
|
||||
|
@ -52,3 +55,23 @@ func (h *Hstore) Scan(value interface{}) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Jsonb Postgresql's JSONB data type
|
||||
type Jsonb struct {
|
||||
json.RawMessage
|
||||
}
|
||||
|
||||
// Value get value of Jsonb
|
||||
func (j Jsonb) Value() (driver.Value, error) {
|
||||
return j.MarshalJSON()
|
||||
}
|
||||
|
||||
// Scan scan value into Jsonb
|
||||
func (j *Jsonb) Scan(value interface{}) error {
|
||||
bytes, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
|
||||
}
|
||||
|
||||
return json.Unmarshal(bytes, j)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue