|
14 | 14 | spin_sdk::{ |
15 | 15 | config, |
16 | 16 | http::{Request, Response}, |
| 17 | +<<<<<<< HEAD |
17 | 18 | http_component, |
18 | 19 | key_value::Store, |
19 | 20 | outbound_http, redis, |
| 21 | +======= |
| 22 | + http_component, outbound_http, redis, |
| 23 | + redis::RedisResult, |
| 24 | +>>>>>>> 0081caf (initial commit) |
20 | 25 | }, |
21 | 26 | std::{ |
22 | 27 | collections::HashMap, |
@@ -331,6 +336,60 @@ fn math_rand(context: &Context, _this: &Value, _args: &[Value]) -> Result<Value> |
331 | 336 | context.value_from_f64(thread_rng().gen_range(0.0_f64..1.0)) |
332 | 337 | } |
333 | 338 |
|
| 339 | +fn redis_exec(context: &Context, _this: &Value, args: &[Value]) -> Result<Value> { |
| 340 | + println!("execcuting exec"); |
| 341 | + match args { |
| 342 | + [address, command, arguments] => { |
| 343 | + let address = &deserialize_helper(address)?; |
| 344 | + let command = &deserialize_helper(command)?; |
| 345 | + let mut arg: Vec<redis::RedisParameter> = vec![]; |
| 346 | + if arguments.is_array() { |
| 347 | + let mut props = arguments.properties()?; |
| 348 | + while let Some(ref _x) = props.next_key()? { |
| 349 | + let val = props.next_value()?; |
| 350 | + if val.is_big_int() { |
| 351 | + let deserializer = &mut Deserializer::from(val.clone()); |
| 352 | + let temp = i64::deserialize(deserializer)?; |
| 353 | + arg.push(redis::RedisParameter::Int64(temp)); |
| 354 | + } else if val.is_array_buffer() { |
| 355 | + let deserializer = &mut Deserializer::from(val.clone()); |
| 356 | + let temp = ByteBuf::deserialize(deserializer)?; |
| 357 | + arg.push(redis::RedisParameter::Binary(&temp)); |
| 358 | + } else { |
| 359 | + bail!("invalid argument type, must be bigint or arraybuffer") |
| 360 | + } |
| 361 | + } |
| 362 | + } else { |
| 363 | + bail!("invalid argument type, must be array") |
| 364 | + } |
| 365 | + let results = redis::execute(address, command, &arg) |
| 366 | + .map_err(|_| anyhow!("Error executing Redis execute command"))?; |
| 367 | + let arr = context.array_value()?; |
| 368 | + for result in results.iter() { |
| 369 | + match result { |
| 370 | + RedisResult::Nil => arr.append_property(context.undefined_value()?)?, |
| 371 | + RedisResult::Status(val) => { |
| 372 | + arr.append_property(context.value_from_str(&val)?)? |
| 373 | + } |
| 374 | + RedisResult::Int64(val) => { |
| 375 | + arr.append_property(context.value_from_i64(val.to_owned())?)? |
| 376 | + } |
| 377 | + RedisResult::Binary(val) => { |
| 378 | + let mut serializer = Serializer::from_context(context)?; |
| 379 | + val.serialize(&mut serializer)?; |
| 380 | + arr.append_property(serializer.value)?; |
| 381 | + } |
| 382 | + } |
| 383 | + } |
| 384 | + return Ok(arr); |
| 385 | + } |
| 386 | + _ => bail!( |
| 387 | + "expected a two arguments (address, key), got {} arguments", |
| 388 | + args.len() |
| 389 | + ), |
| 390 | + } |
| 391 | +} |
| 392 | + |
334 | 393 | fn redis_get(context: &Context, _this: &Value, args: &[Value]) -> Result<Value> { |
335 | 394 | match args { |
336 | 395 | [address, key] => { |
@@ -680,6 +739,7 @@ fn do_init() -> Result<()> { |
680 | 739 | redis.set_property("sadd", context.wrap_callback(redis_sadd)?)?; |
681 | 740 | redis.set_property("smembers", context.wrap_callback(redis_smembers)?)?; |
682 | 741 | redis.set_property("srem", context.wrap_callback(redis_srem)?)?; |
| 742 | + redis.set_property("execute", context.wrap_callback(redis_exec)?)?; |
683 | 743 |
|
684 | 744 | let kv = context.object_value()?; |
685 | 745 | kv.set_property("open", context.wrap_callback(open_kv)?)?; |
|
0 commit comments