neo4j-labs / neo4rs

Rust driver for Neo4j
https://docs.rs/neo4rs
205 stars 59 forks source link

How to cast the `row.get()` method result to a custom Struct? #113

Open tausifcreates opened 1 year ago

tausifcreates commented 1 year ago

The docs only shows ways to cast to Node, String etc. What if I return a collect from the query which holds a list of, say a custom struct type. How do we do that?

knutwalker commented 1 year ago

I'm gonna refer to my comment from the other issue https://github.com/neo4j-labs/neo4rs/issues/108#issuecomment-1741012358

This will be possible with the serde integration, here is a unit test that deserializes a row into custom structs: https://github.com/knutwalker/neo4rs/blob/9681029210e47f55e2114f1572bfc875749cdfd7/lib/src/row.rs#L299 I would also like to add something like your example on the other issue as an example/integration test.

tausifcreates commented 1 year ago

@knutwalker Thanks for your response! It surely will solve my pain points.

knutwalker commented 1 year ago

@tausifcreates we've just released 0.7.0-rc.1 with serde support. It's missing proper documentation, but I can see that maybe something like this works for you


#[derive(Debug, serde::Deserialize)]
struct Thing {
  some_prop: String,
  another_prop: u32,
}

// ...

let things = row.to::<Vec<Thing>>()?;
// or maybe
let things = row.get::<Vec<Things>>("column")?;

It would be great if you could give it a try and see if that works for you.

tausifcreates commented 1 year ago

Hey thanks @knutwalker! Actually I shared the release announcement on reddit