resolve<T> static method

DVFlagResolution<T> resolve<T>(
  1. DVFeatureFlag<T> flag, {
  2. DVFlagContext? context,
  3. DateTime? now,
})

The answer for flag, reported, pinned and exposed as the spec says.

Implementation

static DVFlagResolution<T> resolve<T>(
  DVFeatureFlag<T> flag, {
  DVFlagContext? context,
  DateTime? now,
}) {
  final DateTime at = now ?? DateTime.now().toUtc();
  final DVFlagContext ctx = context ?? DVFlags.context();

  if (flag.isExpired(at) && _reportedOnce.add('004:${flag.key}')) {
    onDiagnostic(
      'DV-FLAGS-004',
      '"${flag.key}" is past its declared expiry '
          '(${flag.expires.toIso8601String()}), owner ${flag.owner}',
    );
  }

  if (flag.settle == DVFlagSettle.onNextLaunch &&
      _pins.containsKey(flag.key)) {
    return _pins[flag.key]! as DVFlagResolution<T>;
  }

  final DVFlagResolution<T> resolution = evaluate(
    flag,
    _rules,
    ctx,
    overrides: overridesInForce,
    allowOverrides: overridesAllowed,
  );

  for (final String code in resolution.codes) {
    switch (code) {
      case 'DV-FLAGS-001':
        if (_reportedOnce.add('001')) {
          onDiagnostic(code,
              'no rule set has synced; flags answered with the defaults '
              'compiled into the build');
        }
      case 'DV-FLAGS-005':
        onDiagnostic(code,
            '"${flag.key}" has a percentage rollout and the context has no '
            'subject for it; the flag held its default');
      case 'DV-FLAGS-006':
        onDiagnostic(code,
            "a rule's value for \"${flag.key}\" is not a $T; the flag held "
            'its default');
      case 'DV-FLAGS-008':
        onDiagnostic(code,
            'a local override is in force for "${flag.key}"; this build is '
            'not answering from the rules');
    }
  }

  final Duration? limit = maxAge;
  final DateTime? received = _receivedAt;
  if (_rules != null &&
      limit != null &&
      received != null &&
      at.difference(received) > limit) {
    onDiagnostic(
      'DV-FLAGS-009',
      'the rule set is older than flags.maxAge (${at.difference(received)}) '
          'and is still in use',
    );
  }

  if (flag.settle == DVFlagSettle.onNextLaunch) {
    _pins[flag.key] = resolution;
  }

  _expose(flag, resolution, ctx);
  return resolution;
}