Skip to content

Large structures that are not flattened cause buffer overflow / convencience functions for flattened types. #271

Description

@Exotik850

Currently, large structs that are flattened into a Vec<FieldType> have the issue of needing to iterate the collection of fields to find a specific one, for example with the quickbooks-xsd crate I'm working on:

pub enum InvoiceTypeContent {
    Id(String),
    SyncToken(String),
    MetaData(ModificationMetaDataType),
    CustomField(CustomFieldType),
    AttachableRef(AttachableRefType),
    DocNumber(String),
    TxnDate(String),
    DepartmentRef(ReferenceTypeType),
    CurrencyRef(ReferenceTypeType),
    ExchangeRate(f64),
    PrivateNote(String),
    TxnStatus(String),
    LinkedTxn(LinkedTxnType),
    Line(LineType),
    TxnTaxDetail(TxnTaxDetailType),
    TxnSource(String),
    TaxFormType(String),
    TaxFormNum(String),
... (96 total variants)

is the field type for the InvoiceType struct:

pub struct InvoiceType {
    pub domain: Option<String>,
    pub status: Option<EntityStatusEnumType>,
    pub sparse: Option<bool>,
    pub content: Vec<InvoiceTypeContent>,
}

Which requires the end-user to iterate the items of the content field. This would be mitigated by not flattening the generated structure, resulting in the following code:

pub struct InvoiceType {
    pub id: Option<String>,
    pub custom_field: Option<String>,
    pub private_note: Option<String>,
    ...
}

However, on the basic example of deserializing an example invoice here it causes a stack overflow

I propose adding the logic to allow for functions like fn $field_name(&self) -> Option<T> to flattened structures to allow for more ergonomic use of the generated code.
This would allow us to replace all instances of

let note: Option<String> = invoice.content.find(|f| match f {
  InvoiceFieldType::PrivateNote(note) => Some(note),
  _ => None
});

with

let note: Option<String> = invoice.private_note();

OR to fix the stack overflow issue for larger structs, however this seems like a more in-depth issue that would take a lot more effort

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions