Skip to content

Commit

Permalink
Add skip attribute to SerializeRow
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorak-mmk committed Jan 4, 2024
1 parent 654734d commit 5a0b285
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions scylla-cql/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ pub use scylla_macros::SerializeCql;
///
/// Serializes the field to the column / bind marker with given name instead of
/// its Rust name.
///
/// `#[scylla(skip)]`
///
/// Don't use the field during serialization.
pub use scylla_macros::SerializeRow;

// Reexports for derive(IntoUserType)
Expand Down
40 changes: 40 additions & 0 deletions scylla-cql/src/types/serialize/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,4 +1519,44 @@ mod tests {

assert_eq!(reference, row);
}

#[derive(SerializeRow, Debug)]
#[scylla(crate = crate)]
struct TestRowWithSkippedFields {
a: String,
b: i32,
#[scylla(skip)]
#[allow(dead_code)]
skipped: Vec<String>,
c: Vec<i64>,
}

#[test]
fn test_row_serialization_with_skipped_field() {
let spec = [
col("a", ColumnType::Text),
col("b", ColumnType::Int),
col("c", ColumnType::List(Box::new(ColumnType::BigInt))),
];

let reference = do_serialize(
TestRowWithColumnSorting {
a: "Ala ma kota".to_owned(),
b: 42,
c: vec![1, 2, 3],
},
&spec,
);
let row = do_serialize(
TestRowWithSkippedFields {
a: "Ala ma kota".to_owned(),
b: 42,
skipped: vec!["abcd".to_owned(), "efgh".to_owned()],
c: vec![1, 2, 3],
},
&spec,
);

assert_eq!(reference, row);
}
}
6 changes: 6 additions & 0 deletions scylla-macros/src/serialize/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ impl Field {
#[darling(attributes(scylla))]
struct FieldAttributes {
rename: Option<String>,

#[darling(default)]
skip: bool,
}

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

0 comments on commit 5a0b285

Please sign in to comment.