Skip to content

Commit

Permalink
feat: Include struct names in ABIs (#2266)
Browse files Browse the repository at this point in the history
* feat: Include struct names in ABIs

* Update crates/nargo_cli/src/cli/check_cmd.rs

Co-authored-by: Tom French <[email protected]>

* Fix spelling

---------

Co-authored-by: Tom French <[email protected]>
  • Loading branch information
spalladino and TomAFrench authored Aug 11, 2023
1 parent 1ad9199 commit 9824ca5
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion crates/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn create_input_toml_template(
.collect();
toml::Value::Array(default_value_vec)
}
AbiType::Struct { fields } => {
AbiType::Struct { fields, .. } => {
let default_value_map = toml::map::Map::from_iter(
fields.into_iter().map(|(name, typ)| (name, default_value(typ))),
);
Expand Down Expand Up @@ -128,6 +128,7 @@ mod tests {
typed_param(
"d",
AbiType::Struct {
name: String::from("MyStruct"),
fields: vec![
(String::from("d1"), AbiType::Field),
(
Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_abi/src/input_parser/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl JsonTypes {

(InputValue::String(s), AbiType::String { .. }) => JsonTypes::String(s.to_string()),

(InputValue::Struct(map), AbiType::Struct { fields }) => {
(InputValue::Struct(map), AbiType::Struct { fields, .. }) => {
let map_with_json_types = try_btree_map(fields, |(key, field_type)| {
JsonTypes::try_from_input_value(&map[key], field_type)
.map(|json_value| (key.to_owned(), json_value))
Expand Down Expand Up @@ -155,7 +155,7 @@ impl InputValue {
InputValue::Vec(array_elements)
}

(JsonTypes::Table(table), AbiType::Struct { fields }) => {
(JsonTypes::Table(table), AbiType::Struct { fields, .. }) => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
// Check that json contains a value for each field of the struct.
let field_id = format!("{arg_name}.{field_name}");
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_abi/src/input_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ mod serialization_tests {
AbiParameter {
name: "bar".into(),
typ: AbiType::Struct {
name: "MyStruct".into(),
fields: vec![
("field1".into(), AbiType::Integer { sign: Sign::Unsigned, width: 8 }),
(
Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_abi/src/input_parser/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl TomlTypes {

(InputValue::String(s), AbiType::String { .. }) => TomlTypes::String(s.to_string()),

(InputValue::Struct(map), AbiType::Struct { fields }) => {
(InputValue::Struct(map), AbiType::Struct { fields, .. }) => {
let map_with_toml_types = try_btree_map(fields, |(key, field_type)| {
TomlTypes::try_from_input_value(&map[key], field_type)
.map(|toml_value| (key.to_owned(), toml_value))
Expand Down Expand Up @@ -138,7 +138,7 @@ impl InputValue {
InputValue::Vec(array_elements)
}

(TomlTypes::Table(table), AbiType::Struct { fields }) => {
(TomlTypes::Table(table), AbiType::Struct { fields, .. }) => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
// Check that json contains a value for each field of the struct.
let field_id = format!("{arg_name}.{field_name}");
Expand Down
3 changes: 2 additions & 1 deletion crates/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub enum AbiType {
},
Boolean,
Struct {
name: String,
#[serde(
serialize_with = "serialization::serialize_struct_fields",
deserialize_with = "serialization::deserialize_struct_fields"
Expand Down Expand Up @@ -308,7 +309,7 @@ impl Abi {
encoded_value.extend(str_as_fields);
}

(InputValue::Struct(object), AbiType::Struct { fields }) => {
(InputValue::Struct(object), AbiType::Struct { fields, .. }) => {
for (field, typ) in fields {
encoded_value.extend(Self::encode_value(object[field].clone(), typ)?);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/noirc_abi/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ mod tests {
\"name\":\"thing3\",
\"type\": {
\"kind\":\"struct\",
\"name\": \"MyStruct\",
\"fields\": [
{
\"name\": \"field1\",
Expand Down Expand Up @@ -119,6 +120,7 @@ mod tests {
let expected_struct = AbiParameter {
name: "thing3".to_string(),
typ: AbiType::Struct {
name: "MyStruct".to_string(),
fields: vec![
("field1".to_string(), AbiType::Integer { sign: Sign::Unsigned, width: 3 }),
(
Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ impl Type {
let struct_type = def.borrow();
let fields = struct_type.get_fields(args);
let fields = vecmap(fields, |(name, typ)| (name, typ.as_abi_type()));
AbiType::Struct { fields }
AbiType::Struct { fields, name: struct_type.name.to_string() }
}
Type::Tuple(_) => todo!("as_abi_type not yet implemented for tuple types"),
Type::TypeVariable(_, _) => unreachable!(),
Expand Down

0 comments on commit 9824ca5

Please sign in to comment.