resolveReadPreference function

ReadPreference? resolveReadPreference(
  1. dynamic parent,
  2. Map<String, Object> options, {
  3. bool? inheritReadPreference = true,
})

Resolves a read preference based on well-defined inheritance rules. This method will not only determine the read preference (if there is one), but will also ensure the returned value is a properly constructed instance of ReadPreference.

@param {Collection|Db|MongoClient} parent The parent of the operation on which to determine the read preference, used for determining the inherited read preference. @param {Object} options The options passed into the method, potentially containing a read preference @returns {(ReadPreference|null)} The resolved read preference

Implementation

ReadPreference? resolveReadPreference(parent, Map<String, Object> options,
    {bool? inheritReadPreference = true}) {
  //options ??= <String, Object>{};
  inheritReadPreference ??= true;

  ReadPreference? inheritedReadPreference;

  if (inheritReadPreference) {
    if (parent is DbCollection) {
      inheritedReadPreference = parent.readPreference;
    } else if (parent is Db) {
      inheritedReadPreference = parent.readPreference;
    } //Todo MongoClient class not yet Implemented
    /*else if (parent is MongoClient) {
    inheritedReadPreference = parent.readPreference;
  }*/
  }

  if (options[keyReadPreference] != null) {
    return ReadPreference.fromOptions(options);
  } // Todo session Class not yet implemented
  /*else if ((session?.inTransaction() ?? false) && session.transaction.options[CommandOperation.keyReadPreference]) {
    // The transaction’s read preference MUST override all other user configurable read preferences.
    readPreference = session.transaction.options[CommandOperation.keyReadPreference];
  }*/
  else if (inheritedReadPreference != null) {
    return inheritedReadPreference;
  } else {
    if (inheritReadPreference) {
      throw ArgumentError('No readPreference was provided or inherited.');
    }
  }
  return null;
}