Skip to content

Commit

Permalink
Made test_unwrap_map pass
Browse files Browse the repository at this point in the history
  • Loading branch information
criminosis committed Oct 31, 2024
1 parent 815767a commit 753dbf7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
1 change: 1 addition & 0 deletions gremlin-client/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl FromGValue for GKey {
GValue::Edge(s) => Ok(GKey::Edge(s)),
GValue::Int64(v) => Ok(GKey::Int64(v)),
GValue::Int32(v) => Ok(GKey::Int32(v)),
GValue::T(t) => Ok(GKey::T(t)),
_ => Err(GremlinError::Cast(format!(
"Cannot convert {:?} to {}",
v, "GKey"
Expand Down
10 changes: 5 additions & 5 deletions gremlin-client/src/io/graph_binary_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,12 @@ impl GraphBinaryV1Deser for GValue {

impl GraphBinaryV1Deser for T {
fn from_be_bytes<'a, S: Iterator<Item = &'a u8>>(bytes: &mut S) -> GremlinResult<Self> {
let literal = GValue::from_be_bytes_nullable(bytes)?;
let literal = GValue::from_be_bytes(bytes)?;
match literal {
Some(GValue::String(literal)) if literal.eq_ignore_ascii_case("id") => Ok(T::Id),
Some(GValue::String(literal)) if literal.eq_ignore_ascii_case("key") => Ok(T::Id),
Some(GValue::String(literal)) if literal.eq_ignore_ascii_case("label") => Ok(T::Id),
Some(GValue::String(literal)) if literal.eq_ignore_ascii_case("value") => Ok(T::Id),
GValue::String(literal) if literal.eq_ignore_ascii_case("id") => Ok(T::Id),
GValue::String(literal) if literal.eq_ignore_ascii_case("key") => Ok(T::Key),
GValue::String(literal) if literal.eq_ignore_ascii_case("label") => Ok(T::Label),
GValue::String(literal) if literal.eq_ignore_ascii_case("value") => Ok(T::Value),
other => Err(GremlinError::Cast(format!(
"Unexpected T literal {other:?}"
))),
Expand Down
36 changes: 25 additions & 11 deletions gremlin-client/tests/integration_traversal_omni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use gremlin_client::structure::{
Cardinality, Column, List, Map, Pop, TextP, Vertex, VertexProperty, P, T,
};

use gremlin_client::{utils, GKey, GValue, GremlinClient, IoProtocol};
use gremlin_client::{
utils, BorrowFromGValue, GKey, GValue, GremlinClient, GremlinError, IoProtocol,
};

mod common;

Expand Down Expand Up @@ -1213,17 +1215,16 @@ fn test_unwrap_map(client: GremlinClient) {
let results = g.v(vertex.id()).value_map(true).next().unwrap().unwrap();
let v_id = vertex.id().get::<i64>().unwrap();

let id = utils::unwrap_map::<i64>(&results, "id", 0);
let property = utils::unwrap_map::<String>(&results, "name", 0);
let label = utils::unwrap_map::<String>(&results, "label", 0);

assert_eq!(id.is_ok(), true);
assert_eq!(property.is_ok(), true);
assert_eq!(label.is_ok(), true);

assert_eq!(id.unwrap(), v_id);
let id = get_map::<i64, _>(&results, "id")
.unwrap()
.or_else(|| get_map::<i64, _>(&results, T::Id).unwrap());
let property = get_map::<String, _>(&results, "name").unwrap();
let label = get_map::<String, _>(&results, "label")
.unwrap()
.or_else(|| get_map::<String, _>(&results, T::Label).unwrap());
assert_eq!(id, Some(v_id));
assert_eq!(property.unwrap(), "test");
assert_eq!(label.unwrap(), "test_value_map");
assert_eq!(label, Some(vertex.label()));
}

#[apply(serializers)]
Expand Down Expand Up @@ -2628,6 +2629,19 @@ fn test_none_step(client: GremlinClient) {
assert_eq!(1, vertex_count);
}

fn get_map<'a, T, K>(map: &'a Map, key: K) -> Result<Option<&'a T>, GremlinError>
where
T: BorrowFromGValue,
K: Into<GKey>,
{
map.get(key)
.map(|val| match val {
GValue::List(list) => list[0].get::<T>(),
other => other.get::<T>(),
})
.transpose()
}

#[apply(serializers)]
#[serial(test_traversal_vertex_mapping)]
#[cfg(feature = "derive")]
Expand Down

0 comments on commit 753dbf7

Please sign in to comment.