Skip to content

Commit

Permalink
feat(named-args): refactor, implement QueryArg for HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFoxPro committed Mar 27, 2024
1 parent cb5ad3e commit 062d7a1
Showing 1 changed file with 61 additions and 49 deletions.
110 changes: 61 additions & 49 deletions edgedb-protocol/src/query_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,61 +529,59 @@ implement_tuple! {11, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, }
implement_tuple! {12, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }



pub struct ValueWithCardinality(Option<Value>, Cardinality);

impl<V: Into<Value>> From<V> for ValueWithCardinality
{
fn from(value: V) -> Self {
ValueWithCardinality(Some(value.into()), Cardinality::One)
}
impl<V: Into<Value>> From<V> for ValueWithCardinality {
fn from(value: V) -> Self {
ValueWithCardinality(Some(value.into()), Cardinality::One)
}
}

impl<V: Into<Value>> From<Option<V>> for ValueWithCardinality
where Value: From<V>
impl<V: Into<Value>> From<Option<V>> for ValueWithCardinality
where Value: From<V>
{
fn from(value: Option<V>) -> Self {
ValueWithCardinality(value.map(Value::from), Cardinality::AtMostOne)
}
fn from(value: Option<V>) -> Self {
ValueWithCardinality(value.map(Value::from), Cardinality::AtMostOne)
}
}

impl<V: Into<Value>> From<Vec<V>> for ValueWithCardinality
impl<V: Into<Value>> From<Vec<V>> for ValueWithCardinality
where Value: From<V>
{
fn from(value: Vec<V>) -> Self {
ValueWithCardinality(Some(Value::Array(value.into_iter().map(Value::from).collect())), Cardinality::One)
}
fn from(value: Vec<V>) -> Self {
ValueWithCardinality(
Some(Value::Array(value.into_iter().map(Value::from).collect())),
Cardinality::One
)
}
}

impl<V: Into<Value>> From<Option<Vec<V>>> for ValueWithCardinality
impl<V: Into<Value>> From<Option<Vec<V>>> for ValueWithCardinality
where Value: From<V>
{
fn from(value: Option<Vec<V>>) -> Self {
let mapped = value.map(|value|
Value::Array(value.into_iter().map(Value::from).collect())
);
ValueWithCardinality(mapped, Cardinality::AtMostOne)
}
fn from(value: Option<Vec<V>>) -> Self {
let mapped = value.map(|value| Value::Array(value.into_iter().map(Value::from).collect()));
ValueWithCardinality(mapped, Cardinality::AtMostOne)
}
}

pub fn object_from_pairs<K, V>(iter: impl IntoIterator<Item = (K, V)>) -> Value
pub fn object_from_pairs<K>(pairs: &HashMap<K, ValueWithCardinality>) -> Value
where
K: ToString,
V: Into<ValueWithCardinality>
{
let mut elements = Vec::new();
let mut fields: Vec<Option<Value>> = Vec::new();
for (key, arg) in iter.into_iter() {
let ValueWithCardinality(value, cd) = arg.into();
for (key, arg) in pairs.iter() {
let ValueWithCardinality(value, cd) = arg;

elements.push(ShapeElement {
name: key.to_string(),
cardinality: Some(cd),
flag_link: false,
flag_link_property: false,
flag_implicit: false
});
fields.push(value);
name: key.to_string(),
cardinality: Some(*cd),
flag_link: false,
flag_link_property: false,
flag_implicit: false
});
fields.push(value.clone());
}

Value::Object {
Expand All @@ -592,25 +590,39 @@ where
}
}

#[cfg(feature = "macros")]
pub mod macros {
use crate::query_arg::{object_from_pairs, Value, ValueWithCardinality};
use std::collections::HashMap;
impl<K: ToString> From<&HashMap<K, ValueWithCardinality>> for Value
{
fn from(value: &HashMap<K, ValueWithCardinality>) -> Self {
object_from_pairs(value)
}
}

impl<K> QueryArg for HashMap<K, ValueWithCardinality>
where
K: ToString + Send + Sync
{
fn to_value(&self) -> Result<Value, edgedb_errors::Error> {
Ok(Value::from(self))
}

pub struct EdgedbArgsIndexMap<'i>(pub std::collections::HashMap<&'i str, ValueWithCardinality>);
impl EdgedbArgsIndexMap<'_> {
pub fn to_value(self) -> Value {
Value::from(self)
}
fn encode_slot(&self, encoder: &mut Encoder) -> Result<(), edgedb_errors::Error> {
Value::from(self).encode_slot(encoder)
}

impl From<EdgedbArgsIndexMap<'_>> for Value {
fn from(value: EdgedbArgsIndexMap) -> Self {
object_from_pairs(value.0)
}
fn check_descriptor(
&self,
ctx: &DescriptorContext,
pos: crate::descriptors::TypePos
) -> Result<(), edgedb_errors::Error> {
Value::from(self).check_descriptor(ctx, pos)
}
}

#[macro_export]
macro_rules! eargs {
#[cfg(feature = "macros")]
pub mod macros {
#[macro_export]
macro_rules! eargs {
($($key:expr => $value:expr,)+) => { $crate::eargs!($($key => $value),+) };
($($key:expr => $value:expr),*) => {
{
Expand All @@ -619,8 +631,8 @@ pub mod macros {
$(
map.insert($key, $crate::query_arg::ValueWithCardinality::from($value));
)*
$crate::query_arg::macros::EdgedbArgsIndexMap(map)
map
}
};
}
}
}

0 comments on commit 062d7a1

Please sign in to comment.