Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skip attribute to serialization proc macros #903

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scylla-cql/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub use scylla_macros::ValueList;
///
/// Serializes the field to the UDT struct field with given name instead of
/// its Rust name.
///
/// `#[scylla(skip)]`
///
/// Don't use the field during serialization.
pub use scylla_macros::SerializeCql;

/// Derive macro for the [`SerializeRow`](crate::types::serialize::row::SerializeRow) trait
Expand Down Expand Up @@ -211,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);
}
}
47 changes: 47 additions & 0 deletions scylla-cql/src/types/serialize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2692,4 +2692,51 @@ mod tests {
BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::NoSuchFieldInUdt { .. })
));
}

#[derive(SerializeCql, Debug)]
#[scylla(crate = crate, flavor = "enforce_order", skip_name_checks)]
struct TestUdtWithSkippedFields {
a: String,
b: i32,
#[scylla(skip)]
#[allow(dead_code)]
skipped: Vec<String>,
c: Vec<i64>,
}

#[test]
fn test_row_serialization_with_skipped_field() {
let typ = ColumnType::UserDefinedType {
type_name: "typ".to_string(),
keyspace: "ks".to_string(),
field_types: vec![
("a".to_string(), ColumnType::Text),
("b".to_string(), ColumnType::Int),
(
"c".to_string(),
ColumnType::List(Box::new(ColumnType::BigInt)),
),
],
};

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

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

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

struct Context {
Expand Down Expand Up @@ -78,6 +81,9 @@ pub fn derive_serialize_cql(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
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