@@ -101,42 +101,64 @@ fn test_struct_deserialization_loose_ordering() {
101
101
b : Option < i32 > ,
102
102
#[ scylla( skip) ]
103
103
c : String ,
104
+ #[ scylla( default_when_null) ]
105
+ d : i32 ,
106
+ #[ scylla( default_when_null) ]
107
+ e : & ' a str ,
104
108
}
105
109
106
110
// Original order of columns
107
111
let specs = & [
108
112
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
109
113
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
114
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
115
+ spec ( "e" , ColumnType :: Native ( NativeType :: Text ) ) ,
110
116
] ;
111
- let byts = serialize_cells ( [ val_str ( "abc" ) , val_int ( 123 ) ] ) ;
117
+ let byts = serialize_cells ( [ val_str ( "abc" ) , val_int ( 123 ) , None , val_str ( "def" ) ] ) ;
112
118
let row = deserialize :: < MyRow < ' _ > > ( specs, & byts) . unwrap ( ) ;
113
119
assert_eq ! (
114
120
row,
115
121
MyRow {
116
122
a: "abc" ,
117
123
b: Some ( 123 ) ,
118
124
c: String :: new( ) ,
125
+ d: 0 ,
126
+ e: "def" ,
119
127
}
120
128
) ;
121
129
122
130
// Different order of columns - should still work
123
131
let specs = & [
132
+ spec ( "e" , ColumnType :: Native ( NativeType :: Text ) ) ,
124
133
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
134
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
125
135
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
126
136
] ;
127
- let byts = serialize_cells ( [ val_int ( 123 ) , val_str ( "abc" ) ] ) ;
137
+ let byts = serialize_cells ( [ None , val_int ( 123 ) , None , val_str ( "abc" ) ] ) ;
128
138
let row = deserialize :: < MyRow < ' _ > > ( specs, & byts) . unwrap ( ) ;
129
139
assert_eq ! (
130
140
row,
131
141
MyRow {
132
142
a: "abc" ,
133
143
b: Some ( 123 ) ,
134
144
c: String :: new( ) ,
145
+ d: 0 ,
146
+ e: "" ,
135
147
}
136
148
) ;
137
149
138
150
// Missing column
139
- let specs = & [ spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ] ;
151
+ let specs = & [
152
+ spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
153
+ spec ( "e" , ColumnType :: Native ( NativeType :: Text ) ) ,
154
+ ] ;
155
+ MyRow :: type_check ( specs) . unwrap_err ( ) ;
156
+
157
+ // Missing both default_when_null column
158
+ let specs = & [
159
+ spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
160
+ spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
161
+ ] ;
140
162
MyRow :: type_check ( specs) . unwrap_err ( ) ;
141
163
142
164
// Wrong column type
@@ -156,33 +178,53 @@ fn test_struct_deserialization_strict_ordering() {
156
178
b : Option < i32 > ,
157
179
#[ scylla( skip) ]
158
180
c : String ,
181
+ #[ scylla( default_when_null) ]
182
+ d : i32 ,
183
+ #[ scylla( default_when_null) ]
184
+ e : & ' a str ,
159
185
}
160
186
161
187
// Correct order of columns
162
188
let specs = & [
163
189
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
164
190
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
191
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
192
+ spec ( "e" , ColumnType :: Native ( NativeType :: Text ) ) ,
165
193
] ;
166
- let byts = serialize_cells ( [ val_str ( "abc" ) , val_int ( 123 ) ] ) ;
194
+ let byts = serialize_cells ( [ val_str ( "abc" ) , val_int ( 123 ) , None , val_str ( "def" ) ] ) ;
167
195
let row = deserialize :: < MyRow < ' _ > > ( specs, & byts) . unwrap ( ) ;
168
196
assert_eq ! (
169
197
row,
170
198
MyRow {
171
199
a: "abc" ,
172
200
b: Some ( 123 ) ,
173
201
c: String :: new( ) ,
202
+ d: 0 ,
203
+ e: "def" ,
174
204
}
175
205
) ;
176
206
177
207
// Wrong order of columns
178
208
let specs = & [
179
209
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
180
210
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
211
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
212
+ spec ( "e" , ColumnType :: Native ( NativeType :: Text ) ) ,
181
213
] ;
182
214
MyRow :: type_check ( specs) . unwrap_err ( ) ;
183
215
184
216
// Missing column
185
- let specs = & [ spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ] ;
217
+ let specs = & [
218
+ spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
219
+ spec ( "e" , ColumnType :: Native ( NativeType :: Text ) ) ,
220
+ ] ;
221
+ MyRow :: type_check ( specs) . unwrap_err ( ) ;
222
+
223
+ // Missing both default_when_null column
224
+ let specs = & [
225
+ spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
226
+ spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
227
+ ] ;
186
228
MyRow :: type_check ( specs) . unwrap_err ( ) ;
187
229
188
230
// Wrong column type
@@ -476,6 +518,8 @@ fn test_struct_deserialization_errors() {
476
518
b : Option < i32 > ,
477
519
#[ scylla( rename = "c" ) ]
478
520
d : bool ,
521
+ #[ scylla( default_when_null) ]
522
+ e : Option < i32 > ,
479
523
}
480
524
481
525
// Type check errors
@@ -496,7 +540,7 @@ fn test_struct_deserialization_errors() {
496
540
else {
497
541
panic ! ( "unexpected error kind: {:?}" , err. kind)
498
542
} ;
499
- assert_eq ! ( missing_fields. as_slice( ) , & [ "c" ] ) ;
543
+ assert_eq ! ( missing_fields. as_slice( ) , & [ "c" , "e" ] ) ;
500
544
}
501
545
502
546
// Duplicated column
@@ -588,6 +632,7 @@ fn test_struct_deserialization_errors() {
588
632
spec ( "c" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
589
633
spec ( "a" , ColumnType :: Native ( NativeType :: Blob ) ) ,
590
634
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
635
+ spec ( "e" , ColumnType :: Native ( NativeType :: Int ) ) ,
591
636
] ;
592
637
593
638
let err = MyRow :: deserialize ( ColumnIterator :: new (
@@ -615,13 +660,15 @@ fn test_struct_deserialization_errors() {
615
660
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
616
661
spec ( "a" , ColumnType :: Native ( NativeType :: Ascii ) ) ,
617
662
spec ( "c" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
663
+ spec ( "e" , ColumnType :: Native ( NativeType :: Int ) ) ,
618
664
] ;
619
665
620
666
let row_bytes = serialize_cells (
621
667
[
622
668
& 0_i32 . to_be_bytes ( ) ,
623
669
"alamakota" . as_bytes ( ) ,
624
670
& 42_i16 . to_be_bytes ( ) ,
671
+ & 13_i32 . to_be_bytes ( ) ,
625
672
]
626
673
. map ( Some ) ,
627
674
) ;
@@ -664,6 +711,8 @@ fn test_struct_deserialization_errors() {
664
711
x : String ,
665
712
b : Option < i32 > ,
666
713
c : bool ,
714
+ #[ scylla( default_when_null) ]
715
+ d : Option < i32 > ,
667
716
}
668
717
669
718
// Type check errors
@@ -682,7 +731,7 @@ fn test_struct_deserialization_errors() {
682
731
else {
683
732
panic ! ( "unexpected error kind: {:?}" , err. kind)
684
733
} ;
685
- assert_eq ! ( rust_cols, 3 ) ;
734
+ assert_eq ! ( rust_cols, 4 ) ;
686
735
assert_eq ! ( cql_cols, 1 ) ;
687
736
}
688
737
@@ -692,7 +741,8 @@ fn test_struct_deserialization_errors() {
692
741
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
693
742
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
694
743
spec ( "c" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
695
- spec ( "d" , ColumnType :: Native ( NativeType :: Counter ) ) ,
744
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
745
+ spec ( "e" , ColumnType :: Native ( NativeType :: Counter ) ) ,
696
746
] ;
697
747
let err = MyRow :: type_check ( & specs) . unwrap_err ( ) ;
698
748
let err = get_typck_err_inner ( err. 0 . as_ref ( ) ) ;
@@ -705,16 +755,17 @@ fn test_struct_deserialization_errors() {
705
755
else {
706
756
panic ! ( "unexpected error kind: {:?}" , err. kind)
707
757
} ;
708
- assert_eq ! ( rust_cols, 3 ) ;
709
- assert_eq ! ( cql_cols, 4 ) ;
758
+ assert_eq ! ( rust_cols, 4 ) ;
759
+ assert_eq ! ( cql_cols, 5 ) ;
710
760
}
711
761
712
762
// Renamed column name mismatch
713
763
{
714
764
let specs = [
715
765
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
716
766
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
717
- spec ( "d" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
767
+ spec ( "e" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
768
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
718
769
] ;
719
770
let err = MyRow :: type_check ( & specs) . unwrap_err ( ) ;
720
771
let err = get_typck_err_inner ( err. 0 . as_ref ( ) ) ;
@@ -731,7 +782,7 @@ fn test_struct_deserialization_errors() {
731
782
assert_eq ! ( field_index, 3 ) ;
732
783
assert_eq ! ( rust_column_name, "c" ) ;
733
784
assert_eq ! ( column_index, 2 ) ;
734
- assert_eq ! ( db_column_name. as_str( ) , "d " ) ;
785
+ assert_eq ! ( db_column_name. as_str( ) , "e " ) ;
735
786
}
736
787
737
788
// Columns switched - column name mismatch
@@ -740,6 +791,7 @@ fn test_struct_deserialization_errors() {
740
791
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
741
792
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
742
793
spec ( "c" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
794
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
743
795
] ;
744
796
let err = MyRow :: type_check ( & specs) . unwrap_err ( ) ;
745
797
let err = get_typck_err_inner ( err. 0 . as_ref ( ) ) ;
@@ -766,6 +818,7 @@ fn test_struct_deserialization_errors() {
766
818
spec ( "a" , ColumnType :: Native ( NativeType :: Blob ) ) ,
767
819
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
768
820
spec ( "c" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
821
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
769
822
] ;
770
823
let err = MyRow :: type_check ( & specs) . unwrap_err ( ) ;
771
824
let err = get_typck_err_inner ( err. 0 . as_ref ( ) ) ;
@@ -804,6 +857,7 @@ fn test_struct_deserialization_errors() {
804
857
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
805
858
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
806
859
spec ( "c" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
860
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
807
861
] ;
808
862
809
863
let err = MyRow :: deserialize ( ColumnIterator :: new (
@@ -831,10 +885,17 @@ fn test_struct_deserialization_errors() {
831
885
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
832
886
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
833
887
spec ( "c" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
888
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
834
889
] ;
835
890
836
891
let row_bytes = serialize_cells (
837
- [ ( & b"alamakota" [ ..] ) , & 42_i32 . to_be_bytes ( ) , & [ true as u8 ] ] . map ( Some ) ,
892
+ [
893
+ ( & b"alamakota" [ ..] ) ,
894
+ & 42_i32 . to_be_bytes ( ) ,
895
+ & [ true as u8 ] ,
896
+ & 13_i32 . to_be_bytes ( ) ,
897
+ ]
898
+ . map ( Some ) ,
838
899
) ;
839
900
840
901
let row_bytes_too_short = row_bytes. slice ( ..row_bytes. len ( ) - 1 ) ;
@@ -852,8 +913,8 @@ fn test_struct_deserialization_errors() {
852
913
else {
853
914
panic ! ( "unexpected error kind: {:?}" , err. kind)
854
915
} ;
855
- assert_eq ! ( column_index, 2 ) ;
856
- assert_eq ! ( column_name, "c " ) ;
916
+ assert_eq ! ( column_index, 3 ) ;
917
+ assert_eq ! ( column_name, "d " ) ;
857
918
}
858
919
859
920
// Column deserialization failed
@@ -862,10 +923,17 @@ fn test_struct_deserialization_errors() {
862
923
spec ( "a" , ColumnType :: Native ( NativeType :: Text ) ) ,
863
924
spec ( "b" , ColumnType :: Native ( NativeType :: Int ) ) ,
864
925
spec ( "c" , ColumnType :: Native ( NativeType :: Boolean ) ) ,
926
+ spec ( "d" , ColumnType :: Native ( NativeType :: Int ) ) ,
865
927
] ;
866
928
867
929
let row_bytes = serialize_cells (
868
- [ & b"alamakota" [ ..] , & 42_i64 . to_be_bytes ( ) , & [ true as u8 ] ] . map ( Some ) ,
930
+ [
931
+ & b"alamakota" [ ..] ,
932
+ & 42_i64 . to_be_bytes ( ) ,
933
+ & [ true as u8 ] ,
934
+ & 13_i32 . to_be_bytes ( ) ,
935
+ ]
936
+ . map ( Some ) ,
869
937
) ;
870
938
871
939
let err = deserialize :: < MyRow > ( & specs, & row_bytes) . unwrap_err ( ) ;
0 commit comments