@@ -2,6 +2,8 @@ package value
2
2
3
3
import (
4
4
"database/sql/driver"
5
+ "encoding/json"
6
+ "errors"
5
7
"reflect"
6
8
"testing"
7
9
"time"
@@ -25,6 +27,28 @@ func unwrapPtr(v interface{}) interface{} {
25
27
return reflect .ValueOf (v ).Elem ().Interface ()
26
28
}
27
29
30
+ type jsonUnmarshaller struct {
31
+ bytes []byte
32
+ }
33
+
34
+ func (json * jsonUnmarshaller ) UnmarshalJSON (bytes []byte ) error {
35
+ json .bytes = bytes
36
+
37
+ return nil
38
+ }
39
+
40
+ var _ json.Unmarshaler = & jsonUnmarshaller {}
41
+
42
+ type jsonUnmarshallerBroken struct {
43
+ bytes []byte
44
+ }
45
+
46
+ func (json * jsonUnmarshallerBroken ) UnmarshalJSON (_ []byte ) error {
47
+ return errors .New ("unmarshal error" )
48
+ }
49
+
50
+ var _ json.Unmarshaler = & jsonUnmarshallerBroken {}
51
+
28
52
func loadLocation (t * testing.T , name string ) * time.Location {
29
53
loc , err := time .LoadLocation (name )
30
54
require .NoError (t , err )
@@ -138,6 +162,75 @@ func TestCastTo(t *testing.T) {
138
162
err : ErrCannotCast ,
139
163
},
140
164
165
+ // JSONValue
166
+ {
167
+ name : xtest .CurrentFileLine (),
168
+ value : JSONValue (`{"test": "text"}"` ),
169
+ dst : ptr [string ](),
170
+ exp : `{"test": "text"}"` ,
171
+ err : nil ,
172
+ },
173
+ {
174
+ name : xtest .CurrentFileLine (),
175
+ value : JSONValue (`{"test":"text"}"` ),
176
+ dst : ptr [Value ](),
177
+ exp : JSONValue (`{"test":"text"}"` ),
178
+ err : nil ,
179
+ },
180
+ {
181
+ name : xtest .CurrentFileLine (),
182
+ value : OptionalValue (JSONValue (`{"test": "text"}"` )),
183
+ dst : ptr [* []byte ](),
184
+ exp : value2ptr ([]byte (`{"test": "text"}"` )),
185
+ err : nil ,
186
+ },
187
+ {
188
+ name : xtest .CurrentFileLine (),
189
+ value : JSONValue (`{"test":"text"}"` ),
190
+ dst : ptr [[]byte ](),
191
+ exp : []byte (`{"test":"text"}"` ),
192
+ err : nil ,
193
+ },
194
+ {
195
+ name : xtest .CurrentFileLine (),
196
+ value : JSONValue (`{"test": "text"}"` ),
197
+ dst : ptr [jsonUnmarshaller ](),
198
+ exp : jsonUnmarshaller {[]byte (`{"test": "text"}"` )},
199
+ err : nil ,
200
+ },
201
+ {
202
+ name : xtest .CurrentFileLine (),
203
+ value : JSONValue (`{"test": "text"}"` ),
204
+ dst : ptr [jsonUnmarshallerBroken ](),
205
+ err : ErrCannotCast ,
206
+ },
207
+ {
208
+ name : xtest .CurrentFileLine (),
209
+ value : OptionalValue (JSONValue (`{"test": "text"}"` )),
210
+ dst : ptr [jsonUnmarshaller ](),
211
+ exp : jsonUnmarshaller {[]byte (`{"test": "text"}"` )},
212
+ err : nil ,
213
+ },
214
+ {
215
+ name : xtest .CurrentFileLine (),
216
+ value : OptionalValue (JSONValue (`{"test": "text"}"` )),
217
+ dst : ptr [jsonUnmarshallerBroken ](),
218
+ err : ErrCannotCast ,
219
+ },
220
+ {
221
+ name : xtest .CurrentFileLine (),
222
+ value : JSONValue (`{"test": "text"}"` ),
223
+ dst : ptr [int ](),
224
+ err : ErrCannotCast ,
225
+ },
226
+ {
227
+ name : xtest .CurrentFileLine (),
228
+ value : OptionalValue (JSONValue (`{"test": "text"}"` )),
229
+ dst : ptr [int ](),
230
+ err : ErrCannotCast ,
231
+ },
232
+
233
+ // JSONDocumentValue
141
234
{
142
235
name : xtest .CurrentFileLine (),
143
236
value : JSONDocumentValue (`{"test": "text"}"` ),
@@ -166,6 +259,44 @@ func TestCastTo(t *testing.T) {
166
259
exp : []byte (`{"test":"text"}"` ),
167
260
err : nil ,
168
261
},
262
+ {
263
+ name : xtest .CurrentFileLine (),
264
+ value : JSONDocumentValue (`{"test": "text"}"` ),
265
+ dst : ptr [jsonUnmarshaller ](),
266
+ exp : jsonUnmarshaller {[]byte (`{"test": "text"}"` )},
267
+ err : nil ,
268
+ },
269
+ {
270
+ name : xtest .CurrentFileLine (),
271
+ value : JSONDocumentValue (`{"test": "text"}"` ),
272
+ dst : ptr [jsonUnmarshallerBroken ](),
273
+ err : ErrCannotCast ,
274
+ },
275
+ {
276
+ name : xtest .CurrentFileLine (),
277
+ value : OptionalValue (JSONDocumentValue (`{"test": "text"}"` )),
278
+ dst : ptr [jsonUnmarshaller ](),
279
+ exp : jsonUnmarshaller {[]byte (`{"test": "text"}"` )},
280
+ err : nil ,
281
+ },
282
+ {
283
+ name : xtest .CurrentFileLine (),
284
+ value : OptionalValue (JSONDocumentValue (`{"test": "text"}"` )),
285
+ dst : ptr [jsonUnmarshallerBroken ](),
286
+ err : ErrCannotCast ,
287
+ },
288
+ {
289
+ name : xtest .CurrentFileLine (),
290
+ value : JSONDocumentValue (`{"test": "text"}"` ),
291
+ dst : ptr [int ](),
292
+ err : ErrCannotCast ,
293
+ },
294
+ {
295
+ name : xtest .CurrentFileLine (),
296
+ value : OptionalValue (JSONDocumentValue (`{"test": "text"}"` )),
297
+ dst : ptr [int ](),
298
+ err : ErrCannotCast ,
299
+ },
169
300
170
301
{
171
302
name : xtest .CurrentFileLine (),
0 commit comments