-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_array.go
164 lines (143 loc) · 3.39 KB
/
string_array.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package nullable
import (
"bytes"
"database/sql/driver"
"encoding/json"
pg "github.com/lib/pq"
"go.mongodb.org/mongo-driver/bson"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"reflect"
)
type StringArray struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid string
Data pg.StringArray
}
func NewStringArray(data pg.StringArray, presentValid ...bool) StringArray {
d := StringArray{Present: true, Valid: true, Data: data}
if len(presentValid) > 0 {
d.Present = presentValid[0]
d.Valid = false
if len(presentValid) > 1 {
d.Valid = presentValid[1]
}
}
return d
}
func NewStringArrayPtr(data pg.StringArray, presentValid ...bool) *StringArray {
d := NewStringArray(data, presentValid...)
return &d
}
func (d StringArray) Ptr() *pg.StringArray {
if d.Valid {
return &d.Data
}
return nil
}
func (d *StringArray) Scan(value interface{}) error {
d.Present = true
if value == nil {
d.Valid = false
return nil
}
var temp pg.StringArray
if err := temp.Scan(value); err != nil {
d.Valid = false
return err
}
if temp != nil {
d.Valid = true
}
d.Data = temp
return nil
}
// Value sql.Value interface
func (d StringArray) Value() (driver.Value, error) {
if !d.Valid || len(d.Data) == 0 {
return nil, nil
}
return d.Data.Value()
}
// MarshalJSON implements json.Marshaler interface.
func (d StringArray) MarshalJSON() ([]byte, error) {
if !d.Present {
return []byte(`null`), nil
} else if !d.Valid {
return []byte(`null`), nil
}
return json.Marshal(d.Data)
}
// UnmarshalJSON implements json.Marshaler interface.
func (d *StringArray) UnmarshalJSON(data []byte) error {
d.Present = true
if bytes.Equal(data, []byte("null")) {
return nil
}
if err := json.Unmarshal(data, &d.Data); err != nil {
return err
}
if len(d.Data) > 0 {
d.Valid = true
}
return nil
}
// MarshalBSON implements bson.Marshaler interface.
func (d StringArray) MarshalBSON() ([]byte, error) {
if !d.Present {
return []byte(`null`), nil
} else if !d.Valid {
return []byte(`null`), nil
}
_, byt, err := bson.MarshalValue(d.Data)
return byt, err
}
// UnmarshalBSON implements bson.Marshaler interface.
func (d *StringArray) UnmarshalBSON(data []byte) error {
d.Present = true
if bytes.Equal(data, []byte("null")) {
return nil
}
if err := bson.Unmarshal(data, &d.Data); err != nil {
return err
}
if len(d.Data) > 0 {
d.Valid = true
}
return nil
}
// GormDataType gorm common data type
func (StringArray) GormDataType() string {
return "json"
}
// GormDBDataType gorm db data type
func (StringArray) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "sqlite":
return "JSON"
case "mysql":
return "JSON"
case "postgres":
return "text[]"
}
return ""
}
func (StringArray) FiberConverter(value string) reflect.Value {
var tmp pg.StringArray
s := StringArray{true, false, pg.StringArray{}}
if err := json.Unmarshal([]byte(value), &tmp); err != nil {
s = NewStringArray(tmp, true, true)
}
return reflect.ValueOf(s)
}
//func (d StringArray) GormValue(_ context.Context, db *gorm.DB) clause.Expr {
// data, _ := d.MarshalJSON()
// switch db.Dialector.Name() {
// case "mysql":
// if v, ok := db.Dialector.(*mysql.Dialector); ok && !strings.Contains(v.ServerVersion, "MariaDB") {
// return gorm.Expr("CAST(? AS JSON)", string(data))
// }
// }
//
// return clause.Expr{}
//}