Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No exposed ability to grab custom maps from output #108

Open
caamartin35 opened this issue Aug 14, 2023 · 4 comments
Open

No exposed ability to grab custom maps from output #108

caamartin35 opened this issue Aug 14, 2023 · 4 comments

Comments

@caamartin35
Copy link
Contributor

Right now as I see it there is no ability to grab a custom map from the output. This is important when aggregating results using something like collect. Specifically, I have code that collects three nodes into a map as below:

MATCH (n:Model) 
      OPTIONAL MATCH (n)-[:LeftLink]->(d_left)
      OPTIONAL MATCH (n)-[:RightLink]->(d_right) 
      RETURN collect({{ node:n, d_left:d_left, d_right:d_right }}) as res;

This result in res being a BoltList of BoltMaps. Unfortunately because BoltMap isnt exposed, I cannot unpack this with something like let maps = row.get::<Vec<BoltMap>>("res").unwrap(); and because Row does not implement From<BoltType> I cannot use Row either.

I have made local changes that include a new exposed type call Map that wraps BoltMap and a exposes a single get method to allow users to use arbitrary output from the graph.

// row.rs

/// A map very similar to a `HashMap` that implements `TryFrom<BoltType>` to allow for retrieval.
#[derive(Debug)]
pub struct Map {
  inner: BoltMap,
}

impl Map {
  pub fn new(inner: BoltMap) -> Self {
    Map { inner }
  }

  pub fn get<T: std::convert::TryFrom<BoltType>>(&self, key: &str) -> Option<T> {
    self.inner.get(key)
  }
}
// convert.rs

impl TryFrom<BoltType> for Map {
  type Error = Error;

  fn try_from(input: BoltType) -> Result<Map> {
    match input {
      BoltType::Map(n) => Ok(Map::new(n)),
      _ => Err(Error::ConversionError),
    }
  }
}

If these changes seem reasonable I can create a pull request, or really any feedback on this would be appreciated. Maybe I am missing something already available to do this. Thanks!

@tausifcreates
Copy link

tausifcreates commented Sep 25, 2023

+1 please open a pr

@knutwalker
Copy link
Collaborator

You're right, getting a BoltMap is not possible right now, and there are other Bolt types that are not publicly accessible.

Before we're talking about adding a PR for this, I am currently rewriting a lot of the "get" functions to be based on serde instead of various TryFrom<BoltType>. It's not done yet, but you could play around with it if you want, it's in my fork: https://github.com/knutwalker/neo4rs/tree/serde-get

With this change .get::<Vec<BoltMap>> should work, as should .get::<CustomStruct> where CustomStruct is something that implements serde::Deserialize. The next release is planned for when the serde integration is finished, but I can't give a date for this. I would prefer to not do anymore in between releases that expose more of the BoltTypes, only for it to be changed again once the serde stuff is done.

@tausifcreates
Copy link

You're right, getting a BoltMap is not possible right now...

Thanks so much for your update, Eagerly waiting for this feat to drop.

@knutwalker
Copy link
Collaborator

We've just release 0.7.0-rc.1, the following should work (in fact, I'm gonna add this as a test case):

#[derive(Debug, Deserialize, PartialEq)]
struct Model {
    node: Node,
    d_left: Option<Node>,
    d_right: Option<Node>,
}

// optional if you want to map the row itself, not just the `res` column
#[derive(Deserialize)]
struct Res {
    res: Vec<Model>,
}

let model1 = BoltNode::new(BoltInteger::new(1), BoltList::new(), BoltMap::new());
let model2 = BoltNode::new(BoltInteger::new(2), BoltList::new(), BoltMap::new());
let models = HashMap::from_iter([("node", model1), ("d_left", model2)]);
let models = BoltList::from(vec![models.into()]);

let row = Row::new(
    vec!["res".into()].into(),
    vec![BoltType::List(models)].into(),
);

let m1 = row.get::<Vec<Model>>("res").unwrap();
let m2 = row.to::<Res>().unwrap().res;

assert_eq!(m1, m2);
assert_eq!(m1.len(), 1);

let m = &m1[0];

assert_eq!(m.node.id(), 1);
assert_eq!(m.d_left.as_ref().unwrap().id(), 2);
assert_eq!(m.d_right.as_ref(), None);

knutwalker added a commit to knutwalker/neo4rs that referenced this issue Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants