forked from fabric8-services/fabric8-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_storage.go
48 lines (38 loc) · 832 Bytes
/
json_storage.go
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
42
43
44
45
46
47
48
package models
import (
"database/sql/driver"
"encoding/json"
"errors"
)
type Fields map[string]interface{}
func (j Fields) Value() (driver.Value, error) {
return toBytes(j);
}
func (j *Fields) Scan(src interface{}) error {
return fromBytes(src, j)
}
func (j FieldDefinition) Value() (driver.Value, error) {
return toBytes(j);
}
func (j *FieldDefinition) Scan(src interface{}) error {
return fromBytes(src, j)
}
func toBytes(j interface{}) (driver.Value, error) {
if j == nil {
// log.Trace("returning null")
return nil, nil
}
res, error := json.Marshal(j)
return res, error
}
func fromBytes(src interface{}, target interface{}) error {
if src == nil {
target = nil
return nil
}
s, ok := src.([]byte)
if !ok {
return errors.New("Scan source was not string")
}
return json.Unmarshal(s, target)
}