Skip to content

Commit fd9db3f

Browse files
committed
feat: add allow_missing in the field attribute
add allow_missing field attribute with macro DeserializeRow to handle partial deserialization
1 parent f4b3658 commit fd9db3f

File tree

8 files changed

+254
-87
lines changed

8 files changed

+254
-87
lines changed

scylla-cql/src/_macro_internal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use crate::deserialize::row::{
55
BuiltinDeserializationError as BuiltinRowDeserializationError,
66
BuiltinDeserializationErrorKind as BuiltinRowDeserializationErrorKind,
77
BuiltinTypeCheckErrorKind as DeserBuiltinRowTypeCheckErrorKind, ColumnIterator, DeserializeRow,
8-
deser_error_replace_rust_name as row_deser_error_replace_rust_name,
8+
RawColumn, deser_error_replace_rust_name as row_deser_error_replace_rust_name,
99
mk_deser_err as mk_row_deser_err, mk_typck_err as mk_row_typck_err,
1010
};
1111
pub use crate::deserialize::value::{

scylla-cql/src/deserialize/row_tests.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ fn test_struct_deserialization_loose_ordering() {
114114
d: i32,
115115
#[scylla(default_when_null)]
116116
e: &'a str,
117+
#[scylla(allow_missing)]
118+
f: &'a str,
117119
}
118120

119-
// Original order of columns
121+
// Original order of columns without field f
120122
let specs = &[
121123
spec("a", ColumnType::Native(NativeType::Text)),
122124
spec("b", ColumnType::Native(NativeType::Int)),
@@ -133,17 +135,19 @@ fn test_struct_deserialization_loose_ordering() {
133135
c: String::new(),
134136
d: 0,
135137
e: "def",
138+
f: "",
136139
}
137140
);
138141

139-
// Different order of columns - should still work
142+
// Different order of columns with field f - should still work with
140143
let specs = &[
141144
spec("e", ColumnType::Native(NativeType::Text)),
142145
spec("b", ColumnType::Native(NativeType::Int)),
143146
spec("d", ColumnType::Native(NativeType::Int)),
147+
spec("f", ColumnType::Native(NativeType::Text)),
144148
spec("a", ColumnType::Native(NativeType::Text)),
145149
];
146-
let byts = serialize_cells([None, val_int(123), None, val_str("abc")]);
150+
let byts = serialize_cells([None, val_int(123), None, val_str("efg"), val_str("abc")]);
147151
let row = deserialize::<MyRow<'_>>(specs, &byts).unwrap();
148152
assert_eq!(
149153
row,
@@ -153,6 +157,7 @@ fn test_struct_deserialization_loose_ordering() {
153157
c: String::new(),
154158
d: 0,
155159
e: "",
160+
f: "efg",
156161
}
157162
);
158163

@@ -189,11 +194,13 @@ fn test_struct_deserialization_strict_ordering() {
189194
c: String,
190195
#[scylla(default_when_null)]
191196
d: i32,
197+
#[scylla(allow_missing)]
198+
f: i32,
192199
#[scylla(default_when_null)]
193200
e: &'a str,
194201
}
195202

196-
// Correct order of columns
203+
// Correct order of columns without field f
197204
let specs = &[
198205
spec("a", ColumnType::Native(NativeType::Text)),
199206
spec("b", ColumnType::Native(NativeType::Int)),
@@ -210,6 +217,35 @@ fn test_struct_deserialization_strict_ordering() {
210217
c: String::new(),
211218
d: 0,
212219
e: "def",
220+
f: 0,
221+
}
222+
);
223+
224+
// Correct order of columns with field f
225+
let specs = &[
226+
spec("a", ColumnType::Native(NativeType::Text)),
227+
spec("b", ColumnType::Native(NativeType::Int)),
228+
spec("d", ColumnType::Native(NativeType::Int)),
229+
spec("f", ColumnType::Native(NativeType::Int)),
230+
spec("e", ColumnType::Native(NativeType::Text)),
231+
];
232+
let byts = serialize_cells([
233+
val_str("abc"),
234+
val_int(123),
235+
None,
236+
val_int(234),
237+
val_str("def"),
238+
]);
239+
let row = deserialize::<MyRow<'_>>(specs, &byts).unwrap();
240+
assert_eq!(
241+
row,
242+
MyRow {
243+
a: "abc",
244+
b: Some(123),
245+
c: String::new(),
246+
d: 0,
247+
e: "def",
248+
f: 234,
213249
}
214250
);
215251

0 commit comments

Comments
 (0)