Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users input pure string literals in filters and updates #83

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions chaindexing-tests/src/tests/contract_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ mod tests {

new_state.create(&event_context).await;

let returned_state = NftState::read_one(
[("token_id".to_owned(), "2".to_owned())].into(),
&event_context,
)
.await
.unwrap();
let returned_state =
NftState::read_one([("token_id", "2")].into(), &event_context).await.unwrap();

assert_eq!(new_state, returned_state);
}
Expand All @@ -46,21 +42,13 @@ mod tests {

let new_state = NftState { token_id: 1 };
new_state.create(&event_context).await;
let updates = [("token_id".to_string(), "4".to_owned())];
let updates = [("token_id", "4")];
new_state.update(updates.into(), &event_context).await;

let initial_state = NftState::read_one(
[("token_id".to_owned(), "1".to_owned())].into(),
&event_context,
)
.await;
let initial_state = NftState::read_one([("token_id", "1")].into(), &event_context).await;
assert_eq!(initial_state, None);

let updated_state = NftState::read_one(
[("token_id".to_owned(), "4".to_owned())].into(),
&event_context,
)
.await;
let updated_state = NftState::read_one([("token_id", "4")].into(), &event_context).await;
assert!(updated_state.is_some());
}

Expand All @@ -80,11 +68,7 @@ mod tests {
new_state.create(&event_context).await;
new_state.delete(&event_context).await;

let state = NftState::read_one(
[("token_id".to_owned(), "9".to_owned())].into(),
&event_context,
)
.await;
let state = NftState::read_one([("token_id", "9")].into(), &event_context).await;
assert_eq!(state, None);
}
}
Expand Down
22 changes: 14 additions & 8 deletions chaindexing/src/contract_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub trait ContractState:

let map: HashMap<String, serde_json::Value> = serde_json::from_value(state).unwrap();

serde_map_to_string_map(map)
serde_map_to_string_map(&map)
}

async fn to_complete_view<'a>(
Expand Down Expand Up @@ -115,14 +115,18 @@ pub trait ContractState:

async fn update<'a, S: Send + Sync + Clone>(
&self,
updates: HashMap<String, String>,
updates: HashMap<&str, &str>,
context: &EventHandlerContext<S>,
) {
let event = &context.event;
let client = context.raw_query_client;

let table_name = Self::table_name();
let state_view = self.to_complete_view(table_name, client, event).await;
let updates = updates
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect::<HashMap<_, _>>();

let latest_state_version =
StateVersion::update(&state_view, &updates, table_name, event, client).await;
Expand All @@ -142,7 +146,7 @@ pub trait ContractState:
}

async fn read_one<'a, S: Send + Sync + Clone>(
filters: HashMap<String, String>,
filters: HashMap<&str, &str>,
context: &EventHandlerContext<S>,
) -> Option<Self> {
let states = Self::read_many(filters, context).await;
Expand All @@ -151,7 +155,7 @@ pub trait ContractState:
}

async fn read_many<'a, S: Send + Sync + Clone>(
filters: HashMap<String, String>,
filters: HashMap<&str, &str>,
context: &EventHandlerContext<S>,
) -> Vec<Self> {
let client = context.raw_query_client;
Expand Down Expand Up @@ -183,8 +187,10 @@ pub fn to_columns_and_values(state: &HashMap<String, String>) -> (Vec<String>, V
)
}

pub fn to_and_filters(state: &HashMap<String, String>) -> String {
pub fn to_and_filters(state: &HashMap<impl AsRef<str>, impl AsRef<str>>) -> String {
let filters = state.iter().fold(vec![], |mut filters, (column, value)| {
let column = column.as_ref();
let value = value.as_ref();
filters.push(format!("{column} = '{value}'"));

filters
Expand All @@ -194,14 +200,14 @@ pub fn to_and_filters(state: &HashMap<String, String>) -> String {
}

pub fn serde_map_to_string_map(
serde_map: HashMap<String, serde_json::Value>,
serde_map: &HashMap<impl AsRef<str>, serde_json::Value>,
) -> HashMap<String, String> {
serde_map.iter().fold(HashMap::new(), |mut map, (key, value)| {
if !value.is_null() {
if value.is_object() {
map.insert(key.to_owned(), value.to_string());
map.insert(key.as_ref().to_owned(), value.to_string());
} else {
map.insert(key.to_owned(), value.to_string().replace('\"', ""));
map.insert(key.as_ref().to_owned(), value.to_string().replace('\"', ""));
}
}

Expand Down
6 changes: 3 additions & 3 deletions chaindexing/src/contract_states/state_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl StateVersions {
HashMap<String, serde_json::Value>,
>(client, &query)
.await
.into_iter()
.iter()
.map(serde_map_to_string_map)
.collect()
}
Expand Down Expand Up @@ -84,7 +84,7 @@ impl StateVersions {
HashMap<String, serde_json::Value>,
>(client, &query)
.await
.into_iter()
.iter()
.map(serde_map_to_string_map)
.collect()
}
Expand Down Expand Up @@ -162,7 +162,7 @@ impl StateVersion {
);

serde_map_to_string_map(
ChaindexingRepo::load_data_from_raw_query_with_txn_client::<
&ChaindexingRepo::load_data_from_raw_query_with_txn_client::<
HashMap<String, serde_json::Value>,
>(client, &query)
.await
Expand Down
4 changes: 2 additions & 2 deletions chaindexing/src/contract_states/state_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ impl StateView {
let query = format!(
"SELECT * FROM {table_name} WHERE {filters}
AND chain_id={context_chain_id} AND contract_address='{context_contract_address}'",
filters = to_and_filters(state_view),
filters = to_and_filters(state_view.into()),
);

serde_map_to_string_map(
ChaindexingRepo::load_data_from_raw_query_with_txn_client::<
&ChaindexingRepo::load_data_from_raw_query_with_txn_client::<
HashMap<String, serde_json::Value>,
>(client, &query)
.await
Expand Down
Loading