Skip to content

Add fallback attribute to all strip_options. #708

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

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

ryzhyk
Copy link
Contributor

@ryzhyk ryzhyk commented Nov 21, 2024

Auto-derived TypedBuilder traits in combination with strip_option attributes are very hard to use in scenarios where options passed to the builder depend on user input. The problem is that this pattern doesn't work, since the two match branches return different types:

let builder = match x {
    None => builder,
    Some(x) => builder.x(x),
};

Fortunately typed_builder 0.20 supports an additional attribute (fallback) that generates a builder method that takes Option<T> in addition to the method that takes naked T.

With this attribute, we can write let builder = builder.x_opt(x).

This commit adds the fallback attribute to all fields that have the strip_option annotation.

Auto-derived `TypedBuilder` traits in combination with `strip_option`
attributes are very hard to use in scenarios where options passed to the
builder depend on user input. The problem is that this pattern doesn't
work, since the two match branches return different types:

```rust
let builder = match x {
    None => builder,
    Some(x) => builder.x(x),
};
```

Fortunately typed_builder 0.20 supports an additional attribute
(`fallback`) that generates a builder method that takes `Option<T>` in
addition to the method that takes naked `T`.

With this attribute, we can write `let builder = builder.x_opt(x)`.

This commit adds the `fallback` attribute to all fields that have the
`strip_option` annotation.

Signed-off-by: Leonid Ryzhyk <[email protected]>
Copy link
Contributor

@liurenjie1024 liurenjie1024 left a comment

Choose a reason for hiding this comment

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

Thanks @ryzhyk for this pr!

@liurenjie1024 liurenjie1024 merged commit d3b7f56 into apache:main Nov 26, 2024
16 checks passed
shaeqahmed pushed a commit to matanolabs/iceberg-rust that referenced this pull request Dec 9, 2024
Auto-derived `TypedBuilder` traits in combination with `strip_option`
attributes are very hard to use in scenarios where options passed to the
builder depend on user input. The problem is that this pattern doesn't
work, since the two match branches return different types:

```rust
let builder = match x {
    None => builder,
    Some(x) => builder.x(x),
};
```

Fortunately typed_builder 0.20 supports an additional attribute
(`fallback`) that generates a builder method that takes `Option<T>` in
addition to the method that takes naked `T`.

With this attribute, we can write `let builder = builder.x_opt(x)`.

This commit adds the `fallback` attribute to all fields that have the
`strip_option` annotation.

Signed-off-by: Leonid Ryzhyk <[email protected]>
Co-authored-by: Leonid Ryzhyk <[email protected]>
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.

2 participants