Skip to content

Commit

Permalink
Added api get_or_null for missing field
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Jun 22, 2020
1 parent 7078b92 commit 1e2127c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions orientdb-client/src/common/types/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ impl OResult {
}
}

pub fn get_or_null<T>(&self, name: &str) -> OrientResult<T>
where
T: FromOValue,
{
let value = self.get_raw(name).unwrap_or_else(|| &OValue::Null);
T::from_value(value)
}

pub fn get<T>(&self, name: &str) -> T
where
T: FromOValue,
Expand Down
3 changes: 3 additions & 0 deletions orientdb-client/tests/orient-session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fn session_query_one() {
#[derive(orientdb_client::derive::FromResult, Debug, PartialEq)]
struct Person {
name: String,
age: Option<i32>,
}

let result: Option<Person> = session
Expand All @@ -142,6 +143,7 @@ fn session_query_one() {
assert_eq!(
Some(Person {
name: String::from("admin"),
age: None
}),
result,
)
Expand Down Expand Up @@ -709,6 +711,7 @@ mod asynchronous {
#[derive(orientdb_client::derive::FromResult, Debug, PartialEq)]
struct Person {
name: String,
age: Option<i32>,
}
let session = session("async_session_query_all").await;
let result: Vec<Person> = session.query("select from OUser").fetch().await.unwrap();
Expand Down

0 comments on commit 1e2127c

Please sign in to comment.