cedar-policy / cedar

Implementation of the Cedar Policy Language
https://www.cedarpolicy.com
Apache License 2.0
892 stars 80 forks source link

Add get_string convenience method to Context for string value extraction #1319

Closed tamas-jozsa closed 1 week ago

tamas-jozsa commented 1 week ago

Describe the improvement you'd like to request

Problem

When working with Context objects, extracting values requires verbose pattern matching through the internal AST structure. This creates unnecessary boilerplate code and complexity for a common operation.

Proposed Solution

Add a get convenience method to Context that would simplify value extraction:

impl Context {
    /// Retrieves a value from the Context by its key.
    pub fn get(&self, key: &str) -> Option<&Value>
}
// Current approach requires complex pattern matching
// New approach would be:
let ray_id = request
            .context()
            .and_then(|ctx| ctx.get("rayId"))
            .map(|id| id.to_string())
            .unwrap_or_else(|| "default_id".to_string());

Let me know if you'd like any adjustments to the issue title or description!

Open PR: https://github.com/cedar-policy/cedar/pull/1318

Describe alternatives you've considered

No response

Additional context

No response

Is this something that you'd be interested in working on?