From 1e2127cdc7a39d80c42f83b9977eac926e7c2249 Mon Sep 17 00:00:00 2001 From: wolf4ood Date: Mon, 22 Jun 2020 23:17:37 +0200 Subject: [PATCH] Added api get_or_null for missing field --- orientdb-client/src/common/types/result.rs | 8 ++++++++ orientdb-client/tests/orient-session.rs | 3 +++ 2 files changed, 11 insertions(+) diff --git a/orientdb-client/src/common/types/result.rs b/orientdb-client/src/common/types/result.rs index 3736a29..e579f6b 100644 --- a/orientdb-client/src/common/types/result.rs +++ b/orientdb-client/src/common/types/result.rs @@ -47,6 +47,14 @@ impl OResult { } } + pub fn get_or_null(&self, name: &str) -> OrientResult + where + T: FromOValue, + { + let value = self.get_raw(name).unwrap_or_else(|| &OValue::Null); + T::from_value(value) + } + pub fn get(&self, name: &str) -> T where T: FromOValue, diff --git a/orientdb-client/tests/orient-session.rs b/orientdb-client/tests/orient-session.rs index 0d4bae9..31aada7 100644 --- a/orientdb-client/tests/orient-session.rs +++ b/orientdb-client/tests/orient-session.rs @@ -131,6 +131,7 @@ fn session_query_one() { #[derive(orientdb_client::derive::FromResult, Debug, PartialEq)] struct Person { name: String, + age: Option, } let result: Option = session @@ -142,6 +143,7 @@ fn session_query_one() { assert_eq!( Some(Person { name: String::from("admin"), + age: None }), result, ) @@ -709,6 +711,7 @@ mod asynchronous { #[derive(orientdb_client::derive::FromResult, Debug, PartialEq)] struct Person { name: String, + age: Option, } let session = session("async_session_query_all").await; let result: Vec = session.query("select from OUser").fetch().await.unwrap();