From 7fe3c0628065c6782701985dfc1cc41d6aa479f4 Mon Sep 17 00:00:00 2001 From: lostman Date: Wed, 29 Nov 2023 06:41:26 -0800 Subject: [PATCH] bugfix: fix bytes encoding in entity::find (#1482) * fix bytes encoding in entity::find * fix blockhegiht to sqlvalue conversion; add tests * add more test cases * fmt --- packages/fuel-indexer-plugin/src/find.rs | 53 ++++++++++++++++++------ 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/packages/fuel-indexer-plugin/src/find.rs b/packages/fuel-indexer-plugin/src/find.rs index 0944b2d2c..bc41a0d3e 100644 --- a/packages/fuel-indexer-plugin/src/find.rs +++ b/packages/fuel-indexer-plugin/src/find.rs @@ -1,4 +1,4 @@ -use fuel_indexer_types::scalar::{Boolean, UID}; +use fuel_indexer_types::scalar::{BlockHeight, Boolean, UID}; use sqlparser::ast as sql; /// Represents a filter that returns a single results. @@ -220,15 +220,17 @@ impl ToSQLValue for UID { } } +impl ToSQLValue for BlockHeight { + fn to_sql_value(self) -> sql::Value { + sqlparser::test_utils::number(&self.as_usize().to_string()) + } +} + macro_rules! impl_bytes_to_sql_value { ($T:ident) => { impl ToSQLValue for fuel_indexer_types::scalar::$T { fn to_sql_value(self) -> sql::Value { - unsafe { - sql::Value::SingleQuotedByteStringLiteral( - std::str::from_utf8_unchecked(self.as_ref()).to_string(), - ) - } + sql::Value::SingleQuotedString(hex::encode(self)) } } }; @@ -268,8 +270,6 @@ impl_number_to_sql_value!(U32); impl_number_to_sql_value!(I8); impl_number_to_sql_value!(U8); -impl_number_to_sql_value!(BlockHeight); - /// Captures the information necessary to represent `struct T { field: F }`. /// It is then used to build a type-safe `Filter`, e.g., `Filter`. pub struct Field { @@ -392,13 +392,24 @@ mod tests { #[test] fn test_find_query_generation() { + use fuel_indexer_types::scalar::{Address, BlockHeight, Bytes8, I32}; + struct MyStruct {} - fn my_field() -> Field { - Field { - field: "my_field".to_string(), - phantom: std::marker::PhantomData, - } + fn my_field() -> Field { + Field::new("my_field".to_string()) + } + + fn my_address_field() -> Field { + Field::new("my_address_field".to_string()) + } + + fn my_bytes8_field() -> Field { + Field::new("my_bytes8_field".to_string()) + } + + fn my_blockheight_field() -> Field { + Field::new("my_blockheight_field".to_string()) } let f: Filter = my_field().gt(7); @@ -426,5 +437,21 @@ mod tests { let mf: ManyFilter = my_field().gt(7).order_by(my_field()).desc().into(); assert_eq!(&mf.to_string(), "my_field > 7 ORDER BY my_field DESC"); + + let addr: Filter = my_address_field().eq(Address::zeroed()); + assert_eq!(&addr.to_string(), "my_address_field = '0000000000000000000000000000000000000000000000000000000000000000'"); + + let addr: Filter = my_address_field().eq(Address::from([238; 32])); + assert_eq!(&addr.to_string(), "my_address_field = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'"); + + let bytes: Filter = my_bytes8_field().eq(Bytes8::zeroed()); + assert_eq!(&bytes.to_string(), "my_bytes8_field = '0000000000000000'"); + + let bytes: Filter = + my_bytes8_field().eq(Bytes8::from([1, 2, 3, 4, 5, 6, 7, 15])); + assert_eq!(&bytes.to_string(), "my_bytes8_field = '010203040506070f'"); + + let word: Filter = my_blockheight_field().eq(BlockHeight::new(123)); + assert_eq!(&word.to_string(), "my_blockheight_field = 123"); } }