Skip to content

Commit 10b2e05

Browse files
committed
refactor: share one JSON section helper between the since and statements sections
Both sqb test --json sections are now added by sqlbuild-core's json with_section. The Python bindings cap follows the smaller crate.
1 parent 81367a6 commit 10b2e05

5 files changed

Lines changed: 32 additions & 27 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod dumps;
22
pub mod python_float_repr;
3+
pub mod with_section;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Add a named section to a JSON document.
2+
3+
use serde_json::Value;
4+
5+
/// `payload`, a JSON document, with `section` added under `name`, pretty-printed with a trailing newline.
6+
pub fn with_section(
7+
payload: &str,
8+
name: &str,
9+
section: Value,
10+
) -> Result<String, serde_json::Error> {
11+
let mut document: Value = serde_json::from_str(payload)?;
12+
if let Value::Object(fields) = &mut document {
13+
fields.insert(name.to_owned(), section);
14+
}
15+
Ok(format!("{}\n", serde_json::to_string_pretty(&document)?))
16+
}

‎crates/sqlbuild-python/src/bindings/_helpers/change_selection/selection.rs‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::path::Path;
66
use pyo3::exceptions::PyValueError;
77
use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyResult};
88
use pyo3::types::{PyDict, PyDictMethods};
9-
use serde_json::Value;
109
use sqlbuild_analysis::definition_changes::constants::{
1110
MACRO_KIND, MODEL_KIND, SCENARIO_KIND, SEED_KIND, SOURCE_KIND, TABLE_FUNCTION_KIND,
1211
TEST_FILE_KIND,
@@ -20,6 +19,7 @@ use sqlbuild_analysis::definition_changes::main::selection_report::selection_rep
2019
use sqlbuild_analysis::definition_changes::models::{
2120
DefinitionChanges, DefinitionSnapshot, ExecutedItem, SelectedItem,
2221
};
22+
use sqlbuild_core::json::main::with_section::with_section;
2323

2424
use crate::bindings::_helpers::change_selection::base_snapshot::BaseComparison;
2525
use crate::bindings::_helpers::change_selection::definition_snapshot::{
@@ -311,17 +311,12 @@ pub(crate) fn with_since_section(payload: String, since: Option<&str>) -> PyResu
311311
return Ok(payload);
312312
};
313313
let invalid = |error: serde_json::Error| PyValueError::new_err(error.to_string());
314-
let mut document: Value = serde_json::from_str(&payload).map_err(invalid)?;
315-
if let Value::Object(fields) = &mut document {
316-
fields.insert(
317-
SINCE_SECTION.to_owned(),
318-
serde_json::from_str(since).map_err(invalid)?,
319-
);
320-
}
321-
Ok(format!(
322-
"{}\n",
323-
serde_json::to_string_pretty(&document).map_err(invalid)?
324-
))
314+
with_section(
315+
&payload,
316+
SINCE_SECTION,
317+
serde_json::from_str(since).map_err(invalid)?,
318+
)
319+
.map_err(invalid)
325320
}
326321

327322
/// The project's compiled tests and models by name.

‎crates/sqlbuild-python/src/bindings/_helpers/native_host/statement_report.rs‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::time::Instant;
55
use pyo3::exceptions::PyException;
66
use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyResult, Python};
77
use pyo3::types::{PyDict, PyDictMethods, PyTypeMethods};
8-
use serde_json::Value;
98
use sqlbuild_core::cost_allocation::main::statement_timings_sql::statement_timings_sql;
9+
use sqlbuild_core::json::main::with_section::with_section;
1010
use sqlbuild_core::progress::main::format_statement_summary::format_statement_summary;
1111
use sqlbuild_core::progress::main::statement_summary_json::statement_summary_json;
1212
use sqlbuild_core::progress::models::{StatementSummary, StatementTiming, StatementTimings};
@@ -74,19 +74,12 @@ impl StatementReport {
7474

7575
/// `payload` with the `statements` section added.
7676
pub(crate) fn with_section(&self, payload: &str) -> PyResult<String> {
77-
let invalid =
78-
|error: serde_json::Error| pyo3::exceptions::PyValueError::new_err(error.to_string());
79-
let mut document: Value = serde_json::from_str(payload).map_err(invalid)?;
80-
if let Value::Object(fields) = &mut document {
81-
fields.insert(
82-
STATEMENTS_SECTION.to_owned(),
83-
statement_summary_json(&self.summary, &self.timing),
84-
);
85-
}
86-
Ok(format!(
87-
"{}\n",
88-
serde_json::to_string_pretty(&document).map_err(invalid)?
89-
))
77+
with_section(
78+
payload,
79+
STATEMENTS_SECTION,
80+
statement_summary_json(&self.summary, &self.timing),
81+
)
82+
.map_err(|error| pyo3::exceptions::PyValueError::new_err(error.to_string()))
9083
}
9184
}
9285

‎fensu.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ select = ["FPRS", "XSB072", "XSB073", "XSB075", "XSB076", "XSB077", "XSB078", "X
16701670

16711671
[[targets.rust.threshold_overrides]]
16721672
paths = ["crates/sqlbuild-python/Cargo.toml"]
1673-
thresholds = { max_crate_code_lines = 147801 }
1673+
thresholds = { max_crate_code_lines = 147789 }
16741674
reason = "The Python bindings crate is above the code-line limit; this caps it at its current size until it is split into crates by area. Lower the cap as code moves out."
16751675

16761676
[[targets.rust.threshold_overrides]]

0 commit comments

Comments
 (0)