From df8f297221ceda7cdac3e9d6cee2e19d0e16f1db Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Tue, 25 Oct 2022 06:23:18 -0700 Subject: [PATCH] Add WebSocket RPC `unset` method for clearing a parameter --- src/net/rpc.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/net/rpc.rs b/src/net/rpc.rs index fd6b6ff8..ebe0763f 100644 --- a/src/net/rpc.rs +++ b/src/net/rpc.rs @@ -256,6 +256,11 @@ impl Rpc { "select" => match params.take_one() { v if v.is_thing() => rpc.read().await.select(v).await, v if v.is_strand() => rpc.read().await.select(v).await, + // Unset and clear a connection-wide parameter + "unset" => match params.needs_one() { + Ok(Value::Strand(s)) => rpc.write().await.unset(s).await, + _ => return res::failure(id, Failure::INVALID_PARAMS).send(out, chn).await, + }, // Select a value or values from the database _ => return res::failure(id, Failure::INVALID_PARAMS).send(out, chn).await, }, @@ -377,17 +382,17 @@ impl Rpc { async fn set(&mut self, key: Strand, val: Value) -> Result { match val { - // Remove the variable if the value is NULL - v if v.is_null() => { - self.vars.remove(&key.0); - Ok(Value::Null) - } - // Store the variable if not NULL - v => { - self.vars.insert(key.0, v); - Ok(Value::Null) - } - } + // Remove the variable if undefined + Value::None => self.vars.remove(&key.0), + // Store the variable if defined + v => self.vars.insert(key.0, v), + }; + Ok(Value::Null) + } + + async fn unset(&mut self, key: Strand) -> Result { + self.vars.remove(&key.0); + Ok(Value::Null) } // ------------------------------