parse static method

FeatureGate parse(
  1. String rawGate
)

Parse a gate string of the form type:value. Throws FormatException for malformed syntax (no colon, empty value).

Implementation

static FeatureGate parse(String rawGate) {
  final raw = rawGate.trim();
  final colon = raw.indexOf(':');
  if (colon <= 0) {
    throw FormatException(
      'invalid gate syntax "$rawGate" — expected "<type>:<value>"',
    );
  }
  final typePart = raw.substring(0, colon).trim().toLowerCase();
  final valuePart = raw.substring(colon + 1).trim();
  if (valuePart.isEmpty) {
    throw FormatException(
      'invalid gate syntax "$rawGate" — empty gate value',
    );
  }

  switch (typePart) {
    case 'membership':
      return FeatureGate._(FeatureGateType.membership, valuePart, [
        valuePart,
      ], raw);
    case 'locale':
      final locales = valuePart
          .split(',')
          .map((l) => l.trim())
          .where((l) => l.isNotEmpty)
          .toList(growable: false);
      if (locales.isEmpty) {
        throw FormatException(
          'invalid gate syntax "$rawGate" — empty locale list',
        );
      }
      return FeatureGate._(FeatureGateType.locale, valuePart, locales, raw);
    case 'variant':
      final variants = valuePart
          .split('|')
          .map((v) => v.trim())
          .where((v) => v.isNotEmpty)
          .toList(growable: false);
      if (variants.isEmpty) {
        throw FormatException(
          'invalid gate syntax "$rawGate" — empty variant list',
        );
      }
      return FeatureGate._(FeatureGateType.variant, valuePart, variants, raw);
    case 'custom':
      return FeatureGate._(FeatureGateType.custom, valuePart, [
        valuePart,
      ], raw);
    case 'platform':
      // Issue #2066: comma-separated OS allow-list, the same list shape
      // `locale:` uses. Whitespace around each name is trimmed; an empty
      // list (nothing usable left) is a malformed gate, not a vacuous
      // pass — the parse layer must never hand the runtime a gate that
      // could be read as "enabled everywhere by listing nothing".
      final platforms = valuePart
          .split(',')
          .map((os) => os.trim())
          .where((os) => os.isNotEmpty)
          .toList(growable: false);
      if (platforms.isEmpty) {
        throw FormatException(
          'invalid gate syntax "$rawGate" — empty platform list',
        );
      }
      return FeatureGate._(
        FeatureGateType.platform,
        valuePart,
        platforms,
        raw,
      );
    default:
      throw FormatException(
        'unknown gate type "$typePart" in gate "$rawGate" '
        '(supported: membership, locale, variant, custom, platform)',
      );
  }
}