parse static method

EventStoreClientSettings parse(
  1. String connectionString
)

Parse connectionString into EventStoreClientSettings.

Implementation

static EventStoreClientSettings parse(String connectionString) {
  var currentIndex = 0;
  final schemeIndex = connectionString.indexOf(_SchemeSeparator);
  if (schemeIndex == -1) {
    throw NoSchemeException(
      "Scheme '$_SchemeSeparator' is missing",
    );
  }
  final scheme = _parseScheme(connectionString.substring(0, schemeIndex));
  currentIndex = schemeIndex + _SchemeSeparator.length;

  final userInfoIndex = connectionString.indexOf(_UserInfoSeparator);
  var userInfo = <String, String>{};
  if (userInfoIndex != -1) {
    userInfo = _parseUserInfo(
      connectionString.substring(currentIndex, userInfoIndex),
    );
    currentIndex = userInfoIndex + _UserInfoSeparator.length;
  }

  var slashIndex = connectionString.indexOf(_Slash, currentIndex);
  var questionMarkIndex = connectionString.indexOf(
    _QuestionMark,
    max(currentIndex, slashIndex),
  );
  var endIndex = connectionString.length;

  if (slashIndex == -1) slashIndex = _MaxValue;
  if (questionMarkIndex == -1) questionMarkIndex = _MaxValue;

  final hostSeparatorIndex = min(
    min(slashIndex, questionMarkIndex),
    endIndex,
  );
  final hosts = _parseHosts(connectionString.substring(
    currentIndex,
    hostSeparatorIndex,
  ));
  currentIndex = hostSeparatorIndex;

  var path = '';
  if (slashIndex != _MaxValue) {
    path = connectionString.substring(
      currentIndex,
      min(questionMarkIndex, endIndex),
    );
  }

  if (path != '' && path != '/') {
    throw ConnectionStringParseException(
      'The specified path must be either an empty string or a forward slash (/)',
      "the following path was found instead: '$path'",
    );
  }

  var options = <String, String>{};
  if (questionMarkIndex != _MaxValue) {
    currentIndex = questionMarkIndex + _QuestionMark.length;
    options = _parseOptions(connectionString.substring(currentIndex));
  }

  final isSingleNode = hosts.length == 1 && scheme != UriSchemeDiscover;
  return EventStoreClientSettings(
    gossipSeeds: isSingleNode ? [] : hosts,
    connectionName: options[ConnectionName],
    singleNode: isSingleNode ? hosts.first : null,
    defaultCredentials: UserCredentials.from(userInfo),
    useTls: _getOrDefault<bool>(
      options,
      key: Tls,
      defaultValue: Defaults.UseTls,
      map: (value) => value.toLowerCase() == 'true',
    ),
    tlsSetup: TlsSetup(
      verifyCert: _getOrDefault<bool>(
        options,
        key: TlsVerifyCert,
        defaultValue: Defaults.TlsVerifyCert,
        map: (value) => value.toLowerCase() == 'true',
      ),
      publicKeyPath: _getOrDefault<String?>(
        options,
        key: TlsPublicKeyPath,
        defaultValue: null,
      ),
    ),
    keepAliveTimeout: _getOrDefault<Duration>(
      options,
      key: KeepAliveTimeout,
      defaultValue: Defaults.NoneDuration,
      map: (value) => Duration(milliseconds: int.parse(value)),
    ),
    keepAliveInterval: _getOrDefault<Duration>(
      options,
      key: KeepAliveInterval,
      defaultValue: Defaults.NoneDuration,
      map: (value) => Duration(milliseconds: int.parse(value)),
    ),
    gossipTimeout: _getOrDefault<Duration>(
      options,
      key: GossipTimeout,
      defaultValue: Defaults.GossipTimeout,
      map: (value) => Duration(milliseconds: int.parse(value)),
    ),
    discoveryInterval: _getOrDefault<Duration>(
      options,
      key: DiscoveryInterval,
      defaultValue: Defaults.DiscoveryInterval,
      map: (value) => Duration(milliseconds: int.parse(value)),
    ),
    maxDiscoverAttempts: _getOrDefault<int>(
      options,
      key: MaxDiscoverAttempts,
      defaultValue: Defaults.MaxDiscoverAttempts,
      map: (value) => int.parse(value),
    ),
    operationTimeout: _getOrDefault<Duration>(
      options,
      key: OperationTimeout,
      defaultValue: Defaults.OperationTimeout,
      map: (value) => Duration(milliseconds: int.parse(value)),
    ),
    nodePreference: _getOrDefault<$e.NodePreference>(
      options,
      key: NodePreference,
      defaultValue: Defaults.NodePreference,
      map: (value) => $e.NodePreference.values.firstWhere(
        (e) => enumName(e).toLowerCase() == value.toLowerCase(),
        orElse: () => Defaults.NodePreference,
      ),
    ),
    batchAppend: _getOrDefault<bool>(
      options,
      key: BatchAppend,
      defaultValue: Defaults.BatchAppend,
      map: (value) => value.toLowerCase() == 'true',
    ),
    batchAppendSize: _getOrDefault<int>(
      options,
      key: BatchAppendSize,
      defaultValue: Defaults.BatchAppendSize,
      map: (value) => int.parse(value),
    ),
    batchAppendDeadline: _getOrDefault<Duration?>(
      options,
      key: BatchAppendDeadline,
      defaultValue: null,
      map: (value) => Duration(milliseconds: int.parse(value)),
    ),
  );
}