Skip to content

Commit

Permalink
Added support for Vec<Vec<u8>> as Array<Bytes>
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Dec 13, 2023
1 parent c8975a0 commit 5113ec2
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions substreams-entity-change/src/tables.rs
Original file line number Diff line number Diff line change
@@ -323,6 +323,26 @@ impl ToValue for &Vec<u8> {
}
}

impl ToValue for &Vec<Vec<u8>> {
fn to_value(self) -> Value {
Value {
typed: Some(Typed::Array(Array {
value: self.iter().map(ToValue::to_value).collect(),
})),
}
}
}

impl ToValue for Vec<Vec<u8>> {
fn to_value(self) -> Value {
Value {
typed: Some(Typed::Array(Array {
value: self.into_iter().map(ToValue::to_value).collect(),
})),
}
}
}

impl ToValue for Vec<String> {
fn to_value(self) -> Value {
Value {
@@ -442,3 +462,40 @@ macro_rules! impl_to_int32_value {
impl_to_int32_value!(i8);
impl_to_int32_value!(i16);
impl_to_int32_value!(i32);

#[cfg(test)]
mod test {
use crate::pb::entity::{EntityChange, EntityChanges, entity_change::Operation, Field, Value, value::Typed, Array};

use super::Tables;

#[test]
fn test_vec_vec_u8() {
let mut tables = Tables::new();
tables.create_row("table", "1").set("field", &vec![vec![1, 2, 3]]);

let changes = tables.to_entity_changes();
assert_eq!(changes.entity_changes.len(), 1);
assert_eq!(changes.entity_changes[0], EntityChange {
entity: "table".to_string(),
id: "1".to_string(),
ordinal: 0,
operation: Operation::Create as i32,
fields: vec![
Field {
name: "field".to_string(),
new_value: Some(Value {
typed: Some(Typed::Array(Array {
value: vec![
Value {
typed: Some(Typed::Bytes("AQID".to_string())),
}
],
})),
}),
old_value: None,
}
],
});
}
}

0 comments on commit 5113ec2

Please sign in to comment.