Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
adress the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
qrayven committed Nov 22, 2022
1 parent 3d80e40 commit 944ecbd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dpp/src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl Document {
/// The path supports syntax from `lodash` JS lib. Example: "root.people[0].name".
/// If parents are not present they will be automatically created
pub fn set(&mut self, path: &str, value: JsonValue) -> Result<(), ProtocolError> {
Ok(self.data.insert_with_parents(path, value)?)
Ok(self.data.insert_with_path(path, value)?)
}

/// Retrieves field specified by path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde_json::Value;

/// Inserts the value specified by the json path. If intermediate object doesn't exist, crates a one.
/// If `Value::Null` is encountered while traversing the path, they are replaced with the required structure.
pub(super) fn insert_with_parents(
pub(super) fn insert_with_path(
data: &mut Value,
json_path: &[JsonPathStep],
value: Value,
Expand Down Expand Up @@ -114,7 +114,7 @@ mod test_set {
JsonPathStep::Key("c".to_string()),
];

insert_with_parents(&mut data, &keys, json!("alpha")).expect("no errors");
insert_with_path(&mut data, &keys, json!("alpha")).expect("no errors");

assert_eq!(data["a"]["b"]["c"], json!("alpha"))
}
Expand All @@ -124,7 +124,7 @@ mod test_set {
let mut data = Value::Null;
let keys = [JsonPathStep::Index(0)];

insert_with_parents(&mut data, &keys, json!("alpha")).expect("no errors");
insert_with_path(&mut data, &keys, json!("alpha")).expect("no errors");

assert_eq!(data[0], json!("alpha"))
}
Expand All @@ -138,7 +138,7 @@ mod test_set {
JsonPathStep::Key("c".to_string()),
];

insert_with_parents(&mut data, &keys, json!("alpha")).expect("no errors");
insert_with_path(&mut data, &keys, json!("alpha")).expect("no errors");
assert_eq!(data["a"]["b"]["c"], json!("alpha"))
}

Expand All @@ -152,7 +152,7 @@ mod test_set {
JsonPathStep::Key("c".to_string()),
];

insert_with_parents(&mut data, &keys, json!("alpha")).expect("no errors");
insert_with_path(&mut data, &keys, json!("alpha")).expect("no errors");

assert_eq!(data["a"]["b"][0]["c"], json!("alpha"))
}
Expand All @@ -167,7 +167,7 @@ mod test_set {
JsonPathStep::Key("c".to_string()),
];

insert_with_parents(&mut data, &keys, json!("alpha")).expect("no errors");
insert_with_path(&mut data, &keys, json!("alpha")).expect("no errors");

assert_eq!(data["a"]["b"][0]["c"], Value::Null);
assert_eq!(data["a"]["b"][1]["c"], Value::Null);
Expand All @@ -189,7 +189,7 @@ mod test_set {
JsonPathStep::Key("c".to_string()),
];

insert_with_parents(&mut data, &keys, json!("alpha")).expect("no errors");
insert_with_path(&mut data, &keys, json!("alpha")).expect("no errors");

assert_eq!(data["a"]["b"][1]["c"], json!("alpha"));
}
Expand All @@ -201,7 +201,7 @@ mod test_set {
});
let keys = [JsonPathStep::Key("a".to_string())];

insert_with_parents(&mut data, &keys, json!("alpha")).expect("no errors");
insert_with_path(&mut data, &keys, json!("alpha")).expect("no errors");

assert_eq!(data["a"], json!("alpha"));
}
Expand All @@ -219,8 +219,7 @@ mod test_set {
JsonPathStep::Key("c".to_string()),
];

insert_with_parents(&mut data, &keys, json!("alpha"))
.expect_err("error should be returned");
insert_with_path(&mut data, &keys, json!("alpha")).expect_err("error should be returned");
}

#[test]
Expand All @@ -235,7 +234,7 @@ mod test_set {
JsonPathStep::Key("b".to_string()),
];

insert_with_parents(&mut data, &keys, json!("alpha")).expect("no errors");
insert_with_path(&mut data, &keys, json!("alpha")).expect("no errors");

assert_eq!(data["a"]["b"], json!("alpha"));
}
Expand All @@ -245,7 +244,7 @@ mod test_set {
let mut data = json!({ "a": [json!("already_taken")] });
let keys = [JsonPathStep::Key("a".to_string()), JsonPathStep::Index(0)];

insert_with_parents(&mut data, &keys, json!("alpha")).expect("no errors");
insert_with_path(&mut data, &keys, json!("alpha")).expect("no errors");

assert_eq!(data["a"][0], json!("alpha"));
}
Expand All @@ -263,6 +262,6 @@ mod test_set {
JsonPathStep::Index(0),
];

insert_with_parents(&mut data, &keys, json!("alpha")).expect_err("inserting error");
insert_with_path(&mut data, &keys, json!("alpha")).expect_err("inserting error");
}
}
14 changes: 7 additions & 7 deletions dpp/src/util/json_value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use super::{
string_encoding::Encoding,
};

mod insert_with_parents;
use insert_with_parents::*;
mod insert_with_path;
use insert_with_path::*;

const PROPERTY_CONTENT_MEDIA_TYPE: &str = "contentMediaType";
const PROPERTY_PROTOCOL_VERSION: &str = "protocolVersion";
Expand Down Expand Up @@ -82,7 +82,7 @@ pub trait JsonValueExt {

/// Insert value under the path. Path is dot-separated string. i.e `properties[0].id`. If parents don't
/// exists they will be created
fn insert_with_parents(&mut self, path: &str, value: JsonValue) -> Result<(), anyhow::Error>;
fn insert_with_path(&mut self, path: &str, value: JsonValue) -> Result<(), anyhow::Error>;
}

impl JsonValueExt for JsonValue {
Expand Down Expand Up @@ -358,14 +358,14 @@ impl JsonValueExt for JsonValue {
}

/// Insert value under the path. Path is dot-separated string. i.e `properties[0].id`
fn insert_with_parents(
fn insert_with_path(
&mut self,
string_path: &str,
value: JsonValue,
) -> Result<(), anyhow::Error> {
let path_literal: JsonPathLiteral = string_path.into();
let path: JsonPath = path_literal.try_into().unwrap();
insert_with_parents(self, &path, value)
insert_with_path(self, &path, value)
}
}

Expand Down Expand Up @@ -596,10 +596,10 @@ mod test {
});

document
.insert_with_parents("root.to.new_field", json!("new_value"))
.insert_with_path("root.to.new_field", json!("new_value"))
.expect("no errors");
document
.insert_with_parents("root.array[0].new_field", json!("new_value"))
.insert_with_path("root.array[0].new_field", json!("new_value"))
.expect("no errors");

assert_eq!(document["root"]["from"]["id"], json!("123"));
Expand Down

0 comments on commit 944ecbd

Please sign in to comment.