forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
56 lines (48 loc) · 976 Bytes
/
map.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
49
50
51
52
53
54
55
56
package slices
import (
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
type Map map[string]interface{}
func (m Map) Interface() interface{} {
return map[string]interface{}(m)
}
func (s *Map) Scan(src interface{}) error {
b, ok := src.([]byte)
if !ok {
return errors.New("Scan source was not []byte")
}
err := json.Unmarshal(b, s)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (s Map) Value() (driver.Value, error) {
b, err := json.Marshal(s)
if err != nil {
return nil, errors.WithStack(err)
}
return string(b), nil
}
func (m Map) UnmarshalJSON(b []byte) error {
var stuff map[string]interface{}
err := json.Unmarshal(b, &stuff)
if err != nil {
return err
}
for key, value := range stuff {
m[key] = value
}
return nil
}
func (s Map) UnmarshalText(text []byte) error {
fmt.Println(string(text))
err := json.Unmarshal(text, &s)
if err != nil {
return errors.WithStack(err)
}
return nil
}