Skip to content

Commit 0208651

Browse files
committed
feat: support serde(flatten) attribute
1 parent 53d4e1f commit 0208651

13 files changed

+59
-19
lines changed

derive/src/lib.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,35 @@ fn column_names(data: &DataStruct, cx: &Ctxt, container: &Container) -> Result<T
1313
Ok(match &data.fields {
1414
Fields::Named(fields) => {
1515
let rename_rule = container.rename_all_rules().deserialize;
16-
let column_names_iter = fields
16+
17+
let chain_iters = fields
1718
.named
1819
.iter()
1920
.enumerate()
20-
.map(|(index, field)| Field::from_ast(cx, index, field, None, &SerdeDefault::None))
21-
.filter(|field| !field.skip_serializing() && !field.skip_deserializing())
22-
.map(|field| {
23-
rename_rule
24-
.apply_to_field(field.name().serialize_name())
25-
.to_string()
21+
.map(|(index, field)| {
22+
(
23+
Field::from_ast(cx, index, field, None, &SerdeDefault::None),
24+
&field.ty,
25+
)
26+
})
27+
.filter(|(field, _)| !(field.skip_serializing() || field.skip_deserializing()))
28+
.map(|(field_meta, ty)| {
29+
if field_meta.flatten() {
30+
quote! {
31+
<#ty as clickhouse::Row>::column_names().into_iter()
32+
}
33+
} else {
34+
let column_name = rename_rule
35+
.apply_to_field(field_meta.name().serialize_name())
36+
.to_string();
37+
quote! {
38+
std::iter::once(#column_name)
39+
}
40+
}
2641
});
2742

2843
quote! {
29-
[#( #column_names_iter,)*]
44+
std::iter::empty() #(.chain(#chain_iters))*
3045
}
3146
}
3247
Fields::Unnamed(_) => {

derive/src/tests/snapshots/generic_borrowed_row-2.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Sample<'a, A, B> {
1212
impl<'a, A, B> clickhouse::Row for Sample<'a, A, B> {
1313
const NAME: &'static str = stringify!(Sample);
1414
fn column_names() -> impl IntoIterator<Item = &'static str> {
15-
["a", "b"]
15+
std::iter::empty().chain(std::iter::once("a")).chain(std::iter::once("b"))
1616
}
1717
fn column_count() -> usize {
1818
<Self as clickhouse::Row>::column_names().into_iter().count()

derive/src/tests/snapshots/generic_borrowed_row-3.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where
1818
{
1919
const NAME: &'static str = stringify!(Sample);
2020
fn column_names() -> impl IntoIterator<Item = &'static str> {
21-
["a", "b"]
21+
std::iter::empty().chain(std::iter::once("a")).chain(std::iter::once("b"))
2222
}
2323
fn column_count() -> usize {
2424
<Self as clickhouse::Row>::column_names().into_iter().count()

derive/src/tests/snapshots/generic_borrowed_row.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Sample<'a, T> {
1212
impl<'a, T> clickhouse::Row for Sample<'a, T> {
1313
const NAME: &'static str = stringify!(Sample);
1414
fn column_names() -> impl IntoIterator<Item = &'static str> {
15-
["a", "b"]
15+
std::iter::empty().chain(std::iter::once("a")).chain(std::iter::once("b"))
1616
}
1717
fn column_count() -> usize {
1818
<Self as clickhouse::Row>::column_names().into_iter().count()

derive/src/tests/snapshots/generic_owned_row-2.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Sample<A, B> {
1212
impl<A, B> clickhouse::Row for Sample<A, B> {
1313
const NAME: &'static str = stringify!(Sample);
1414
fn column_names() -> impl IntoIterator<Item = &'static str> {
15-
["a", "b"]
15+
std::iter::empty().chain(std::iter::once("a")).chain(std::iter::once("b"))
1616
}
1717
fn column_count() -> usize {
1818
<Self as clickhouse::Row>::column_names().into_iter().count()

derive/src/tests/snapshots/generic_owned_row-3.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where
1818
{
1919
const NAME: &'static str = stringify!(Sample);
2020
fn column_names() -> impl IntoIterator<Item = &'static str> {
21-
["a", "b"]
21+
std::iter::empty().chain(std::iter::once("a")).chain(std::iter::once("b"))
2222
}
2323
fn column_count() -> usize {
2424
<Self as clickhouse::Row>::column_names().into_iter().count()

derive/src/tests/snapshots/generic_owned_row.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Sample<T> {
1212
impl<T> clickhouse::Row for Sample<T> {
1313
const NAME: &'static str = stringify!(Sample);
1414
fn column_names() -> impl IntoIterator<Item = &'static str> {
15-
["a", "b"]
15+
std::iter::empty().chain(std::iter::once("a")).chain(std::iter::once("b"))
1616
}
1717
fn column_count() -> usize {
1818
<Self as clickhouse::Row>::column_names().into_iter().count()

derive/src/tests/snapshots/serde_rename.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ struct Sample {
1515
impl clickhouse::Row for Sample {
1616
const NAME: &'static str = stringify!(Sample);
1717
fn column_names() -> impl IntoIterator<Item = &'static str> {
18-
["a", "items.a", "items.b"]
18+
std::iter::empty()
19+
.chain(std::iter::once("a"))
20+
.chain(std::iter::once("items.a"))
21+
.chain(std::iter::once("items.b"))
1922
}
2023
fn column_count() -> usize {
2124
<Self as clickhouse::Row>::column_names().into_iter().count()

derive/src/tests/snapshots/serde_skip_deserializing.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Sample {
1313
impl clickhouse::Row for Sample {
1414
const NAME: &'static str = stringify!(Sample);
1515
fn column_names() -> impl IntoIterator<Item = &'static str> {
16-
["a"]
16+
std::iter::empty().chain(std::iter::once("a"))
1717
}
1818
fn column_count() -> usize {
1919
<Self as clickhouse::Row>::column_names().into_iter().count()

derive/src/tests/snapshots/serde_skip_serializing.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Sample {
1313
impl clickhouse::Row for Sample {
1414
const NAME: &'static str = stringify!(Sample);
1515
fn column_names() -> impl IntoIterator<Item = &'static str> {
16-
["a"]
16+
std::iter::empty().chain(std::iter::once("a"))
1717
}
1818
fn column_count() -> usize {
1919
<Self as clickhouse::Row>::column_names().into_iter().count()

0 commit comments

Comments
 (0)