|
11 | 11 | use core::ffi::CStr; |
12 | 12 | use pgrx::Json; |
13 | 13 | use pgrx::PostgresEnum; |
14 | | -use pgrx::array::{FlatArray, RawArray}; |
| 14 | +use pgrx::array::{FlatArray, RawArray, Text}; |
15 | 15 | use pgrx::memcx::MemCx; |
16 | 16 | use pgrx::nullable::Nullable; |
17 | 17 | use pgrx::palloc::PBox; |
@@ -68,11 +68,10 @@ fn borrow_optional_array_with_default( |
68 | 68 | values.unwrap().iter().map(|v| v.into_option().copied().unwrap_or(0)).sum() |
69 | 69 | } |
70 | 70 |
|
71 | | -// FIXME: replace with Text |
72 | | -// #[pg_extern] |
73 | | -// fn borrow_serde_serialize_array<'dat>(values: &FlatArray<'dat, &'dat str>) -> Json { |
74 | | -// Json(json! { { "values": values } }) |
75 | | -// } |
| 71 | +#[pg_extern] |
| 72 | +fn borrow_serde_serialize_array(values: &FlatArray<'_, pgrx::array::Text>) -> Json { |
| 73 | + Json(json! { { "values": values } }) |
| 74 | +} |
76 | 75 |
|
77 | 76 | #[pg_extern] |
78 | 77 | fn borrow_serde_serialize_array_i32(values: &FlatArray<'_, i32>) -> Json { |
@@ -265,16 +264,36 @@ mod tests { |
265 | 264 | assert_eq!(sum, Ok(Some(6f32))); |
266 | 265 | } |
267 | 266 |
|
268 | | - // TODO: fix this test by redesigning SPI. |
269 | | - // #[pg_test] |
270 | | - // fn borrow_test_serde_serialize_array() -> Result<(), pgrx::spi::Error> { |
271 | | - // let json = Spi::get_one::<Json>( |
272 | | - // "SELECT borrow_serde_serialize_array(ARRAY['one', null, 'two', 'three'])", |
273 | | - // )? |
274 | | - // .expect("returned json was null"); |
275 | | - // assert_eq!(json.0, json! {{"values": ["one", null, "two", "three"]}}); |
276 | | - // Ok(()) |
277 | | - // } |
| 267 | + #[pg_test] |
| 268 | + fn borrow_test_serde_serialize_array() -> Result<(), pgrx::spi::Error> { |
| 269 | + let json = Spi::get_one::<Json>( |
| 270 | + "SELECT borrow_serde_serialize_array(ARRAY['one', null, 'two', 'three'])", |
| 271 | + )? |
| 272 | + .expect("returned json was null"); |
| 273 | + assert_eq!(json.0, json! {{"values": ["one", null, "two", "three"]}}); |
| 274 | + Ok(()) |
| 275 | + } |
| 276 | + |
| 277 | + #[pg_test] |
| 278 | + fn borrow_test_serde_serialize_array_mixed() -> Result<(), pgrx::spi::Error> { |
| 279 | + // Stride stress: empty string, null, multibyte, and a >127-byte element |
| 280 | + // (forces a 4-byte varlena header next to 1-byte ones). Mis-stride corrupts tail. |
| 281 | + let long = "x".repeat(200); |
| 282 | + let json = Spi::get_one::<Json>(&format!( |
| 283 | + "SELECT borrow_serde_serialize_array(ARRAY['', null, '日本語', '{long}', 'z'])" |
| 284 | + ))? |
| 285 | + .expect("returned json was null"); |
| 286 | + assert_eq!(json.0, json! {{"values": ["", null, "日本語", long, "z"]}}); |
| 287 | + Ok(()) |
| 288 | + } |
| 289 | + |
| 290 | + #[pg_test] |
| 291 | + fn borrow_test_serde_serialize_array_empty() -> Result<(), pgrx::spi::Error> { |
| 292 | + let json = Spi::get_one::<Json>("SELECT borrow_serde_serialize_array(ARRAY[]::text[])")? |
| 293 | + .expect("returned json was null"); |
| 294 | + assert_eq!(json.0, json! {{"values": []}}); |
| 295 | + Ok(()) |
| 296 | + } |
278 | 297 |
|
279 | 298 | #[pg_test] |
280 | 299 | fn borrow_test_optional_array_with_default() { |
|
0 commit comments