feat(gax): add mutable extension accessors to RequestOptions - #6254
Conversation
There was a problem hiding this comment.
Code Review
This pull request bumps the version of google-cloud-gax to 1.14.0 and adds get_extension_mut and get_extension_or_default_mut methods to RequestOptions to support mutable extension retrieval. The review feedback correctly points out that the Clone trait bound on get_extension_or_default_mut is unnecessary because the default value is inserted directly by value, and removing it would make the API more flexible.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6254 +/- ##
=======================================
Coverage 96.17% 96.17%
=======================================
Files 288 288
Lines 75433 75464 +31
=======================================
+ Hits 72545 72580 +35
+ Misses 2888 2884 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Add `get_extension_mut` and `get_extension_or_default_mut` to the `RequestOptionsExt` trait and implement them for `RequestOptions`. These methods allow client library layers to mutate request option extensions in place (such as injecting or updating headers in an existing `HeaderMap`) without needing to clone or re-insert the extension.
615ee69 to
01be876
Compare
coryan
left a comment
There was a problem hiding this comment.
I think you intended this as an example:
These methods allow client library layers to mutate request option extensions in place (such as injecting or updating headers in an existing HeaderMap) without needing to clone or re-insert the extension.
I must ask, if this is the only use-case you had in mind, would the changes in #6260 satisfy your needs? I would like to avoid two different ways to set headers.
If you had other cases in mind, then this looks good.
|
@coryan Thanks for taking a look at this.
No, this was actually for a specific use case that we have in Spanner. And it serves a different purpose than #6260. #6260 is to give external users access to adding headers. This is to allow our clients to directly mutate extensions (that again can contain headers) without the need to clone them. The Spanner client includes at least one header with every request (
// Current code in Spanner client:
let mut headers = options
.get_extension::<HeaderMap>()
.cloned() // <-- clones the entire map of existing headers
.unwrap_or_default();
headers.insert(REQUEST_ID_HEADER.clone(), val);
options.insert_extension(headers)However, with this PR, we can change the above code to this: // With this PR:
options
.get_extension_or_default_mut::<HeaderMap>()
.insert(REQUEST_ID_HEADER.clone(), val); |
Add
get_extension_mutandget_extension_or_default_mutto theRequestOptionsExttrait and implement them forRequestOptions.These methods allow client library layers to mutate request option extensions in place (such as injecting or updating headers in an existing
HeaderMap) without needing to clone or re-insert the extension.