Skip to content

Commit befa148

Browse files
authored
Merge pull request #903 from Lorak-mmk/serialize_skip_field
Add `skip` attribute to serialization proc macros
2 parents 2faeba0 + 5a0b285 commit befa148

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

scylla-cql/src/macros.rs

Lines changed: 8 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
@@ -211,6 +215,10 @@ pub use scylla_macros::SerializeCql;
211215
///
212216
/// Serializes the field to the column / bind marker with given name instead of
213217
/// its Rust name.
218+
///
219+
/// `#[scylla(skip)]`
220+
///
221+
/// Don't use the field during serialization.
214222
pub use scylla_macros::SerializeRow;
215223

216224
// 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-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)?;

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)