From 0a8c5d1c1f9dc1877212257be24f966172f7c557 Mon Sep 17 00:00:00 2001 From: Lee Smet Date: Thu, 31 Jul 2025 12:08:22 +0200 Subject: [PATCH] Expand Index struct to also get the original field name This is needed in postgres, since we store data as jsonb so struct fields can't get renamed Signed-off-by: Lee Smet --- heromodels-derive/src/lib.rs | 4 ++++ heromodels/src/models/biz/company.rs | 6 ++++++ heromodels_core/src/lib.rs | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/heromodels-derive/src/lib.rs b/heromodels-derive/src/lib.rs index 94c62a5..d2021a2 100644 --- a/heromodels-derive/src/lib.rs +++ b/heromodels-derive/src/lib.rs @@ -169,6 +169,10 @@ pub fn model(_attr: TokenStream, item: TokenStream) -> TokenStream { fn key() -> &'static str { #index_key } + + fn field_name() -> &'static str { + #name_str + } } }; diff --git a/heromodels/src/models/biz/company.rs b/heromodels/src/models/biz/company.rs index ff30261..1c7ffb4 100644 --- a/heromodels/src/models/biz/company.rs +++ b/heromodels/src/models/biz/company.rs @@ -84,6 +84,9 @@ impl Index for CompanyNameIndex { fn key() -> &'static str { "name" } + fn field_name() -> &'static str { + "name" + } } pub struct CompanyRegistrationNumberIndex; @@ -93,6 +96,9 @@ impl Index for CompanyRegistrationNumberIndex { fn key() -> &'static str { "registration_number" } + fn field_name() -> &'static str { + "registration_number" + } } // --- Builder Pattern --- diff --git a/heromodels_core/src/lib.rs b/heromodels_core/src/lib.rs index 00e54b6..ca7cb2a 100644 --- a/heromodels_core/src/lib.rs +++ b/heromodels_core/src/lib.rs @@ -83,6 +83,11 @@ pub trait Index { /// The key of this index fn key() -> &'static str; + + /// The original field name. This is the same as [Index::key] by default, unless the user + /// specified a `(name=...)` value to the index attribute, in which case [Index::key] will be + /// set to the specified key. + fn field_name() -> &'static str; } /// Base struct that all models should include