From c0c99d33b7c979bd23f3f1e879f612d9c3b5c71d Mon Sep 17 00:00:00 2001 From: Micha de Vries Date: Thu, 18 Jul 2024 09:07:57 +0200 Subject: [PATCH] Allow unsetting ns/db on rpc (#4373) --- core/src/rpc/rpc_context.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/core/src/rpc/rpc_context.rs b/core/src/rpc/rpc_context.rs index f993ed95..435099fb 100644 --- a/core/src/rpc/rpc_context.rs +++ b/core/src/rpc/rpc_context.rs @@ -85,13 +85,30 @@ pub trait RpcContext { // ------------------------------ async fn yuse(&mut self, params: Array) -> Result, RpcError> { + // For both ns+db, string = change, null = unset, none = do nothing + // We need to be able to adjust either ns or db without affecting the other + // To be able to select a namespace, and then list resources in that namespace, as an example let (ns, db) = params.needs_two()?; - if let Value::Strand(ns) = ns { + let unset_ns = matches!(ns, Value::Null); + let unset_db = matches!(db, Value::Null); + + // If we unset the namespace, we must also unset the database + if unset_ns && !unset_db { + return Err(RpcError::InvalidParams); + } + + if unset_ns { + self.session_mut().ns = None; + } else if let Value::Strand(ns) = ns { self.session_mut().ns = Some(ns.0); } - if let Value::Strand(db) = db { + + if unset_db { + self.session_mut().db = None; + } else if let Value::Strand(db) = db { self.session_mut().db = Some(db.0); } + Ok(Value::None) }