Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike: returning a record replacement with glide strings. #418

8 changes: 8 additions & 0 deletions node/npm/glide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ function initialize() {
GlideClient,
GlideClusterClient,
GlideClientConfiguration,
GlideRecord,
GlideString,
SortedSetDataType,
StreamEntryDataType,
HashDataType,
FunctionListOptions,
FunctionListResponse,
FunctionStatsSingleResponse,
Expand Down Expand Up @@ -202,7 +206,11 @@ function initialize() {
Decoder,
DecoderOption,
GeoAddOptions,
GlideRecord,
GlideString,
SortedSetDataType,
StreamEntryDataType,
HashDataType,
CoordOrigin,
MemberOrigin,
SearchOrigin,
Expand Down
18 changes: 11 additions & 7 deletions node/rust-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use napi::bindgen_prelude::Uint8Array;
use napi::{Env, Error, JsObject, JsUnknown, Result, Status};
use napi_derive::napi;
use num_traits::sign::Signed;
use redis::{aio::MultiplexedConnection, AsyncCommands, FromRedisValue, Value};
use redis::{aio::MultiplexedConnection, AsyncCommands, Value};
#[cfg(feature = "testing_utilities")]
use std::collections::HashMap;
use std::str;
Expand Down Expand Up @@ -195,13 +195,17 @@ fn redis_value_to_js(val: Value, js_env: Env, string_decoder: bool) -> Result<Js
Ok(js_array_view.into_unknown())
}
Value::Map(map) => {
let mut obj = js_env.create_object()?;
for (key, value) in map {
let field_name = String::from_owned_redis_value(key).map_err(to_js_error)?;
let value = redis_value_to_js(value, js_env, string_decoder)?;
obj.set_named_property(&field_name, value)?;
// Convert map to array of key-value pairs instead of a `Record` (object),
// because `Record` does not support `GlideString` as a key.
// The result is in format `GlideRecord<T>`.
let mut js_array = js_env.create_array_with_length(map.len())?;
for (idx, (key, value)) in (0_u32..).zip(map.into_iter()) {
let mut obj = js_env.create_object()?;
obj.set_named_property("key", redis_value_to_js(key, js_env, string_decoder)?)?;
obj.set_named_property("value", redis_value_to_js(value, js_env, string_decoder)?)?;
js_array.set_element(idx, obj)?;
}
Ok(obj.into_unknown())
Ok(js_array.into_unknown())
}
Value::Double(float) => js_env.create_double(float).map(|val| val.into_unknown()),
Value::Boolean(bool) => js_env.get_boolean(bool).map(|val| val.into_unknown()),
Expand Down
Loading
Loading