Skip to content

Commit

Permalink
fix clippy and insert keys
Browse files Browse the repository at this point in the history
Signed-off-by: Runji Wang <[email protected]>
  • Loading branch information
wangrunji0408 committed Oct 19, 2023
1 parent b4a0925 commit f9dabaa
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/common/src/types/jsonb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a> ScalarRef<'a> for JsonbRef<'a> {
type ScalarType = JsonbVal;

fn to_owned_scalar(&self) -> Self::ScalarType {
JsonbVal(self.0.clone().into())
JsonbVal(self.0.into())
}

fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down Expand Up @@ -114,8 +114,7 @@ impl std::str::FromStr for JsonbVal {
type Err = <Value as std::str::FromStr>::Err;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let v: Value = s.parse()?;
Ok(Self(v.into()))
Ok(Self(s.parse()?))
}
}

Expand All @@ -139,10 +138,10 @@ impl JsonbVal {
pub fn memcmp_deserialize(
deserializer: &mut memcomparable::Deserializer<impl bytes::Buf>,
) -> memcomparable::Result<Self> {
let v: Value = <String as serde::Deserialize>::deserialize(deserializer)?
let v = <String as serde::Deserialize>::deserialize(deserializer)?
.parse()
.map_err(|_| memcomparable::Error::Message("invalid json".into()))?;
Ok(Self(v.into()))
Ok(Self(v))
}

/// Deserialize from a pgwire "BINARY" encoding.
Expand Down Expand Up @@ -172,17 +171,20 @@ impl JsonbVal {
/// # Panics
///
/// Panics if this is not a json object.
pub fn object_insert(&mut self, k: &str, v: Self) {
pub fn object_insert(&mut self, key: &str, value: Self) {
// TODO(runji): using `Builder` to rebuild a value is inefficient
// we may support `Value::object_insert` in the future
let mut builder = jsonbb::Builder::<Vec<u8>>::with_capacity(self.0.capacity());
builder.begin_object();
for (k, v) in self.0.as_object().unwrap().iter() {
if k == key {
continue;
}
builder.add_string(k);
builder.add_value(v);
}
builder.add_string(k);
builder.add_value(v.0.as_ref());
builder.add_string(key);
builder.add_value(value.0.as_ref());
builder.end_object();
self.0 = builder.finish();
}
Expand Down

0 comments on commit f9dabaa

Please sign in to comment.