merge method

Merges this instances with other.

Implementation

@override
EntityResolutionRules merge(EntityResolutionRules? other) {
  if (other == null || other.isInnocuous || identical(this, other)) {
    return isInnocuous ? innocuous : this;
  } else if (isInnocuous) {
    return other;
  }

  var bothMergeTolerant = mergeTolerant && other.mergeTolerant;

  var allowEntityFetch = _mergeFlag(
    this,
    other,
    _allowEntityFetch,
    other._allowEntityFetch,
    'allowEntityFetch',
  );

  var allowReadFile = this.allowReadFile || other.allowReadFile;

  var allLazy = _mergeFlag(
    this,
    other,
    this.allLazy,
    other.allLazy,
    'allLazy',
  );

  var allEager = _mergeFlag(
    this,
    other,
    this.allEager,
    other.allEager,
    'allEager',
  );

  // `allEager` and `allLazy` can't both be `true` (see `_validateImpl`):
  // a cross-flag conflict (one side `allEager`, the other `allLazy`) is
  // dropped as the same-flag conflicts of [_mergeFlag] are: both sides
  // tolerant, both flags dropped; one side tolerant, it yields its flag to
  // the intolerant side; neither tolerant, the merge is invalid (thrown by
  // `merge.validate()` below).
  if ((allEager ?? false) && (allLazy ?? false)) {
    if (bothMergeTolerant) {
      allEager = null;
      allLazy = null;
    } else if (mergeTolerant || other.mergeTolerant) {
      var tolerant = mergeTolerant ? this : other;
      if (tolerant.allEager ?? false) allEager = null;
      if (tolerant.allLazy ?? false) allLazy = null;
    }
  }

  var lazyEntityTypes = this.lazyEntityTypes
      .merge(other.lazyEntityTypes)
      .nullIfEmpty();

  var eagerEntityTypes = this.eagerEntityTypes
      .merge(other.eagerEntityTypes)
      .nullIfEmpty();

  var merge = EntityResolutionRules(
    allowEntityFetch: allowEntityFetch,
    allowReadFile: allowReadFile,
    lazyEntityTypes: lazyEntityTypes,
    eagerEntityTypes: eagerEntityTypes,
    allLazy: allLazy,
    allEager: allEager,
    mergeTolerant: bothMergeTolerant,
  );

  if ((mergeTolerant || other.mergeTolerant)) {
    var conflict = merge._hasConflictingEntityTypes();

    if (conflict != null) {
      if (bothMergeTolerant) {
        merge = merge.copyWith(conflictingEntityTypes: conflict);
      } else {
        // Exactly one side is tolerant: it yields the conflicting types to
        // the intolerant side.
        var (tolerant, intolerant) = mergeTolerant
            ? (this, other)
            : (other, this);

        var lazyEntityTypes = _mergeTypesDroppingConflict(
          tolerant.lazyEntityTypes,
          intolerant.lazyEntityTypes,
          conflict,
        );

        var eagerEntityTypes = _mergeTypesDroppingConflict(
          tolerant.eagerEntityTypes,
          intolerant.eagerEntityTypes,
          conflict,
        );

        merge = EntityResolutionRules(
          allowEntityFetch: allowEntityFetch,
          allowReadFile: allowReadFile,
          lazyEntityTypes: lazyEntityTypes.nullIfEmpty(),
          eagerEntityTypes: eagerEntityTypes.nullIfEmpty(),
          allLazy: allLazy,
          allEager: allEager,
        );
      }
    }
  }

  merge.validate();

  return merge;
}