Skip to content

Commit

Permalink
fix: query string overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
mosure committed May 15, 2024
1 parent 25dbf8e commit 0f06cc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bevy_args"
description = "bevy plugin to parse command line arguments and URL query parameters"
version = "1.4.0"
version = "1.4.1"
edition = "2021"
authors = ["mosure <[email protected]>"]
license = "MIT"
Expand All @@ -28,7 +28,7 @@ exclude = [
[dependencies]
clap = { version = "4.4", features = ["derive"] }
serde = "1.0"
serde_urlencoded = "0.7"
serde_json = "1.0"

[dependencies.bevy]
version = "0.13"
Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ fn update_struct<R>(mut instance: R, query_map: HashMap<String, String>) -> R
where
R: Serialize + for<'de> Deserialize<'de>,
{
let mut instance_json = serde_json::to_value(&instance).unwrap();

for (key, value) in query_map {
let update_map = [(key, value)].iter().cloned().collect::<HashMap<_, _>>();
let update_str = serde_urlencoded::to_string(update_map).unwrap();
let update_instance: R = serde_urlencoded::from_str(&update_str).unwrap_or(instance);
instance = update_instance;
if let Some(field) = instance_json.get_mut(&key) {
*field = serde_json::Value::String(value);
}
}

instance = serde_json::from_value(instance_json).unwrap();
instance
}

Expand Down

0 comments on commit 0f06cc4

Please sign in to comment.