Skip to content

Refactor macro attribute parsing: migrate from bae to darling and remove string/Ident round-trips - #3169

Open
TheShiveshNetwork wants to merge 7 commits into
SeaQL:masterfrom
TheShiveshNetwork:refactor/3119-Refactor-macro-implementations
Open

Refactor macro attribute parsing: migrate from bae to darling and remove string/Ident round-trips#3169
TheShiveshNetwork wants to merge 7 commits into
SeaQL:masterfrom
TheShiveshNetwork:refactor/3119-Refactor-macro-implementations

Conversation

@TheShiveshNetwork

@TheShiveshNetwork TheShiveshNetwork commented Aug 12, 2026

Copy link
Copy Markdown
  • Dependencies:
    • The bae -> darling migration is part of this branch: bae (sea-bae) is fully removed and all five attribute structs (derive_attr, relation_attr, compound_attr, value_type_attr, related_attr) now use darling::FromMeta. darling is pinned to 0.23 (latest release still on syn 2.0; 0.24 would require a separate syn 2->3 migration). The original try_from_attributes / from_attributes signatures are preserved so call sites are unchanged.
    • Went back and forth on darling vs just hand-parsing with parse_nested_meta (a few derives here already do that, e.g. column.rs/derive_iden.rs, so it's not like it'd be a foreign pattern). Ended up going with darling because compound_attr has 14 fields and actual co-occurrence rules between them (from+to need each other, has_one/belongs_to are mutually exclusive, etc.), and writing that validation by hand for all 5 structs felt like we'd just be badly reinventing what FromMeta already does. Also figured darling being a known quantity in the ecosystem is nicer for anyone picking this crate up later than a parser only sea-orm uses.
  • Dependents:
    • Working on syn migration to 3.0.3, yet to create the pr

New Features

  • None

Bug Fixes

  • None (refactor only)

Breaking Changes

  • None. Attribute syntax (#[sea_orm(...)]) is untouched, this is purely how the macros parse it internally.

Changes

  • Migrate macro attribute parsing from bae to darling. Removed the bae/sea-bae dependency; the five attribute structs in sea-orm-macros/src/derives/attributes.rs now derive darling::FromMeta, with validation deferred so unknown attributes still warn rather than error.
  • Replace manual Ident::new(&format!(..)) with format_ident! for literal and transformed idents across the derive macros (model_ex, active_model_ex, entity_model, typed_column, value_type_match, related_entity, entity_loader, partial_model, into_active_model, raw_sql).
  • Build derived types with syn::parse_quote! instead of syn::parse_str on a formatted string (e.g. the #[sea_orm(compact)] related ActiveModel path in partial_model).
  • Compare Idents directly via *id == "some_str" instead of stringifying with .to_string() first, mainly in util.rs's CompoundType::matches_type / CompoundType::from_type (worst offender, 5 stringify-then-match sites in one function), plus a few smaller ones in active_enum.rs, column.rs, and derive_iden.rs.

Comment thread sea-orm-macros/src/derives/active_model_ex.rs Outdated
Comment thread sea-orm-macros/src/derives/attributes.rs Outdated
Comment thread sea-orm-macros/src/derives/attributes.rs Outdated

/// Attributes for compound model fields
#[derive(Default, FromAttributes)]
#[derive(Default, FromMeta)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't solve the issue. You should replace the current manually parsed compatibility layer with darling.

Comment thread sea-orm-macros/src/raw_sql.rs
@TheShiveshNetwork

Copy link
Copy Markdown
Author

@Huliiiiii this pr should be ready for review. moreover, i've also opened the pr for migration of syn 2 to syn 3 #3171 we can complete that first and then, i'll migrate darling to latest once that is merged.

@Huliiiiii

Copy link
Copy Markdown
Member
pub fn try_from_attributes(attrs: &[Attribute]) -> syn::Result<Option<Self>> {
    super::try_from_attributes(attrs)
}

Please, I don't want to see this kind of LLM slop. You also haven't understood what we're trying to fix. Please put more thought into the code. If you're going to rely on LLM, it'd be much easier for me to just do it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor macro implmentions

2 participants