use crate::{
commands,
interfaces::{ClientLike, FredResult},
types::{FromValue, Key, MultipleKeys, MultipleStrings, SetOptions},
};
use bytes_utils::Str;
use fred_macros::rm_send_if;
use futures::Future;
use serde_json::Value;
#[cfg_attr(docsrs, doc(cfg(feature = "i-redis-json")))]
#[rm_send_if(feature = "glommio")]
pub trait RedisJsonInterface: ClientLike + Sized {
fn json_arrappend<R, K, P, V>(&self, key: K, path: P, values: Vec<V>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
V: Into<Value> + Send,
{
async move {
into!(key, path);
let values = values.into_iter().map(|v| v.into()).collect();
commands::redis_json::json_arrappend(self, key, path, values)
.await?
.convert()
}
}
fn json_arrindex<R, K, P, V>(
&self,
key: K,
path: P,
value: V,
start: Option<i64>,
stop: Option<i64>,
) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
V: Into<Value> + Send,
{
async move {
into!(key, path, value);
commands::redis_json::json_arrindex(self, key, path, value, start, stop)
.await?
.convert()
}
}
fn json_arrinsert<R, K, P, V>(
&self,
key: K,
path: P,
index: i64,
values: Vec<V>,
) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
V: Into<Value> + Send,
{
async move {
into!(key, path);
let values = values.into_iter().map(|v| v.into()).collect();
commands::redis_json::json_arrinsert(self, key, path, index, values)
.await?
.convert()
}
}
fn json_arrlen<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key);
let path = path.map(|p| p.into());
commands::redis_json::json_arrlen(self, key, path).await?.convert()
}
}
fn json_arrpop<R, K, P>(
&self,
key: K,
path: Option<P>,
index: Option<i64>,
) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key);
let path = path.map(|p| p.into());
commands::redis_json::json_arrpop(self, key, path, index)
.await?
.convert()
}
}
fn json_arrtrim<R, K, P>(
&self,
key: K,
path: P,
start: i64,
stop: i64,
) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key, path);
commands::redis_json::json_arrtrim(self, key, path, start, stop)
.await?
.convert()
}
}
fn json_clear<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key);
let path = path.map(|p| p.into());
commands::redis_json::json_clear(self, key, path).await?.convert()
}
}
fn json_debug_memory<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key);
let path = path.map(|p| p.into());
commands::redis_json::json_debug_memory(self, key, path)
.await?
.convert()
}
}
fn json_del<R, K, P>(&self, key: K, path: P) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key, path);
commands::redis_json::json_del(self, key, path).await?.convert()
}
}
fn json_get<R, K, I, N, S, P>(
&self,
key: K,
indent: Option<I>,
newline: Option<N>,
space: Option<S>,
paths: P,
) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
I: Into<Str> + Send,
N: Into<Str> + Send,
S: Into<Str> + Send,
P: Into<MultipleStrings> + Send,
{
async move {
into!(key, paths);
let indent = indent.map(|v| v.into());
let newline = newline.map(|v| v.into());
let space = space.map(|v| v.into());
commands::redis_json::json_get(self, key, indent, newline, space, paths)
.await?
.convert()
}
}
fn json_merge<R, K, P, V>(&self, key: K, path: P, value: V) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
V: Into<Value> + Send,
{
async move {
into!(key, path, value);
commands::redis_json::json_merge(self, key, path, value)
.await?
.convert()
}
}
fn json_mget<R, K, P>(&self, keys: K, path: P) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<MultipleKeys> + Send,
P: Into<Str> + Send,
{
async move {
into!(keys, path);
commands::redis_json::json_mget(self, keys, path).await?.convert()
}
}
fn json_mset<R, K, P, V>(&self, values: Vec<(K, P, V)>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
V: Into<Value> + Send,
{
async move {
let values = values
.into_iter()
.map(|(k, p, v)| (k.into(), p.into(), v.into()))
.collect();
commands::redis_json::json_mset(self, values).await?.convert()
}
}
fn json_numincrby<R, K, P, V>(&self, key: K, path: P, value: V) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
V: Into<Value> + Send,
{
async move {
into!(key, path, value);
commands::redis_json::json_numincrby(self, key, path, value)
.await?
.convert()
}
}
fn json_objkeys<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key);
let path = path.map(|p| p.into());
commands::redis_json::json_objkeys(self, key, path).await?.convert()
}
}
fn json_objlen<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key);
let path = path.map(|p| p.into());
commands::redis_json::json_objlen(self, key, path).await?.convert()
}
}
fn json_resp<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key);
let path = path.map(|p| p.into());
commands::redis_json::json_resp(self, key, path).await?.convert()
}
}
fn json_set<R, K, P, V>(
&self,
key: K,
path: P,
value: V,
options: Option<SetOptions>,
) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
V: Into<Value> + Send,
{
async move {
into!(key, path, value);
commands::redis_json::json_set(self, key, path, value, options)
.await?
.convert()
}
}
fn json_strappend<R, K, P, V>(
&self,
key: K,
path: Option<P>,
value: V,
) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
V: Into<Value> + Send,
{
async move {
into!(key, value);
let path = path.map(|p| p.into());
commands::redis_json::json_strappend(self, key, path, value)
.await?
.convert()
}
}
fn json_strlen<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key);
let path = path.map(|p| p.into());
commands::redis_json::json_strlen(self, key, path).await?.convert()
}
}
fn json_toggle<R, K, P>(&self, key: K, path: P) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key, path);
commands::redis_json::json_toggle(self, key, path).await?.convert()
}
}
fn json_type<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
K: Into<Key> + Send,
P: Into<Str> + Send,
{
async move {
into!(key);
let path = path.map(|p| p.into());
commands::redis_json::json_type(self, key, path).await?.convert()
}
}
}