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

Implement apply attribute #85 #524

Merged
merged 3 commits into from
Nov 13, 2022
Merged

Implement apply attribute #85 #524

merged 3 commits into from
Nov 13, 2022

Conversation

jonasbb
Copy link
Owner

@jonasbb jonasbb commented Nov 13, 2022

This apply proc-macro takes a list of type patterns and a list of attributes for each. On each field where the type matches, the list of attributes will be added to the existing field attributes.

#[serde_with::apply(
    Option => #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")],
    Option<bool> => #[serde(rename = "bool")],
)]
#[derive(serde::Serialize)]
struct Data {
    a: Option<String>,
    b: Option<u64>,
    c: Option<String>,
    d: Option<bool>,
}

The apply attribute will expand into this, applying the attributs to the matching fields:

#[derive(serde::Serialize)]
struct Data {
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    a: Option<String>,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    b: Option<u64>,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    c: Option<String>,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "bool")]
    d: Option<bool>,
}

bors r+

bors bot added a commit that referenced this pull request Nov 13, 2022
524: Start work on an `apply` attribute #85 r=jonasbb a=jonasbb

This `apply` proc-macro takes a list of type patterns and a list of attributes for each. On each field where the type matches, the list of attributes will be added to the existing field attributes.

```rust
#[serde_with::apply(
    Option => #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")],
    Option<bool> => #[serde(rename = "bool")],
)]
#[derive(serde::Serialize)]
struct Data {
    a: Option<String>,
    b: Option<u64>,
    c: Option<String>,
    d: Option<bool>,
}
```

The `apply` attribute will expand into this, applying the attributs to the matching fields:

```rust
#[derive(serde::Serialize)]
struct Data {
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    a: Option<String>,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    b: Option<u64>,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    c: Option<String>,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "bool")]
    d: Option<bool>,
}
```

bors r+

Co-authored-by: Jonas Bushart <jonas@bushart.org>
@jonasbb jonasbb changed the title Start work on an apply attribute #85 Implement apply attribute #85 Nov 13, 2022
@bors
Copy link
Contributor

bors bot commented Nov 13, 2022

Build failed:

Switch from from_timestamp to from_timestamp_opt.
This `apply` proc-macro takes a list of type patterns and a list of
attributes for each. On each field where the type matches, the list of
attributes will be added to the existing field attributes.
@jonasbb
Copy link
Owner Author

jonasbb commented Nov 13, 2022

bors r+

@bors
Copy link
Contributor

bors bot commented Nov 13, 2022

Build succeeded:

@bors bors bot merged commit fa1f1fe into master Nov 13, 2022
@bors bors bot deleted the apply branch November 13, 2022 15:37
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.

None yet

1 participant