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§

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.

Returns true if the reference is valid.

If the reference is invalid, this method returns an error description; otherwise, it returns an empty string.

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);

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

A default Reference is empty and invalid.

Deserialize this value from the given Serde deserializer. Read more

Displays the input string used to construct the Reference.

Formats the value using the given formatter. Read more

AttributeNames are converted into References based on the presence or absence of a leading ‘/’.

Although References are able to represent plain, top-level attribute names, they cannot represent those that begin with a leading ‘/’ because that signifies the pointer syntax.

Therefore, if the first character is a ‘/’ the string must be escaped.

This results in the equivalent Reference representation of that [AttributeName].

Note that References constructed from an AttributeName will serialize to the string passed into the Reference constructor, not the original AttributeName. This is desirable since data should be “upgraded” into the new format as it is encountered.

Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more