-
Notifications
You must be signed in to change notification settings - Fork 17
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
Feature request: Add support for conditional cfg_attr for primary key #215
Comments
For the error, this isn't related to By reversing (as shown below), the commented code and the #[cfg_attr(
feature = "ssr",
native_db,
native_model(id = 1, version = 1)
)]
#[derive(Clone, Debug, Deserialize, Serialize, Getters, Dissolve, new)]
pub struct User {]
// #[cfg_attr(feature = "ssr", primary_key)]
#[primary_key]
pub id: IdType,
} |
Thank you for getting back to me so quickly, I really appreciate it. I'm aware that commenting out the primary key check is a very bad idea, but as long as I remember to add the primary key and add tests, then it should be fine for development. I tried removing the commented line for both cases, that did not change the outcome. I've set up a minimal project to illustrate the issue: Cargo.toml[package]
name = "t"
version = "0.1.0"
edition = "2021"
[dependencies]
native_db = { version = "0.7.1", optional = true }
native_model = {version = "0.4.19", optional = true }
serde = { version = "1.0.208", features = ["derive"] }
[features]
default = []
enable_native_db = ["native_db", "native_model"] main.rs#[cfg(feature = "enable_native_db")]
mod enable_native_db {
pub use native_db::{native_db, ToKey};
pub use native_model::{native_model, Model};
}
use serde::{Deserialize, Serialize};
#[cfg(feature = "enable_native_db")]
use enable_native_db::*;
#[cfg_attr(feature = "enable_native_db", native_db, native_model(id = 1, version = 1))]
#[derive(Serialize, Deserialize)]
struct Foo {
#[cfg_attr(feature = "enable_native_db", primary_key)]
id: i32,
}
fn main() {
println!("Hello, world!");
} This compiles just fine if I don't enable the feature. But if I compile it with
Then the other option is to change the annotation to this: #[primary_key] so the code is this: #[cfg(feature = "enable_native_db")]
mod enable_native_db {
pub use native_db::{native_db, ToKey};
pub use native_model::{native_model, Model};
}
use serde::{Deserialize, Serialize};
#[cfg(feature = "enable_native_db")]
use enable_native_db::*;
#[cfg_attr(feature = "enable_native_db", native_db, native_model(id = 1, version = 1))]
#[derive(Serialize, Deserialize)]
struct Foo {
#[primary_key]
id: i32,
}
fn main() {
println!("Hello, world!");
} But now we have a dependency on native_db even if enable_native_db is not enabled:
So I'm unable to define the model in a way such that native_db is only added to the model if the enable_native_db feature is enabled If I use my fork instead, then this compiles: Cargo.toml[package]
name = "t"
version = "0.1.0"
edition = "2021"
[dependencies]
native_db = { git = "https://github.com/niklass-l/native_db", optional = true }
native_model = {version = "0.4.19", optional = true }
serde = { version = "1.0.208", features = ["derive"] }
[features]
default = []
enable_native_db = ["native_db", "native_model"] main.rs#[cfg(feature = "enable_native_db")]
mod ssr {
pub use native_db::{native_db, ToKey};
pub use native_model::{native_model, Model};
}
use serde::{Deserialize, Serialize};
#[cfg(feature = "enable_native_db")]
use ssr::*;
#[cfg_attr(
feature = "enable_native_db",
native_db,
native_model(id = 1, version = 1)
)]
#[derive(Serialize, Deserialize)]
struct Foo {
#[cfg_attr(feature = "enable_native_db", primary_key)]
id: i32,
}
fn main() {
println!("Hello, world!");
}
I would also note that it does not seem to be an issue with proc macros + conditional attributes in general, since I've been using it with the |
Thank you for your explanation, I understand better now. I had never tested For now, I can suggest this workaround—it’s not very elegant, but it gets the job done: #[cfg_attr(feature = "enable_native_db", native_db, native_model(id = 1, version = 1))]
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Foo {
#[cfg(not(feature = "enable_native_db"))]
id: i32,
#[cfg(feature = "enable_native_db")]
#[primary_key]
id: i32,
} |
Thank you! That's much safer than my solution at least, I'll go with that. |
I have a use case where I need to conditionally add the proc macro attributes to a model based on a feature. This compiles:
#[cfg_attr(
feature = "ssr",
native_db,
native_model(id = 1, version = 1)
)]
#[derive(Clone, Debug, Deserialize, Serialize, Getters, Dissolve, new)]
pub struct User {
#[primary_key]
// #[cfg_attr(feature = "ssr", primary_key)]
pub id: IdType,
}
However, if I comment in the #[cfg_attr(feature = "ssr", primary_key)], the proc macro panics with: "Primary key is not set", even though native_db is only added to the model if "ssr" is enabled.
I would have created a pull request, but proc macros are a bit beyond my ability. For now I've forked the project and added a workaround:
The text was updated successfully, but these errors were encountered: