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

Derive derive attributes #243

Closed
clux opened this issue May 22, 2020 · 1 comment · Fixed by #248
Closed

Derive derive attributes #243

clux opened this issue May 22, 2020 · 1 comment · Fixed by #248
Labels
derive kube-derive proc_macro related help wanted Not immediately prioritised, please help!

Comments

@clux
Copy link
Member

clux commented May 22, 2020

Currently we have hardcode the derives to the root struct (["Serialize", "Deserialize", "Clone", "Debug"], and allow a few extra ones using proc macro arguments:

#[derive(CustomResource, Serialize, Deserialize, Debug, PartialEq, Clone)]
#[kube(
    group = "clux.dev",
    version = "v1",
    kind = "Foo",
    status = "FooStatus",
    PartialEq
)]
pub struct MyFoo { ... }

This PartialEq argument (from #242) allows mutating this derive list slightly, but not more than appending, and not more than that particular trait. Not an ideal solution.

Ideally, the proc macro should be able to identify certain traits derived for this sub struct (the spec) and apply them to the created object. The proc macro crate SHOULD give access to this information at the very least through the token tree, but it's a pain to figure it all out.

Logic could be a little something like:

if derive_attrs.contains("PartialEq") {
  derives.push("PartialEq")
}

(Hopefully, it's easy to access the particular #[derive] attrs - because if it is, we could just parse them like we parse the #[kube] metas.)

But there are big complications w.r.t. error handling; in particular to relations of FooStatus - a struct which we do not know if they implement these traits. It might not be possible to provide good errors for this if we ask to auto-derive, and FooSpec has PartialEq, but FooStatus does not. That will fail with the usual compile error (or worse). It is possible that this isn't something we should auto-derive but instead have a named vector argument for instead:

#[kube(derive = PartialEq, derive = ExtraTrait)]

Not sure what solution is best atm, but I would like an easy to use one with good defaults. I had to propagate Debug everywhere in my code earlier because deriving CustomResource depended on it, and it's not a nice first step. If we auto-derived, we could push Debug into one of those traits that we'll add if the spec struct wants it.

@clux clux added help wanted Not immediately prioritised, please help! derive kube-derive proc_macro related labels May 22, 2020
clux added a commit that referenced this issue Jun 4, 2020
@clux clux closed this as completed in #248 Jun 8, 2020
@clux
Copy link
Member Author

clux commented Jun 15, 2020

New attr released in kube-derive 0.35.0

ctron pushed a commit to ctron/kube-rs that referenced this issue Jun 18, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
derive kube-derive proc_macro related help wanted Not immediately prioritised, please help!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant