pub struct Reference { /* private fields */ }
Expand description
Represents an attribute name or path expression identifying a value within a crate::Context.
This can be used to retrieve a value with crate::Context::get_value, or to identify an attribute or nested value that should be considered private with crate::ContextBuilder::add_private_attribute (the SDK configuration can also have a list of private attribute references).
This is represented as a separate type, rather than just a string, so that validation and parsing can be done ahead of time if an attribute reference will be used repeatedly later (such as in flag evaluations).
If the string starts with ‘/’, then this is treated as a slash-delimited path reference where the
first component is the name of an attribute, and subsequent components are the names of nested JSON
object properties. In this syntax, the escape sequences “~0” and “1” represent ‘’ and ‘/’
respectively within a path component.
If the string does not start with ‘/’, then it is treated as the literal name of an attribute.
§Example
// Given the following JSON representation of a context:
{
"kind": "user",
"key": "123",
"name": "xyz",
"address": {
"street": "99 Main St.",
"city": "Westview"
},
"a/b": "ok"
}
assert_eq!(context.get_value(&Reference::new("name")),
Some(AttributeValue::String("xyz".to_owned())));
assert_eq!(context.get_value(&Reference::new("/address/street")),
Some(AttributeValue::String("99 Main St.".to_owned())));
assert_eq!(context.get_value(&Reference::new("a/b")),
Some(AttributeValue::String("ok".to_owned())));
assert_eq!(context.get_value(&Reference::new("/a~1b")),
Some(AttributeValue::String("ok".to_owned())));
Implementations§
Source§impl Reference
impl Reference
Sourcepub fn new<S: AsRef<str>>(value: S) -> Self
pub fn new<S: AsRef<str>>(value: S) -> Self
Construct a new context attribute reference.
This constructor always returns a reference that preserves the original string, even if validation fails, so that serializing the reference to JSON will produce the original string.
Sourcepub fn error(&self) -> String
pub fn error(&self) -> String
If the reference is invalid, this method returns an error description; otherwise, it returns an empty string.
Sourcepub fn depth(&self) -> usize
pub fn depth(&self) -> usize
Returns the number of path components in the reference.
For a simple attribute reference such as “name” with no leading slash, this returns 1.
For an attribute reference with a leading slash, it is the number of slash-delimited path components after the initial slash.
§Example
assert_eq!(Reference::new("a").depth(), 1);
assert_eq!(Reference::new("/a/b").depth(), 2);
Sourcepub fn component(&self, index: usize) -> Option<&str>
pub fn component(&self, index: usize) -> Option<&str>
Retrieves a single path component from the attribute reference.
Returns the attribute name for a simple attribute reference such as “name” with no leading slash, if index is zero.
Returns the specified path component if index is less than Reference::depth, and the reference begins with a slash.
If index is out of range, it returns None.
§Examples
assert_eq!(Reference::new("a").component(0), Some("a"));
assert_eq!(Reference::new("/a/b").component(1), Some("b"));
assert_eq!(Reference::new("/a/b").component(2), None);
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Reference
impl<'de> Deserialize<'de> for Reference
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl Eq for Reference
impl StructuralPartialEq for Reference
Auto Trait Implementations§
impl Freeze for Reference
impl RefUnwindSafe for Reference
impl Send for Reference
impl Sync for Reference
impl Unpin for Reference
impl UnwindSafe for Reference
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.