Add accessors for the observed and desired XR and composed resources #34
Description
Found while building composition functions on function-sdk-rust 0.3.1.
Today. Functions reach into the request and response through Option chains, and the two most common ones already appear twice in the SDK's own docs and the CLI template:
- reading the observed XR: the example function and the Rust template's
function.rs.tmplin crossplane/cli#374; - writing a desired composed resource: the example function and the README.
let xr = req.observed.as_ref().and_then(|s| s.composite.as_ref());
let observed_bucket = req.observed.as_ref().and_then(|s| s.resources.get("bucket"));
let bucket = rsp.desired.get_or_insert_default().resources.entry("bucket".to_string()).or_default();
let desired_xr = rsp.desired.get_or_insert_default().composite.get_or_insert_default();Go. request.GetObservedCompositeResource and GetObservedComposedResources, and response.SetDesiredCompositeResource and SetDesiredComposedResources. These accessors are parity with Go, not new surface.
Proposed. Four borrowing accessors with no conversion. Typed reads stay one call through resource::get::<T>.
// request
pub fn observed_composite(req: &RunFunctionRequest) -> Option<&Resource>;
pub fn observed_composed<'a>(req: &'a RunFunctionRequest, name: &str) -> Option<&'a Resource>;
// response: insert an empty resource when the response has none
pub fn desired_composite(rsp: &mut RunFunctionResponse) -> &mut Resource;
pub fn desired_composed(rsp: &mut RunFunctionResponse, name: impl Into<String>) -> &mut Resource;let xr: Xr = resource::get(request::observed_composite(&req))?;
resource::update(response::desired_composed(&mut rsp, "bucket"), &bucket)?;
resource::update_status(response::desired_composite(&mut rsp), &status)?;The template, the README and the example would switch to these in the same change. The response accessors modify the entry in place, so with #31 an updated Resource keeps its unknown fields. Go also has request.GetDesiredCompositeResource and GetDesiredComposedResources; read-only request::desired_composite and request::desired_composed could follow if a function needs them, but nothing in the template or docs does today.
Prototyped against v0.3.1 with tests; clippy is clean.
Activity