ReadPreference.fromOptions constructor

ReadPreference.fromOptions(
  1. Map<String, Object> options
)

We can accept three formats for ReadPreference inside Options:

  • optionskeyReadPreference id ReadPreference an Instance of ReadPreference)
  • optionskeyReadPrefernce is Map (in format: {keyMode:
  • optionskeyReadPreference is ReadPreferenceMode. In this this case we expect the other options to be inside the options map itself (ex. optionskeyReadPreferencTags)

Implementation

factory ReadPreference.fromOptions(Map<String, Object> options) {
  if (options[keyReadPreference] == null) {
    throw MongoDartError('ReadPreference mode is needed');
  }
  dynamic readPreference = options[keyReadPreference];
  if (readPreference is ReadPreferenceMode) {
    return ReadPreference(readPreference,
        tags: options[keyReadPreferenceTags] as List?,
        maxStalenessSeconds: options[keyMaxStalenessSecond] as int?,
        hedgeOptions: options[keyHedgeOptions] as Map<String, Object>?);
  } else if (readPreference is Map) {
    var mode = readPreference[keyMode] as String?;
    if (mode != null) {
      return ReadPreference(getReadPreferenceModeFromString(mode),
          tags: readPreference[keyReadPreferenceTags] as List?,
          maxStalenessSeconds: readPreference[keyMaxStalenessSecond] as int?,
          hedgeOptions:
              readPreference[keyHedgeOptions] as Map<String, Object>?);
    }
  } else if (options[keyReadPreference] is ReadPreference) {
    return options[keyReadPreference] as ReadPreference;
  }
  throw UnsupportedError('The "$keyReadPreference" value is of an '
      'unmanaged type ${options[keyReadPreference].runtimeType}');
}