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
2 changes: 1 addition & 1 deletion src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn json_api_get_json<M: Manager>(
str: *mut *mut rawmod::RedisModuleString,
) -> c_int {
let json = unsafe { &*(json as *const M::V) };
let res = KeyValue::new(json).to_value(json).to_string();
let res = KeyValue::<M::V>::serialize_object(json, None, None, None);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does the <M::V> is mandatory? the compiler will not conclude it from the json argument?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Without it we get error[E0283]: type annotations needed
(probably since V and O could be different types)

create_rmstring(ctx, &res, str)
}

Expand Down
47 changes: 11 additions & 36 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::error::Error;

use crate::redisjson::SetOptions;

use serde_json::{Map, Value};
use serde_json::Value;

use serde::Serialize;
use std::collections::HashMap;
Expand Down Expand Up @@ -62,30 +62,6 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
KeyValue { val: v }
}

pub fn to_value(&self, val: &V) -> Value {
match val.get_type() {
SelectValueType::Null => Value::Null,
SelectValueType::Bool => Value::Bool(val.get_bool()),
SelectValueType::String => Value::String(val.get_str()),
SelectValueType::Long => val.get_long().into(),
SelectValueType::Double => val.get_double().into(),
SelectValueType::Array => {
let mut arr = Vec::new();
for v in val.values().unwrap() {
arr.push(self.to_value(v));
}
Value::Array(arr)
}
SelectValueType::Object => {
let mut m = Map::new();
for (k, v) in val.items().unwrap() {
m.insert(k.to_string(), self.to_value(v));
}
Value::Object(m)
}
}
}

fn get_first<'b>(&'a self, path: &'b str) -> Result<&'a V, Error> {
let results = self.get_values(path)?;
match results.first() {
Expand Down Expand Up @@ -146,8 +122,7 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
Ok(results)
}

fn serialize_object<O: Serialize>(
&'a self,
pub fn serialize_object<O: Serialize>(
o: &O,
indent: Option<&str>,
newline: Option<&str>,
Expand Down Expand Up @@ -189,20 +164,20 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
acc.insert(path.get_original(), value);
acc
});
Ok(self
.serialize_object(&temp_doc, indent, newline, space)
.into())
Ok(Self::serialize_object(&temp_doc, indent, newline, space).into())
} else {
let path = &paths[0];
if path.is_legacy() {
Ok(self
.serialize_object(self.get_first(paths[0].get_path())?, indent, newline, space)
.into())
Ok(Self::serialize_object(
self.get_first(paths[0].get_path())?,
indent,
newline,
space,
)
.into())
} else {
let values = self.get_values(path.get_path())?;
Ok(self
.serialize_object(&values, indent, newline, space)
.into())
Ok(Self::serialize_object(&values, indent, newline, space).into())
}
}
}
Expand Down