-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsqlobject.go
More file actions
41 lines (31 loc) · 709 Bytes
/
sqlobject.go
File metadata and controls
41 lines (31 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package schemaless
import (
"database/sql/driver"
"encoding/json"
)
// SQLObject - implements a driver custom type for postgress
type SQLObject map[string]interface{}
// JSON - return the json string of the object
func (o SQLObject) JSON() string {
j, _ := json.Marshal(o)
return string(j)
}
// Value - implements driver.Value
func (o SQLObject) Value() (driver.Value, error) {
jsonBytes, err := json.Marshal(o)
return jsonBytes, err
}
// Scan - implements driver.Scan
func (o *SQLObject) Scan(src interface{}) error {
source, ok := src.([]byte)
if !ok {
return nil
}
var v map[string]interface{}
err := json.Unmarshal(source, &v)
if err != nil {
return err
}
*o = v
return nil
}