set method Null safety

LDAttributesBuilder set(
  1. String name,
  2. LDValue value
)

Sets the value of any attribute for the Context.

This method uses the LDValue type to represent a value of any JSON type: null, boolean, number, string, array, or object. For all attribute names that do not have special meaning to LaunchDarkly, you may use any of those types. Values of different JSON types are always treated as different values: for instance, null, false, and the empty string "" are not the same, and the number 1 is not the same as the string "1".

The following attribute names have special restrictions on their value types, and any value of an unsupported type will be ignored (leaving the attribute unchanged):

  • "kind", "key": Required and must be a non-empty string.
  • "name": Must be a non-empty string.
  • "anonymous": Must be a boolean.
  • "_meta": Is reserved for internal use.

Values that are JSON arrays or objects have special behavior when referenced in flag/segment rules.

A value of LDValue.ofNull is equivalent to removing any current non-default value of the attribute. Null is not a valid attribute value in the LaunchDarkly model; any expressions in feature flags that reference an attribute with a null value will behave as if the attribute did not exist.

Implementation

LDAttributesBuilder set(String name, LDValue value) {

  // validates attribute, will log info if invalid
  if (_validateAttribute(name, value)) {
    _attributes[name] = value;
  }

  return this;
}