Skip to content

Commit 654734d

Browse files
committed
Add skip field attribute to SerializeCql
1 parent 2faeba0 commit 654734d

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

scylla-cql/src/macros.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ pub use scylla_macros::ValueList;
117117
///
118118
/// Serializes the field to the UDT struct field with given name instead of
119119
/// its Rust name.
120+
///
121+
/// `#[scylla(skip)]`
122+
///
123+
/// Don't use the field during serialization.
120124
pub use scylla_macros::SerializeCql;
121125

122126
/// Derive macro for the [`SerializeRow`](crate::types::serialize::row::SerializeRow) trait

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,4 +2692,51 @@ mod tests {
26922692
BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::NoSuchFieldInUdt { .. })
26932693
));
26942694
}
2695+
2696+
#[derive(SerializeCql, Debug)]
2697+
#[scylla(crate = crate, flavor = "enforce_order", skip_name_checks)]
2698+
struct TestUdtWithSkippedFields {
2699+
a: String,
2700+
b: i32,
2701+
#[scylla(skip)]
2702+
#[allow(dead_code)]
2703+
skipped: Vec<String>,
2704+
c: Vec<i64>,
2705+
}
2706+
2707+
#[test]
2708+
fn test_row_serialization_with_skipped_field() {
2709+
let typ = ColumnType::UserDefinedType {
2710+
type_name: "typ".to_string(),
2711+
keyspace: "ks".to_string(),
2712+
field_types: vec![
2713+
("a".to_string(), ColumnType::Text),
2714+
("b".to_string(), ColumnType::Int),
2715+
(
2716+
"c".to_string(),
2717+
ColumnType::List(Box::new(ColumnType::BigInt)),
2718+
),
2719+
],
2720+
};
2721+
2722+
let reference = do_serialize(
2723+
TestUdtWithFieldSorting {
2724+
a: "Ala ma kota".to_owned(),
2725+
b: 42,
2726+
c: vec![1, 2, 3],
2727+
},
2728+
&typ,
2729+
);
2730+
let row = do_serialize(
2731+
TestUdtWithSkippedFields {
2732+
a: "Ala ma kota".to_owned(),
2733+
b: 42,
2734+
skipped: vec!["abcd".to_owned(), "efgh".to_owned()],
2735+
c: vec![1, 2, 3],
2736+
},
2737+
&typ,
2738+
);
2739+
2740+
assert_eq!(reference, row);
2741+
}
26952742
}

scylla-macros/src/serialize/cql.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ impl Field {
5151
#[darling(attributes(scylla))]
5252
struct FieldAttributes {
5353
rename: Option<String>,
54+
55+
#[darling(default)]
56+
skip: bool,
5457
}
5558

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

0 commit comments

Comments
 (0)