-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
177 lines (156 loc) · 3.6 KB
/
type.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
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Copyright © Portalnesia <support@portalnesia.com>
*/
package nullable
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"reflect"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
type Type[D any] 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 D
}
func NewType[T any](data T, presentValid ...bool) Type[T] {
d := Type[T]{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 NewTypePtr[T any](data T, presentValid ...bool) *Type[T] {
d := NewType[T](data, presentValid...)
return &d
}
func (d Type[D]) Datatypes() *datatypes.JSONType[D] {
if !d.Present || !d.Valid {
return nil
}
dt := datatypes.NewJSONType(d.Data)
return &dt
}
func (d Type[D]) Ptr() *D {
if d.Valid {
return &d.Data
}
return nil
}
func (d *Type[D]) Scan(value interface{}) error {
d.Present = true
if value == nil {
d.Valid = false
return nil
}
var bytes []byte
switch v := value.(type) {
case []byte:
bytes = v
case string:
bytes = []byte(v)
default:
return errors.New(fmt.Sprint("Failed to unmarshal JSON value:", value))
}
d.Valid = true
return json.Unmarshal(bytes, &d.Data)
}
// Value sql.Value interface
func (d Type[D]) Value() (driver.Value, error) {
if !d.Valid {
return nil, nil
}
return json.Marshal(d.Data)
}
// MarshalJSON implements json.Marshaler interface.
// Bug: Marshal undefined value
func (i Type[D]) MarshalJSON() ([]byte, error) {
if !i.Present {
return []byte(`null`), nil
} else if !i.Valid {
return []byte("null"), nil
}
return json.Marshal(i.Data)
}
// UnmarshalJSON implements json.Marshaler interface.
func (i *Type[D]) UnmarshalJSON(data []byte) error {
i.Present = true
if bytes.Equal(data, []byte("null")) {
return nil
}
if err := json.Unmarshal(data, &i.Data); err != nil {
return err
}
i.Valid = true
return nil
}
// MarshalBSON implements bson.Marshaler interface.
func (i Type[D]) MarshalBSON() ([]byte, error) {
if !i.Present {
return []byte(`null`), nil
} else if !i.Valid {
return []byte("null"), nil
}
return bson.Marshal(i.Data)
}
// UnmarshalBSON implements bson.Marshaler interface.
func (i *Type[D]) UnmarshalBSON(data []byte) error {
i.Present = true
if bytes.Equal(data, []byte("null")) {
return nil
}
if err := bson.Unmarshal(data, &i.Data); err != nil {
return err
}
i.Valid = true
return nil
}
// GormDataType gorm common data type
func (Type[D]) GormDataType() string {
return "json"
}
// GormDBDataType gorm db data type
func (Type[D]) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "sqlite":
return "JSON"
case "mysql":
return "JSON"
case "postgres":
return "JSONB"
}
return ""
}
func (Type[D]) FiberConverter(value string) reflect.Value {
var tmp D
s := Type[D]{true, false, tmp}
if err := json.Unmarshal([]byte(value), &tmp); err != nil {
s = NewType(tmp, true, true)
}
return reflect.ValueOf(s)
}
//func (js Type[T]) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
// if !js.Valid {
// return clause.Expr{SQL: "?", Vars: []any{"NULL"}, WithoutParentheses: true}
// }
//
// data, _ := js.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 gorm.Expr("?", string(data))
//}