Skip to content

Commit 5a0b285

Browse files
committed
Add skip attribute to SerializeRow
1 parent 654734d commit 5a0b285

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

scylla-cql/src/macros.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ pub use scylla_macros::SerializeCql;
215215
///
216216
/// Serializes the field to the column / bind marker with given name instead of
217217
/// its Rust name.
218+
///
219+
/// `#[scylla(skip)]`
220+
///
221+
/// Don't use the field during serialization.
218222
pub use scylla_macros::SerializeRow;
219223

220224
// Reexports for derive(IntoUserType)

scylla-cql/src/types/serialize/row.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,4 +1519,44 @@ mod tests {
15191519

15201520
assert_eq!(reference, row);
15211521
}
1522+
1523+
#[derive(SerializeRow, Debug)]
1524+
#[scylla(crate = crate)]
1525+
struct TestRowWithSkippedFields {
1526+
a: String,
1527+
b: i32,
1528+
#[scylla(skip)]
1529+
#[allow(dead_code)]
1530+
skipped: Vec<String>,
1531+
c: Vec<i64>,
1532+
}
1533+
1534+
#[test]
1535+
fn test_row_serialization_with_skipped_field() {
1536+
let spec = [
1537+
col("a", ColumnType::Text),
1538+
col("b", ColumnType::Int),
1539+
col("c", ColumnType::List(Box::new(ColumnType::BigInt))),
1540+
];
1541+
1542+
let reference = do_serialize(
1543+
TestRowWithColumnSorting {
1544+
a: "Ala ma kota".to_owned(),
1545+
b: 42,
1546+
c: vec![1, 2, 3],
1547+
},
1548+
&spec,
1549+
);
1550+
let row = do_serialize(
1551+
TestRowWithSkippedFields {
1552+
a: "Ala ma kota".to_owned(),
1553+
b: 42,
1554+
skipped: vec!["abcd".to_owned(), "efgh".to_owned()],
1555+
c: vec![1, 2, 3],
1556+
},
1557+
&spec,
1558+
);
1559+
1560+
assert_eq!(reference, row);
1561+
}
15221562
}

scylla-macros/src/serialize/row.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ impl Field {
4848
#[darling(attributes(scylla))]
4949
struct FieldAttributes {
5050
rename: Option<String>,
51+
52+
#[darling(default)]
53+
skip: bool,
5154
}
5255

5356
struct Context {
@@ -75,6 +78,9 @@ pub fn derive_serialize_row(tokens_input: TokenStream) -> Result<syn::ItemImpl,
7578
attrs,
7679
})
7780
})
81+
// Filter the fields now instead of at the places that use them later
82+
// as it's less error prone - we just filter in one place instead of N places.
83+
.filter(|f| f.as_ref().map(|f| !f.attrs.skip).unwrap_or(true))
7884
.collect::<Result<_, _>>()?;
7985
let ctx = Context { attributes, fields };
8086
ctx.validate(&input.ident)?;

0 commit comments

Comments
 (0)