-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring.go
49 lines (39 loc) · 795 Bytes
/
string.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
package jsons
import (
"database/sql/driver"
"encoding/json"
"errors"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func (s String) Value() (driver.Value, error) {
return json.Marshal(s)
}
func (s *String) Scan(v interface{}) error {
if v == nil {
*s = ""
return nil
}
switch val := v.(type) {
case []byte:
return json.Unmarshal(val, s)
case string:
return json.Unmarshal([]byte(val), s)
}
return errors.New("invalid scan string source")
}
func (String) GormDBDataType(*gorm.DB, *schema.Field) string {
return "json"
}
func (s String) Raw() Raw {
return s.JSONValue().Raw()
}
func (s String) JSON() []byte {
return s.JSONValue().JSON()
}
func (s String) JSONValue() Value {
return value(s)
}
func (s String) JSONString() string {
return s.JSONValue().JSONString()
}