Skip to content

Commit 814e47e

Browse files
committed
feat: enhance config loading and saving with null field stripping; update response structure in handlers; adjust playwright config command
1 parent a142049 commit 814e47e

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

jkconfig-web/src/config.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ pub fn load_config(path: &Path) -> Result<Option<Value>> {
5353
return Ok(None);
5454
}
5555

56-
let ext = path
57-
.extension()
58-
.and_then(|s| s.to_str())
59-
.unwrap_or("");
56+
let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("");
6057

6158
let value = match ext {
6259
"json" => serde_json::from_str(&content)?,
@@ -72,15 +69,31 @@ pub fn load_config(path: &Path) -> Result<Option<Value>> {
7269
Ok(Some(value))
7370
}
7471

72+
/// 递归删除 JSON Value 中所有 null 字段(TOML 不支持 null)
73+
fn strip_nulls(value: Value) -> Value {
74+
match value {
75+
Value::Object(map) => {
76+
let cleaned = map
77+
.into_iter()
78+
.filter(|(_, v)| !v.is_null())
79+
.map(|(k, v)| (k, strip_nulls(v)))
80+
.collect();
81+
Value::Object(cleaned)
82+
}
83+
Value::Array(arr) => Value::Array(arr.into_iter().map(strip_nulls).collect()),
84+
other => other,
85+
}
86+
}
87+
7588
/// 保存配置文件
7689
pub fn save_config(path: &Path, value: &Value) -> Result<()> {
77-
let ext = path
78-
.extension()
79-
.and_then(|s| s.to_str())
80-
.unwrap_or("");
90+
let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("");
8191

8292
let content = match ext {
83-
"toml" | "tml" => toml::to_string_pretty(value)?,
93+
"toml" | "tml" => {
94+
let cleaned = strip_nulls(value.clone());
95+
toml::to_string_pretty(&cleaned)?
96+
}
8497
"json" => serde_json::to_string_pretty(value)?,
8598
_ => {
8699
return Err(Error::UnsupportedFileType(ext.to_string()));

jkconfig-web/src/web/handlers.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,21 @@ pub async fn update_config_handler(
4747
) -> impl IntoResponse {
4848
if let Some(config) = payload.get("config") {
4949
state.jkconfig.update_config(config.clone()).await;
50-
Json(json!({
51-
"success": true,
52-
"message": "Configuration updated successfully"
53-
}))
50+
(
51+
StatusCode::OK,
52+
Json(json!({
53+
"success": true,
54+
"message": "Configuration updated successfully"
55+
})),
56+
)
5457
} else {
55-
Json(json!({
56-
"success": false,
57-
"error": "Missing 'config' field in request"
58-
}))
58+
(
59+
StatusCode::BAD_REQUEST,
60+
Json(json!({
61+
"success": false,
62+
"error": "Missing 'config' field in request"
63+
})),
64+
)
5965
}
6066
}
6167

jkconfig-web/tests/e2e/playwright.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default defineConfig({
2424
},
2525
],
2626
webServer: {
27-
command: 'cargo run -p jkconfig-web -- --port 3000 --schema tests/test-schema.json',
27+
command: 'cd .. && cargo run -p jkconfig-web -- --port 3000 --schema tests/test-schema.json',
2828
url: 'http://localhost:3000/api/health',
2929
reuseExistingServer: !process.env.CI,
3030
timeout: 120000,

jkconfig-web/web/src/ConfigEditor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ export const ConfigEditor: React.FC = () => {
166166
schema={schema}
167167
formData={config}
168168
onChange={handleChange}
169+
onSubmit={() => handleSave()}
169170
validator={validator}
170171
liveValidate
172+
uiSchema={{ 'ui:submitButtonOptions': { norender: true } }}
171173
/>
172174
</Box>
173175
</Paper>

0 commit comments

Comments
 (0)