| 14 | use super::session::RespSession; |
| 15 | |
| 16 | pub(super) async fn handle_hget( |
| 17 | cmd: &RespCommand, |
| 18 | session: &RespSession, |
| 19 | state: &SharedState, |
| 20 | ) -> RespValue { |
| 21 | if cmd.argc() < 2 { |
| 22 | return RespValue::err("ERR wrong number of arguments for 'hget' command"); |
| 23 | } |
| 24 | |
| 25 | let key = cmd.args[0].clone(); |
| 26 | let field = cmd.arg_str(1).unwrap_or("").to_string(); |
| 27 | |
| 28 | let plan = PhysicalPlan::Kv(KvOp::FieldGet { |
| 29 | collection: session.collection.clone(), |
| 30 | key, |
| 31 | fields: vec![field.clone()], |
| 32 | }); |
| 33 | |
| 34 | match dispatch_kv(state, session, plan).await { |
| 35 | Ok(resp) if resp.status == Status::Ok => { |
| 36 | let json: serde_json::Value = sonic_rs::from_slice(&resp.payload).unwrap_or_default(); |
| 37 | match json.get(&field) { |
| 38 | Some(serde_json::Value::Null) | None => RespValue::nil(), |
| 39 | Some(serde_json::Value::String(s)) => RespValue::bulk_str(s), |
| 40 | Some(v) => RespValue::bulk(v.to_string().into_bytes()), |
| 41 | } |
| 42 | } |
| 43 | Ok(_) => RespValue::nil(), |
| 44 | Err(e) => RespValue::err(format!("ERR {e}")), |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub(super) async fn handle_hmget( |
| 49 | cmd: &RespCommand, |