Use static str where possible for context param names (#1942)

This commit is contained in:
Tobie Morgan Hitchcock 2023-05-06 21:49:34 +01:00 committed by GitHub
parent 531e75a5b0
commit f0e5753cd1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 37 deletions

View file

@ -28,7 +28,7 @@ pub struct Context<'a> {
// Whether or not this context is cancelled.
cancelled: Arc<AtomicBool>,
// A collection of read only values stored in this context.
values: HashMap<String, Cow<'a, Value>>,
values: HashMap<Cow<'static, str>, Cow<'a, Value>>,
}
impl<'a> Default for Context<'a> {
@ -93,11 +93,12 @@ impl<'a> Context<'a> {
/// Add a value to the context. It overwrites any previously set values
/// with the same key.
pub fn add_value<V>(&mut self, key: String, value: V)
pub fn add_value<K, V>(&mut self, key: K, value: V)
where
K: Into<Cow<'static, str>>,
V: Into<Cow<'a, Value>>,
{
self.values.insert(key, value.into());
self.values.insert(key.into(), value.into());
}
/// Get the timeout for this operation, if any. This is useful for

View file

@ -110,14 +110,14 @@ impl<'a> Executor<'a> {
async fn set_ns(&self, ctx: &mut Context<'_>, opt: &mut Options, ns: &str) {
let mut session = ctx.value("session").unwrap_or(&Value::None).clone();
session.put(NS.as_ref(), ns.to_owned().into());
ctx.add_value(String::from("session"), session);
ctx.add_value("session", session);
opt.ns = Some(ns.into());
}
async fn set_db(&self, ctx: &mut Context<'_>, opt: &mut Options, db: &str) {
let mut session = ctx.value("session").unwrap_or(&Value::None).clone();
session.put(DB.as_ref(), db.to_owned().into());
ctx.add_value(String::from("session"), session);
ctx.add_value("session", session);
opt.db = Some(db.into());
}

View file

@ -93,19 +93,15 @@ impl Session {
/// Convert a session into a runtime
pub(crate) fn context<'a>(&self, mut ctx: Context<'a>) -> Context<'a> {
// Add auth data
let key = String::from("auth");
let val: Value = self.sd.to_owned().into();
ctx.add_value(key, val);
ctx.add_value("auth", val);
// Add scope data
let key = String::from("scope");
let val: Value = self.sc.to_owned().into();
ctx.add_value(key, val);
ctx.add_value("scope", val);
// Add token data
let key = String::from("token");
let val: Value = self.tk.to_owned().into();
ctx.add_value(key, val);
ctx.add_value("token", val);
// Add session value
let key = String::from("session");
let val: Value = Value::from(map! {
"db".to_string() => self.db.to_owned().into(),
"id".to_string() => self.id.to_owned().into(),
@ -116,7 +112,7 @@ impl Session {
"sd".to_string() => self.sd.to_owned().into(),
"tk".to_string() => self.tk.to_owned().into(),
});
ctx.add_value(key, val);
ctx.add_value("session", val);
// Output context
ctx
}

View file

@ -68,7 +68,7 @@ impl<'a> Document<'a> {
let mut ctx = Context::new(ctx);
// Add insertable value
if let Workable::Insert(value) = &self.extras {
ctx.add_value("input".into(), value);
ctx.add_value("input", value);
}
// Process ON DUPLICATE KEY clause
for x in x.iter() {

View file

@ -37,10 +37,10 @@ impl<'a> Document<'a> {
};
// Configure the context
let mut ctx = Context::new(ctx);
ctx.add_value("event".into(), met);
ctx.add_value("value".into(), self.current.deref());
ctx.add_value("after".into(), self.current.deref());
ctx.add_value("before".into(), self.initial.deref());
ctx.add_value("event", met);
ctx.add_value("value", self.current.deref());
ctx.add_value("after", self.current.deref());
ctx.add_value("before", self.initial.deref());
// Process conditional clause
let val = ev.when.compute(&ctx, opt, txn, Some(&self.current)).await?;
// Execute event if value is truthy

View file

@ -62,10 +62,10 @@ impl<'a> Document<'a> {
if let Some(expr) = &fd.value {
// Configure the context
let mut ctx = Context::new(ctx);
ctx.add_value("input".into(), &inp);
ctx.add_value("value".into(), &val);
ctx.add_value("after".into(), &val);
ctx.add_value("before".into(), &old);
ctx.add_value("input", &inp);
ctx.add_value("value", &val);
ctx.add_value("after", &val);
ctx.add_value("before", &old);
// Process the VALUE clause
val = expr.compute(&ctx, opt, txn, Some(&self.current)).await?;
}
@ -90,10 +90,10 @@ impl<'a> Document<'a> {
if let Some(expr) = &fd.assert {
// Configure the context
let mut ctx = Context::new(ctx);
ctx.add_value("input".into(), &inp);
ctx.add_value("value".into(), &val);
ctx.add_value("after".into(), &val);
ctx.add_value("before".into(), &old);
ctx.add_value("input", &inp);
ctx.add_value("value", &val);
ctx.add_value("after", &val);
ctx.add_value("before", &old);
// Process the ASSERT clause
if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
return Err(Error::FieldValue {
@ -121,10 +121,10 @@ impl<'a> Document<'a> {
let opt = &opt.perms(false);
// Configure the context
let mut ctx = Context::new(ctx);
ctx.add_value("input".into(), &inp);
ctx.add_value("value".into(), &val);
ctx.add_value("after".into(), &val);
ctx.add_value("before".into(), &old);
ctx.add_value("input", &inp);
ctx.add_value("value", &val);
ctx.add_value("after", &val);
ctx.add_value("before", &old);
// Process the PERMISSION clause
if !e.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
val = old

View file

@ -72,7 +72,7 @@ impl<'a> Document<'a> {
let val = self.current.pick(k);
// Configure the context
let mut ctx = Context::new(ctx);
ctx.add_value("value".into(), &val);
ctx.add_value("value", &val);
// Process the PERMISSION clause
if !e
.compute(&ctx, opt, txn, Some(&self.current))

View file

@ -81,7 +81,7 @@ impl Subquery {
let mut ctx = Context::new(ctx);
// Add parent document
if let Some(doc) = doc {
ctx.add_value("parent".into(), doc);
ctx.add_value("parent", doc);
}
// Process subquery
match v.compute(&ctx, opt, txn, doc).await? {
@ -103,7 +103,7 @@ impl Subquery {
let mut ctx = Context::new(ctx);
// Add parent document
if let Some(doc) = doc {
ctx.add_value("parent".into(), doc);
ctx.add_value("parent", doc);
}
// Process subquery
match v.compute(&ctx, opt, txn, doc).await? {
@ -125,7 +125,7 @@ impl Subquery {
let mut ctx = Context::new(ctx);
// Add parent document
if let Some(doc) = doc {
ctx.add_value("parent".into(), doc);
ctx.add_value("parent", doc);
}
// Process subquery
match v.compute(&ctx, opt, txn, doc).await? {
@ -147,7 +147,7 @@ impl Subquery {
let mut ctx = Context::new(ctx);
// Add parent document
if let Some(doc) = doc {
ctx.add_value("parent".into(), doc);
ctx.add_value("parent", doc);
}
// Process subquery
match v.compute(&ctx, opt, txn, doc).await? {
@ -169,7 +169,7 @@ impl Subquery {
let mut ctx = Context::new(ctx);
// Add parent document
if let Some(doc) = doc {
ctx.add_value("parent".into(), doc);
ctx.add_value("parent", doc);
}
// Process subquery
match v.compute(&ctx, opt, txn, doc).await? {
@ -191,7 +191,7 @@ impl Subquery {
let mut ctx = Context::new(ctx);
// Add parent document
if let Some(doc) = doc {
ctx.add_value("parent".into(), doc);
ctx.add_value("parent", doc);
}
// Process subquery
match v.compute(&ctx, opt, txn, doc).await? {