Skip to content

Commit 44e47f7

Browse files
authored
Merge pull request #154 from psqlpy-python/feature/just_array_in_jsonb_column
Added support for just array in JSONB column
2 parents bb32bbc + 6eb40b1 commit 44e47f7

File tree

7 files changed

+26
-20
lines changed

7 files changed

+26
-20
lines changed

python/tests/test_value_converter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ async def test_as_class(
178178
JSONB([{"array": "json"}, {"one more": "test"}]),
179179
[{"array": "json"}, {"one more": "test"}],
180180
),
181+
(
182+
"JSONB",
183+
JSONB([1, "1", 1.0]),
184+
[1, "1", 1.0],
185+
),
181186
(
182187
"JSON",
183188
{
@@ -194,6 +199,11 @@ async def test_as_class(
194199
JSON([{"array": "json"}, {"one more": "test"}]),
195200
[{"array": "json"}, {"one more": "test"}],
196201
),
202+
(
203+
"JSON",
204+
JSON([1, "1", 1.0]),
205+
[1, "1", 1.0],
206+
),
197207
(
198208
"MACADDR",
199209
MacAddr6("08:00:2b:01:02:03"),

src/driver/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl Connection {
401401
/// Create new transaction object.
402402
///
403403
/// # Errors
404-
/// May return Err Result if db_client is None.
404+
/// May return Err Result if `db_client` is None.
405405
#[pyo3(signature = (
406406
isolation_level=None,
407407
read_variant=None,

src/driver/connection_pool_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl ConnectionPoolBuilder {
7676
))
7777
}
7878

79-
/// Set ca_file for ssl_mode in PostgreSQL.
79+
/// Set `ca_file` for `ssl_mode` in `PostgreSQL`.
8080
fn ca_file(self_: Py<Self>, ca_file: String) -> Py<Self> {
8181
Python::with_gil(|gil| {
8282
let mut self_ = self_.borrow_mut(gil);
@@ -241,7 +241,7 @@ impl ConnectionPoolBuilder {
241241
/// Sets the TCP user timeout.
242242
///
243243
/// This is ignored for Unix domain socket connections. It is only supported on systems where
244-
/// TCP_USER_TIMEOUT is available and will default to the system default if omitted or set to 0;
244+
/// `TCP_USER_TIMEOUT` is available and will default to the system default if omitted or set to 0;
245245
/// on other systems, it has no effect.
246246
#[must_use]
247247
pub fn tcp_user_timeout(self_: Py<Self>, tcp_user_timeout: u64) -> Py<Self> {
@@ -314,7 +314,7 @@ impl ConnectionPoolBuilder {
314314
}
315315

316316
/// Sets the time interval between TCP keepalive probes.
317-
/// On Windows, this sets the value of the tcp_keepalive struct’s keepaliveinterval field.
317+
/// On Windows, this sets the value of the `tcp_keepalive` struct’s keepaliveinterval field.
318318
///
319319
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
320320
#[must_use]

src/driver/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl Transaction {
309309
/// Execute many queries in a transaction.
310310
///
311311
/// More information in a documentation:
312-
/// https://psqlpy-python.github.io/components/transaction.html#pipeline
312+
/// `<https://psqlpy-python.github.io/components/transaction.html#pipeline>`
313313
///
314314
/// # Errors
315315
/// Can return error if there is a problem with DB communication.

src/extra_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Text {
9797

9898
#[pymethods]
9999
impl Text {
100-
/// Create new PyText from Python str.
100+
/// Create new `PyText` from Python str.
101101
#[new]
102102
#[allow(clippy::missing_errors_doc)]
103103
#[must_use]
@@ -121,7 +121,7 @@ impl VarChar {
121121

122122
#[pymethods]
123123
impl VarChar {
124-
/// Create new PyVarChar from Python str.
124+
/// Create new `PyVarChar` from Python str.
125125
#[new]
126126
#[allow(clippy::missing_errors_doc)]
127127
#[must_use]

src/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub enum SynchronousCommit {
175175
/// As the name indicates, the commit acknowledgment can come before
176176
/// flushing the records to disk.
177177
/// This is generally called as an asynchronous commit.
178-
/// If the PostgreSQL instance crashes,
178+
/// If the `PostgreSQL` instance crashes,
179179
/// the last few asynchronous commits might be lost.
180180
Off,
181181
/// WAL records are written and flushed to local disks.

src/value_converter/models/serde_value.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,12 @@ fn serde_value_from_list(_gil: Python<'_>, bind_value: &Bound<'_, PyAny>) -> PSQ
6767
let mut result_vec: Vec<Value> = Vec::with_capacity(py_list.len());
6868

6969
for item in py_list.iter() {
70-
if item.is_instance_of::<PyDict>() {
71-
let python_dto = from_python_untyped(&item)?;
72-
result_vec.push(python_dto.to_serde_value()?);
73-
} else if item.is_instance_of::<PyList>() {
70+
if item.is_instance_of::<PyList>() {
7471
let serde_value = build_serde_value(&item)?;
7572
result_vec.push(serde_value);
7673
} else {
77-
return Err(RustPSQLDriverError::PyToRustValueConversionError(
78-
"Items in JSON array must be dicts or lists.".to_string(),
79-
));
74+
let python_dto = from_python_untyped(&item)?;
75+
result_vec.push(python_dto.to_serde_value()?);
8076
}
8177
}
8278

@@ -112,17 +108,17 @@ fn serde_value_from_dict(bind_value: &Bound<'_, PyAny>) -> PSQLPyResult<Value> {
112108
/// # Errors
113109
/// May return error if cannot convert Python type into Rust one.
114110
#[allow(clippy::needless_pass_by_value)]
111+
#[allow(clippy::needless_return)]
115112
pub fn build_serde_value(value: &Bound<'_, PyAny>) -> PSQLPyResult<Value> {
116113
Python::with_gil(|gil| {
117114
if value.is_instance_of::<PyList>() {
118-
serde_value_from_list(gil, value)
115+
return serde_value_from_list(gil, value);
119116
} else if value.is_instance_of::<PyDict>() {
120117
return serde_value_from_dict(value);
121-
} else {
122-
return Err(RustPSQLDriverError::PyToRustValueConversionError(
123-
"PyJSON must be dict value.".to_string(),
124-
));
125118
}
119+
Err(RustPSQLDriverError::PyToRustValueConversionError(
120+
"PyJSON must be dict or list value.".to_string(),
121+
))
126122
})
127123
}
128124

0 commit comments

Comments
 (0)