-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
56 lines (48 loc) · 1.07 KB
/
util.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 models
import (
"database/sql/driver"
"encoding/json"
"errors"
)
// UserSessionInfo ...
type UserSessionInfo struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
// JSONRaw ...
type JSONRaw json.RawMessage
// Value ...
func (j JSONRaw) Value() (driver.Value, error) {
byteArr := []byte(j)
return driver.Value(byteArr), nil
}
// Scan ...
func (j *JSONRaw) Scan(src interface{}) error {
asBytes, ok := src.([]byte)
if !ok {
return error(errors.New("Scan source was not []bytes"))
}
err := json.Unmarshal(asBytes, &j)
if err != nil {
return error(errors.New("Scan could not unmarshal to []string"))
}
return nil
}
// MarshalJSON ...
func (j *JSONRaw) MarshalJSON() ([]byte, error) {
return *j, nil
}
// UnmarshalJSON ...
func (j *JSONRaw) UnmarshalJSON(data []byte) error {
if j == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
*j = append((*j)[0:0], data...)
return nil
}
// DataList ....
type DataList struct {
Data JSONRaw `db:"data" json:"data"`
Meta JSONRaw `db:"meta" json:"meta"`
}