-
Notifications
You must be signed in to change notification settings - Fork 22
/
json.go
192 lines (165 loc) · 4.05 KB
/
json.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package schema
import (
"bytes"
"encoding/json"
"errors"
"reflect"
)
type JSONTransformer interface {
TransformJSON(*JSON) any
}
type JSON struct {
Bytes []byte
Status Status
}
func (dst *JSON) GetStatus() Status {
return dst.Status
}
func (*JSON) Type() ValueType {
return TypeJSON
}
func (dst *JSON) Size() int {
return len(dst.Bytes)
}
// JSONBytesEqual compares the JSON in two byte slices.
func jsonBytesEqual(a, b []byte) (bool, error) {
var j, j2 any
if err := json.Unmarshal(a, &j); err != nil {
return false, err
}
if err := json.Unmarshal(b, &j2); err != nil {
return false, err
}
return reflect.DeepEqual(j2, j), nil
}
func (dst *JSON) Equal(src CQType) bool {
if src == nil {
return false
}
s, ok := src.(*JSON)
if !ok {
return false
}
if dst.Status == s.Status && dst.Status != Present {
return true
}
if dst.Status != s.Status {
return false
}
equal, err := jsonBytesEqual(dst.Bytes, s.Bytes)
if err != nil {
return false
}
return equal
}
func (dst *JSON) String() string {
return string(dst.Bytes)
}
func (dst *JSON) Set(src any) error {
if src == nil {
*dst = JSON{Status: Null}
return nil
}
if value, ok := src.(interface{ Get() any }); ok {
value2 := value.Get()
if value2 != value {
return dst.Set(value2)
}
}
switch value := src.(type) {
case string:
if value == "" {
*dst = JSON{Bytes: []byte(""), Status: Null}
return nil
}
if !json.Valid([]byte(value)) {
return errors.New("invalid JSON string value")
}
*dst = JSON{Bytes: []byte(value), Status: Present}
case *string:
if value == nil {
*dst = JSON{Status: Null}
} else {
if *value == "" {
*dst = JSON{Bytes: []byte(""), Status: Null}
return nil
}
if !json.Valid([]byte(*value)) {
return errors.New("invalid JSON string pointer value")
}
*dst = JSON{Bytes: []byte(*value), Status: Present}
}
case []byte:
if value == nil {
*dst = JSON{Status: Null}
} else {
if string(value) == "" {
*dst = JSON{Bytes: []byte(""), Status: Null}
return nil
}
if !json.Valid(value) {
return errors.New("invalid JSON byte array value")
}
*dst = JSON{Bytes: value, Status: Present}
}
// Encode* methods are defined on *JSON. If JSON is passed directly then the
// struct itself would be encoded instead of Bytes. This is clearly a footgun
// so detect and return an error. See https://github.com/jackc/pgx/issues/350.
case JSON:
return errors.New("use pointer to JSON instead of value")
default:
buf, err := json.Marshal(value)
if err != nil {
return err
}
// For map and slice jsons, it is easier for users to work with '[]' or '{}' instead of JSON's 'null'.
if bytes.Equal(buf, []byte(`null`)) {
if isEmptyStringMap(value) {
*dst = JSON{Bytes: []byte("{}"), Status: Present}
return nil
}
if isEmptySlice(value) {
*dst = JSON{Bytes: []byte("[]"), Status: Present}
return nil
}
}
*dst = JSON{Bytes: buf, Status: Present}
}
return nil
}
func (dst JSON) Get() any {
switch dst.Status {
case Present:
var i any
err := json.Unmarshal(dst.Bytes, &i)
if err != nil {
return dst
}
return i
case Null:
return nil
default:
return dst.Status
}
}
// isEmptyStringMap returns true if the value is a map from string to any (i.e. map[string]any).
// We need to use reflection for this, because it impossible to type-assert a map[string]string into a
// map[string]any. See https://go.dev/doc/faq#convert_slice_of_interface.
func isEmptyStringMap(value any) bool {
if reflect.TypeOf(value).Kind() != reflect.Map {
return false
}
if reflect.TypeOf(value).Key().Kind() != reflect.String {
return false
}
return reflect.ValueOf(value).Len() == 0
}
// isEmptySlice returns true if the value is a slice (i.e. []any).
// We need to use reflection for this, because it impossible to type-assert a map[string]string into a
// map[string]any. See https://go.dev/doc/faq#convert_slice_of_interface.
func isEmptySlice(value any) bool {
if reflect.TypeOf(value).Kind() != reflect.Slice {
return false
}
return reflect.ValueOf(value).Len() == 0
}