-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
51 lines (41 loc) · 1.11 KB
/
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"database/sql/driver"
"encoding/json"
"errors"
)
// Map : holds a map[string]interface{} value that can be loaded/serialized to a JSONB field
type Map map[string]interface{}
// Value : returns a valid []byte json object
func (m Map) Value() (driver.Value, error) {
return json.Marshal(m)
}
// Scan : serializes the jsonb object to a map[string]interface{}
func (m *Map) Scan(src interface{}) error {
var ok bool
var i interface{}
var source []byte
switch src.(type) {
case string:
source = []byte(src.(string))
case []byte:
source = src.([]byte)
default:
return errors.New("type assertion .([]byte) & .(string) failed")
}
if string(source) == "null" {
source = []byte("{}")
}
err := json.Unmarshal(source, &i)
if err != nil {
return err
}
*m, ok = i.(map[string]interface{})
if !ok {
return errors.New("type assertion .(map[string]interface{}) failed")
}
return nil
}