Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
let temp_doc = paths.drain(..).fold(HashMap::new(), |mut acc, path| {
let mut selector = Selector::new();
selector.value(self.val);
if let Err(_) = selector.str_path(&path.fixed) {
if let Err(_) = selector.str_path(path.get_path()) {
return acc;
}
let value = match selector.select() {
Expand All @@ -190,20 +190,25 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
},
Err(_) => None,
};
acc.insert(path.path, value);
acc.insert(path.take_original(), value);
acc
});
Ok(self
.serialize_object(&temp_doc, &indent, &newline, &space)
.into())
} else {
let path = &paths[0];
if path.is_legacy {
if path.is_legacy() {
Ok(self
.serialize_object(self.get_first(&paths[0].fixed)?, &indent, &newline, &space)
.serialize_object(
self.get_first(paths[0].get_path())?,
&indent,
&newline,
&space,
)
.into())
} else {
let values = self.get_values(&path.fixed)?;
let values = self.get_values(path.get_path())?;
Ok(self
.serialize_object(&values, &indent, &newline, &space)
.into())
Expand Down Expand Up @@ -1071,7 +1076,7 @@ pub fn command_json_clear<M: Manager>(manager: M, ctx: &Context, args: Vec<Strin
paths
};

let path = paths.first().unwrap().fixed.as_str();
let path = paths.first().unwrap().get_path();

// FIXME: handle multi paths
let mut redis_key = manager.open_key_write(ctx, &key)?;
Expand All @@ -1080,7 +1085,7 @@ pub fn command_json_clear<M: Manager>(manager: M, ctx: &Context, args: Vec<Strin
.get_value()?
.ok_or_else(RedisError::nonexistent_key)?;

let paths = find_paths(&path, root, |_v| true)?;
let paths = find_paths(path, root, |_v| true)?;
if paths.len() > 0 {
let mut res = None;
for p in paths {
Expand Down
43 changes: 27 additions & 16 deletions src/redisjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,42 @@ impl Format {
/// Backwards compatibility convertor for RedisJSON 1.x clients
///
pub struct Path {
pub path: String,
pub fixed: String,
pub is_legacy: bool,
original_path: String,
fixed_path: Option<String>,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought,
Can we even consider (also later) avoiding the fixed_path member and calculate it when calling get_path which will return the moved String? (either original or fixed if needed)
(assuming that once we pass it to JSONPath selector we don't need it anymore)
This will shorten the lifetime of the cloned/mutated strings to the minimum, but it will probably have none to very small impact (unless paths: &mut Vec<Path> vector in method to_json is big)

}

impl Path {
pub fn new(path: String) -> Path {
let mut fixed = path.clone();
let mut is_legacy = false;
if !fixed.starts_with('$') {
is_legacy = true;
if fixed == "." {
fixed.replace_range(..1, "$");
} else if fixed.starts_with('.') {
fixed.insert(0, '$');
let fixed_path = if path.starts_with('$') {
None
} else {
let mut cloned = path.clone();
if path == "." {
cloned.replace_range(..1, "$");
} else if path.starts_with('.') {
cloned.insert(0, '$')
} else {
fixed.insert_str(0, "$.");
cloned.insert_str(0, "$.");
}
}
Some(cloned)
};
Path {
path,
fixed,
is_legacy,
original_path: path,
fixed_path,
}
}

pub fn is_legacy(&self) -> bool {
self.fixed_path.is_some()
}

pub fn get_path(&self) -> &String {
self.fixed_path.as_ref().unwrap_or(&self.original_path)
}

pub fn take_original(self) -> String {
self.original_path
}
}

#[derive(Debug)]
Expand Down