Skip to content

No exposed ability to grab custom maps from output #108

@caamartin35

Description

@caamartin35

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions