evaluate<T> static method

DVFlagResolution<T> evaluate<T>(
  1. DVFeatureFlag<T> flag,
  2. DVFlagRules? rules,
  3. DVFlagContext context, {
  4. Map<String, Object?> overrides = const <String, Object?>{},
  5. bool allowOverrides = true,
})

The pure evaluation: an override if allowed, then the first rule that admits context, then the compiled default.

No clock, no storage, no randomness — which is what lets a client and a backend function agree.

Implementation

static DVFlagResolution<T> evaluate<T>(
  DVFeatureFlag<T> flag,
  DVFlagRules? rules,
  DVFlagContext context, {
  Map<String, Object?> overrides = const <String, Object?>{},
  bool allowOverrides = true,
}) {
  DVFlagResolution<T> fallback(List<String> codes, {int? rule}) =>
      DVFlagResolution<T>(
        value: flag.defaultValue,
        source: DVFlagSource.defaults,
        codes: codes,
        rule: rule,
      );

  if (allowOverrides && overrides.containsKey(flag.key)) {
    final (bool, T?) read = _read(flag, overrides[flag.key]);
    if (read.$1) {
      return DVFlagResolution<T>(
        value: read.$2 as T,
        source: DVFlagSource.override,
        codes: const <String>['DV-FLAGS-008'],
      );
    }
  }

  if (rules == null) return fallback(const <String>['DV-FLAGS-001']);

  final List<DVFlagRule>? entry = rules.flags[flag.key];
  if (entry == null) return fallback(const <String>[]);

  for (int index = 0; index < entry.length; index++) {
    final DVFlagRule rule = entry[index];
    final DVFlagTarget? target = rule.target;
    if (target != null && !target.matches(context)) continue;
    final DVFlagRollout? rollout = rule.rollout;
    if (rollout != null) {
      final String? subject = context.subjectFor(rollout.by);
      // Held, not rolled: a flag that flickers between two frames is a bug
      // report nobody can reproduce.
      if (subject == null) {
        return fallback(const <String>['DV-FLAGS-005'], rule: index);
      }
      if (!rollout.includes(flag.key, subject)) continue;
    }
    final (bool, T?) read = _read(flag, rule.value);
    if (!read.$1) {
      return fallback(const <String>['DV-FLAGS-006'], rule: index);
    }
    return DVFlagResolution<T>(
      value: read.$2 as T,
      source: DVFlagSource.rules,
      rulesVersion: rules.rulesVersion,
      rule: index,
    );
  }
  return fallback(const <String>[]);
}