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

Raise error if struct field name is greater than 30 chars #1239

Merged
merged 3 commits into from
Mar 5, 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
11 changes: 5 additions & 6 deletions soroban-sdk-macros/src/derive_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use syn::{Attribute, DataStruct, Error, Ident, Path, Visibility};
use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::{
ScSpecEntry, ScSpecTypeDef, ScSpecUdtStructFieldV0, ScSpecUdtStructV0, StringM, WriteXdr,
SCSYMBOL_LIMIT,
};

use crate::{doc::docs_from_attrs, map_type::map_type, DEFAULT_XDR_RW_LIMITS};
Expand Down Expand Up @@ -37,13 +36,13 @@ pub fn derive_type_struct(
let field_ident = field.ident.as_ref().unwrap();
let field_name = field_ident.to_string();
let field_idx_lit = Literal::usize_unsuffixed(field_num);

if field_name.len() > SCSYMBOL_LIMIT as usize {
errors.push(Error::new(field_ident.span(), format!("struct field name is too long: {}, max is {}", field_name.len(), SCSYMBOL_LIMIT)));
}
let spec_field = ScSpecUdtStructFieldV0 {
doc: docs_from_attrs(&field.attrs).try_into().unwrap(), // TODO: Truncate docs, or display friendly compile error.
name: field_name.clone().try_into().unwrap_or_else(|_| StringM::default()),
name: field_name.clone().try_into().unwrap_or_else(|_| {
const MAX: u32 = 30;
errors.push(Error::new(field_ident.span(), format!("struct field name is too long: {}, max is {MAX}", field_name.len())));
StringM::<MAX>::default()
}),
type_: match map_type(&field.ty) {
Ok(t) => t,
Err(e) => {
Expand Down
8 changes: 4 additions & 4 deletions soroban-sdk/src/tests/contract_udt_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Udt {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[contracttype]
pub struct UdtWithLongName {
pub this_is_a_very_long_name_1234567: u64,
pub this_is_a_very_long_name_12345: u64,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand All @@ -40,7 +40,7 @@ impl Contract {
}

pub fn add_udt_with_long_name(a: UdtWithLongName, b: UdtWithLongName) -> u64 {
a.this_is_a_very_long_name_1234567 + b.this_is_a_very_long_name_1234567
a.this_is_a_very_long_name_12345 + b.this_is_a_very_long_name_12345
}
}

Expand All @@ -61,10 +61,10 @@ fn test_long_names_functional() {
let contract_id = env.register_contract(None, Contract);

let a = UdtWithLongName {
this_is_a_very_long_name_1234567: 1_000_000_000_000,
this_is_a_very_long_name_12345: 1_000_000_000_000,
};
let b = UdtWithLongName {
this_is_a_very_long_name_1234567: 5_000_000_000_000,
this_is_a_very_long_name_12345: 5_000_000_000_000,
};
assert_eq!(
ContractClient::new(&env, &contract_id).add_udt_with_long_name(&a, &b),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"map": [
{
"key": {
"symbol": "this_is_a_very_long_name_1234567"
"symbol": "this_is_a_very_long_name_12345"
},
"val": {
"u64": 1000000000000
Expand All @@ -108,7 +108,7 @@
"map": [
{
"key": {
"symbol": "this_is_a_very_long_name_1234567"
"symbol": "this_is_a_very_long_name_12345"
},
"val": {
"u64": 5000000000000
Expand Down
Loading