|
268 | 268 | //! ```
|
269 | 269 | //! ---
|
270 | 270 | //!
|
| 271 | +//! In some cases, you might want to exclude certain fields from the generated field enums (like |
| 272 | +//! `SparseFooFieldWithEditContext`). This is particularly useful for fields that are not suitable for |
| 273 | +//! field filtering, such as catch-all fields for additional data or serde flattened fields. The |
| 274 | +//! `WpContextualExcludeFromFields` attribute can be used for this purpose: |
| 275 | +//! |
| 276 | +//! ``` |
| 277 | +//! # use wp_contextual::WpContextual; |
| 278 | +//! #[derive(WpContextual)] |
| 279 | +//! pub struct SparseComment { |
| 280 | +//! #[WpContext(edit, embed, view)] |
| 281 | +//! pub id: Option<u32>, |
| 282 | +//! #[WpContext(edit, embed, view)] |
| 283 | +//! #[WpContextualExcludeFromFields] |
| 284 | +//! pub additional_fields: Option<String>, |
| 285 | +//! } |
| 286 | +//! # // We need these 2 lines for UniFFI |
| 287 | +//! # uniffi::setup_scaffolding!(); |
| 288 | +//! # fn main() {} |
| 289 | +//! ``` |
| 290 | +//! |
| 291 | +//! This will generate the contextual types as usual, but the field enums will exclude the `additional_fields` field: |
| 292 | +//! |
| 293 | +//! ``` |
| 294 | +//! pub enum SparseCommentFieldWithEditContext { |
| 295 | +//! Id, |
| 296 | +//! // Note: additional_fields is excluded from field filtering |
| 297 | +//! } |
| 298 | +//! pub enum SparseCommentFieldWithEmbedContext { |
| 299 | +//! Id, |
| 300 | +//! // Note: additional_fields is excluded from field filtering |
| 301 | +//! } |
| 302 | +//! pub enum SparseCommentFieldWithViewContext { |
| 303 | +//! Id, |
| 304 | +//! // Note: additional_fields is excluded from field filtering |
| 305 | +//! } |
| 306 | +//! ``` |
| 307 | +//! |
| 308 | +//! --- |
| 309 | +//! |
271 | 310 | //! Most endpoints support filtering the fields by `_fields` argument which means some of the
|
272 | 311 | //! fields might be missing from the response. In these cases, we want to use the `Sparse` type.
|
273 | 312 | //! In order to make it easier to combine `context` & `_fields` arguments, and get the most useful
|
@@ -342,6 +381,8 @@ mod wp_contextual;
|
342 | 381 | /// type with the appropriate contextual type: `BazWithEditContext`, `BazWithEmbedContext` or
|
343 | 382 | /// `BazWithViewContext`.
|
344 | 383 | /// * `[WpContextualOption]` is used to tell the compiler to keep the field's `Option` type.
|
| 384 | +/// * `[WpContextualExcludeFromFields]` is used to exclude a field from the generated field enums |
| 385 | +/// (`SparseFooFieldWithEditContext`, etc.) while still including it in the contextual types. |
345 | 386 | /// * Generated types will have the following derive macros:
|
346 | 387 | /// `#[derive(Debug, serde::Serialize, serde::Deserialize, uniffi::Record)]`. These types are meant
|
347 | 388 | /// to be used for the
|
@@ -453,7 +494,12 @@ mod wp_contextual;
|
453 | 494 | /// `#[WpContextualOption]`.
|
454 | 495 | #[proc_macro_derive(
|
455 | 496 | WpContextual,
|
456 |
| - attributes(WpContext, WpContextualField, WpContextualOption) |
| 497 | + attributes( |
| 498 | + WpContext, |
| 499 | + WpContextualField, |
| 500 | + WpContextualOption, |
| 501 | + WpContextualExcludeFromFields |
| 502 | + ) |
457 | 503 | )]
|
458 | 504 | pub fn derive(input: TokenStream) -> TokenStream {
|
459 | 505 | wp_contextual::wp_contextual(parse_macro_input!(input))
|
|
0 commit comments