Skip to content

Commit 1269002

Browse files
committed
macros: SerializeValue supports #[default_when_null]
The `#[default_when_null]` attibute is intented to allow a flexible transition period when schema is altered. Namely, if a UDT is extended with a new field, then server will populate the new column with NULLs. If the data model does not abstractly permit NULLs in that column, one may not want to represent the field with `Option<T>`, not to have to handle the case of `None`. In such case, `#[default_when_null]` can be attached to the new Rust struct's field and this way handle the situation: - in deserialization, the NULL field received from the DB will be default-initialized, - in serialization, there is no corresponding situation, so this attribute does nothing. This commit adds support for this attribute: for parsing it using `darling`, and that's it - because it's a no-op. Also, a corresponding (doc)test is added to confirm that the attibute is parsed correctly.
1 parent 941684a commit 1269002

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,18 @@ mod doctests {
15791579
/// }
15801580
/// ```
15811581
fn _test_udt_unordered_flavour_no_limitations_on_allow_missing() {}
1582+
1583+
/// ```
1584+
/// #[derive(scylla_macros::SerializeValue)]
1585+
/// #[scylla(crate = scylla_cql)]
1586+
/// struct TestUdt {
1587+
/// a: i32,
1588+
/// #[scylla(default_when_null)]
1589+
/// b: bool,
1590+
/// c: String,
1591+
/// }
1592+
/// ```
1593+
fn _test_udt_default_when_null_is_accepted() {}
15821594
}
15831595

15841596
#[cfg(test)]

scylla-macros/src/serialize/value.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ struct FieldAttributes {
7272
#[darling(default)]
7373
#[darling(rename = "allow_missing")]
7474
ignore_missing: bool,
75+
76+
// Used for deserialization only. Ignored in serialization.
77+
#[darling(default)]
78+
#[darling(rename = "default_when_null")]
79+
_default_when_null: bool,
7580
}
7681

7782
struct Context {

0 commit comments

Comments
 (0)