@@ -12,7 +12,7 @@ import (
12
12
"time"
13
13
)
14
14
15
- func TestUnmarshall_attrStringSlice (t * testing.T ) {
15
+ func TestUnmarshal_attrStringSlice (t * testing.T ) {
16
16
out := & Book {}
17
17
tags := []string {"fiction" , "sale" }
18
18
data := map [string ]interface {}{
@@ -249,6 +249,89 @@ func TestUnmarshal_nonNumericID(t *testing.T) {
249
249
}
250
250
}
251
251
252
+ func TestUnmarshal_CustomType (t * testing.T ) {
253
+ // given
254
+ // register the custom `UUID` type
255
+ uuidType := reflect .TypeOf (UUID {})
256
+ RegisterType (uuidType ,
257
+ func (value interface {}) (string , error ) {
258
+ return value .(UUID ).String (), nil
259
+ },
260
+ func (value string ) (interface {}, error ) {
261
+ result , err := UUIDFromString (value )
262
+ if err != nil {
263
+ fmt .Println ("Error while converting from string to UUID: " + err .Error ())
264
+ return nil , err
265
+ }
266
+ return * result , nil
267
+ })
268
+ in := sampleModelCustomType ()
269
+ // when
270
+ out := new (ModelWithUUIDs )
271
+ if err := UnmarshalPayload (in , out ); err != nil {
272
+ t .Fatal (err )
273
+ }
274
+ // then
275
+ if ! out .ID .Equal (UUID {"12345678-abcd-1234-abcd-123456789012" }) {
276
+ t .Fatalf ("Did not set ID on dst interface: '%v'" , out .ID )
277
+ }
278
+ if ! out .UUIDField .Equal (UUID {"87654321-dcba-4321-dcba-210987654321" }) {
279
+ t .Fatalf ("Did not set UUIDField on dst interface: '%v'" , out .UUIDField )
280
+ }
281
+ if ! out .LatestRelatedModel .ID .Equal (UUID {"12345678-abcd-1234-abcd-111111111111" }) {
282
+ t .Fatalf ("Did not set LatestRelatedModel.ID on dst interface: '%v'" , out .LatestRelatedModel .ID )
283
+ }
284
+ if ! out .LatestRelatedModel .UUIDField .Equal (UUID {"87654321-dcba-4321-dcba-111111111111" }) {
285
+ t .Fatalf ("Did not set LatestRelatedModel.UUIDField on dst interface: '%v'" , out .LatestRelatedModel .UUIDField )
286
+ }
287
+ if ! out .RelatedModels [0 ].ID .Equal (UUID {"12345678-abcd-1234-abcd-222222222222" }) {
288
+ t .Fatalf ("Did not set RelatedModels[0].ID on dst interface: '%v'" , out .LatestRelatedModel .ID )
289
+ }
290
+ if ! out .RelatedModels [0 ].UUIDField .Equal (UUID {"87654321-dcba-4321-dcba-222222222222" }) {
291
+ t .Fatalf ("Did not set LatestRelatedModel.UUIDField on dst interface: '%v'" , out .LatestRelatedModel .UUIDField )
292
+ }
293
+
294
+ }
295
+
296
+ func TestUnmarshal_CustomType_Ptr (t * testing.T ) {
297
+ // given
298
+ // register the custom `*UUID` type
299
+ uuidType := reflect .TypeOf (& UUID {})
300
+ RegisterType (uuidType ,
301
+ func (value interface {}) (string , error ) {
302
+ result := value .(* UUID ).String ()
303
+ return result , nil
304
+ },
305
+ func (value string ) (interface {}, error ) {
306
+ return UUIDFromString (value )
307
+ })
308
+ in := sampleModelCustomTypeWithPtrs ()
309
+ // when
310
+ out := new (ModelWithUUIDs )
311
+ if err := UnmarshalPayload (in , out ); err != nil {
312
+ t .Fatal (err )
313
+ }
314
+ // then
315
+ if ! out .ID .Equal (UUID {"12345678-abcd-1234-abcd-123456789012" }) {
316
+ t .Fatalf ("Did not set ID on dst interface: '%v'" , out .ID )
317
+ }
318
+ if ! out .UUIDField .Equal (UUID {"87654321-dcba-4321-dcba-210987654321" }) {
319
+ t .Fatalf ("Did not set UUIDField on dst interface: '%v'" , out .UUIDField )
320
+ }
321
+ if ! out .LatestRelatedModel .ID .Equal (UUID {"12345678-abcd-1234-abcd-111111111111" }) {
322
+ t .Fatalf ("Did not set LatestRelatedModel.ID on dst interface: '%v'" , out .LatestRelatedModel .ID )
323
+ }
324
+ if ! out .LatestRelatedModel .UUIDField .Equal (UUID {"87654321-dcba-4321-dcba-111111111111" }) {
325
+ t .Fatalf ("Did not set LatestRelatedModel.UUIDField on dst interface: '%v'" , out .LatestRelatedModel .UUIDField )
326
+ }
327
+ if ! out .RelatedModels [0 ].ID .Equal (UUID {"12345678-abcd-1234-abcd-222222222222" }) {
328
+ t .Fatalf ("Did not set RelatedModels[0].ID on dst interface: '%v'" , out .LatestRelatedModel .ID )
329
+ }
330
+ if ! out .RelatedModels [0 ].UUIDField .Equal (UUID {"87654321-dcba-4321-dcba-222222222222" }) {
331
+ t .Fatalf ("Did not set LatestRelatedModel.UUIDField on dst interface: '%v'" , out .LatestRelatedModel .UUIDField )
332
+ }
333
+ }
334
+
252
335
func TestUnmarshalSetsAttrs (t * testing.T ) {
253
336
out , err := unmarshalSamplePayload ()
254
337
if err != nil {
@@ -945,3 +1028,67 @@ func sampleSerializedEmbeddedTestModel() *Blog {
945
1028
946
1029
return blog
947
1030
}
1031
+
1032
+ func sampleModelCustomType () io.Reader {
1033
+ model := sampleModelCustomTypePayload ()
1034
+ out := bytes .NewBuffer (nil )
1035
+ err := MarshalPayload (out , model )
1036
+ if err != nil {
1037
+ fmt .Printf ("Marshalled Custom Type failed: %s\n " , err .Error ())
1038
+ }
1039
+ fmt .Printf ("Marshalled Custom Type: %s\n " , out .String ())
1040
+ return out
1041
+ }
1042
+
1043
+ func sampleModelCustomTypePayload () * ModelWithUUIDs {
1044
+ return & ModelWithUUIDs {
1045
+ ID : UUID {"12345678-abcd-1234-abcd-123456789012" },
1046
+ UUIDField : UUID {"87654321-dcba-4321-dcba-210987654321" },
1047
+ LatestRelatedModel : & RelatedModelWithUUIDs {
1048
+ ID : UUID {"12345678-abcd-1234-abcd-111111111111" },
1049
+ UUIDField : UUID {"87654321-dcba-4321-dcba-111111111111" },
1050
+ },
1051
+ RelatedModels : []* RelatedModelWithUUIDs {
1052
+ & RelatedModelWithUUIDs {
1053
+ ID : UUID {"12345678-abcd-1234-abcd-222222222222" },
1054
+ UUIDField : UUID {"87654321-dcba-4321-dcba-222222222222" },
1055
+ },
1056
+ & RelatedModelWithUUIDs {
1057
+ ID : UUID {"12345678-abcd-1234-abcd-333333333333" },
1058
+ UUIDField : UUID {"87654321-dcba-4321-dcba-333333333333" },
1059
+ },
1060
+ },
1061
+ }
1062
+ }
1063
+
1064
+ func sampleModelCustomTypeWithPtrs () io.Reader {
1065
+ model := sampleModelCustomTypeWithPtrs ()
1066
+ out := bytes .NewBuffer (nil )
1067
+ err := MarshalPayload (out , model )
1068
+ if err != nil {
1069
+ fmt .Printf ("Marshalled Custom Type failed: %s\n " , err .Error ())
1070
+ }
1071
+ fmt .Printf ("Marshalled Custom Type (with pointers): '%s'\n " , out .String ())
1072
+ return out
1073
+ }
1074
+
1075
+ func sampleModelCustomTypeWithPtrsPayload () * ModelWithUUIDPtrs {
1076
+ return & ModelWithUUIDPtrs {
1077
+ ID : & UUID {"12345678-abcd-1234-abcd-123456789012" },
1078
+ UUIDField : & UUID {"87654321-dcba-4321-dcba-210987654321" },
1079
+ LatestRelatedModel : & RelatedModelWithUUIDPtrs {
1080
+ ID : & UUID {"12345678-abcd-1234-abcd-111111111111" },
1081
+ UUIDField : & UUID {"87654321-dcba-4321-dcba-111111111111" },
1082
+ },
1083
+ RelatedModels : []* RelatedModelWithUUIDPtrs {
1084
+ & RelatedModelWithUUIDPtrs {
1085
+ ID : & UUID {"12345678-abcd-1234-abcd-222222222222" },
1086
+ UUIDField : & UUID {"87654321-dcba-4321-dcba-222222222222" },
1087
+ },
1088
+ & RelatedModelWithUUIDPtrs {
1089
+ ID : & UUID {"12345678-abcd-1234-abcd-333333333333" },
1090
+ UUIDField : & UUID {"87654321-dcba-4321-dcba-333333333333" },
1091
+ },
1092
+ },
1093
+ }
1094
+ }
0 commit comments