From 1846e90f80cd6ff809f3eb35272f3eae29cf5d2a Mon Sep 17 00:00:00 2001 From: Jonatan Czarniecki Date: Wed, 27 Aug 2025 20:21:01 +0200 Subject: [PATCH] make `#[derive(sqlx::Type)]` automatically generate `impl PgHasArrayType` by default for newtype structs add regression tests --- sqlx-macros-core/src/derives/type.rs | 11 +++++++++++ tests/postgres/types.rs | 26 +++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/sqlx-macros-core/src/derives/type.rs b/sqlx-macros-core/src/derives/type.rs index e94b12b615..a66229287e 100644 --- a/sqlx-macros-core/src/derives/type.rs +++ b/sqlx-macros-core/src/derives/type.rs @@ -124,6 +124,17 @@ fn expand_derive_has_sql_type_transparent( } } )); + + if !attr.no_pg_array { + tts.extend(quote!( + #[automatically_derived] + impl ::sqlx::postgres::PgHasArrayType for #ident #ty_generics { + fn array_type_info() -> ::sqlx::postgres::PgTypeInfo { + ::sqlx::postgres::PgTypeInfo::array_of(#ty_name) + } + } + )); + } } Ok(tts) diff --git a/tests/postgres/types.rs b/tests/postgres/types.rs index 3b3a7eda1c..145e46f12b 100644 --- a/tests/postgres/types.rs +++ b/tests/postgres/types.rs @@ -713,6 +713,22 @@ test_type!(nested_domain_types_1(Postgres, "ROW(1, 21::positive_int, 50::percentage)::person" == Person { id: 1, age: PositiveInt(21), percent: Percentage(PositiveInt(50)) }) ); +test_type!(domain_type_array_1>(Postgres, + "ARRAY[1, 50, 1000]::positive_int[]" == vec![ + PositiveInt(1), + PositiveInt(50), + PositiveInt(1000), + ], +)); + +test_type!(domain_type_array_2>(Postgres, + "ARRAY[4, 66, 100]::percentage[]" == vec![ + Percentage(PositiveInt(4)), + Percentage(PositiveInt(66)), + Percentage(PositiveInt(100)) + ], +)); + #[derive(sqlx::Type, Debug, PartialEq)] #[sqlx(type_name = "leaf_composite")] struct LeafComposite { @@ -730,9 +746,17 @@ struct RootComposite { } test_type!(nested_domain_types_2(Postgres, - "ROW(ROW(1))::root_composite" == RootComposite { domain: Domain(LeafComposite { prim: 1})}) + "ROW(ROW(1))::root_composite" == RootComposite { domain: Domain(LeafComposite { prim: 1 }) }) ); +test_type!(domain_type_array_3>(Postgres, + "ARRAY[ROW(50), ROW(1), ROW(1000)]::domain[]" == vec![ + Domain(LeafComposite { prim: 50 }), + Domain(LeafComposite { prim: 1 }), + Domain(LeafComposite { prim: 1000 }), + ] +)); + test_type!(test_arc>(Postgres, "1::INT4" == Arc::new(1i32))); test_type!(test_cow>(Postgres, "1::INT4" == Cow::::Owned(1i32))); test_type!(test_box>(Postgres, "1::INT4" == Box::new(1i32)));