Skip to content

Commit 3abb186

Browse files
make #[derive(sqlx::Type)] automatically generate impl PgHasArrayType by default for newtype structs (#4008)
add regression tests
1 parent e6e8fc7 commit 3abb186

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

sqlx-macros-core/src/derives/type.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ fn expand_derive_has_sql_type_transparent(
124124
}
125125
}
126126
));
127+
128+
if !attr.no_pg_array {
129+
tts.extend(quote!(
130+
#[automatically_derived]
131+
impl ::sqlx::postgres::PgHasArrayType for #ident #ty_generics {
132+
fn array_type_info() -> ::sqlx::postgres::PgTypeInfo {
133+
::sqlx::postgres::PgTypeInfo::array_of(#ty_name)
134+
}
135+
}
136+
));
137+
}
127138
}
128139

129140
Ok(tts)

tests/postgres/types.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,22 @@ test_type!(nested_domain_types_1<Person>(Postgres,
713713
"ROW(1, 21::positive_int, 50::percentage)::person" == Person { id: 1, age: PositiveInt(21), percent: Percentage(PositiveInt(50)) })
714714
);
715715

716+
test_type!(domain_type_array_1<Vec<PositiveInt>>(Postgres,
717+
"ARRAY[1, 50, 1000]::positive_int[]" == vec![
718+
PositiveInt(1),
719+
PositiveInt(50),
720+
PositiveInt(1000),
721+
],
722+
));
723+
724+
test_type!(domain_type_array_2<Vec<Percentage>>(Postgres,
725+
"ARRAY[4, 66, 100]::percentage[]" == vec![
726+
Percentage(PositiveInt(4)),
727+
Percentage(PositiveInt(66)),
728+
Percentage(PositiveInt(100))
729+
],
730+
));
731+
716732
#[derive(sqlx::Type, Debug, PartialEq)]
717733
#[sqlx(type_name = "leaf_composite")]
718734
struct LeafComposite {
@@ -730,9 +746,17 @@ struct RootComposite {
730746
}
731747

732748
test_type!(nested_domain_types_2<RootComposite>(Postgres,
733-
"ROW(ROW(1))::root_composite" == RootComposite { domain: Domain(LeafComposite { prim: 1})})
749+
"ROW(ROW(1))::root_composite" == RootComposite { domain: Domain(LeafComposite { prim: 1 }) })
734750
);
735751

752+
test_type!(domain_type_array_3<Vec<Domain>>(Postgres,
753+
"ARRAY[ROW(50), ROW(1), ROW(1000)]::domain[]" == vec![
754+
Domain(LeafComposite { prim: 50 }),
755+
Domain(LeafComposite { prim: 1 }),
756+
Domain(LeafComposite { prim: 1000 }),
757+
]
758+
));
759+
736760
test_type!(test_arc<Arc<i32>>(Postgres, "1::INT4" == Arc::new(1i32)));
737761
test_type!(test_cow<Cow<'_, i32>>(Postgres, "1::INT4" == Cow::<i32>::Owned(1i32)));
738762
test_type!(test_box<Box<i32>>(Postgres, "1::INT4" == Box::new(1i32)));

0 commit comments

Comments
 (0)