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 = _allowEntityFetch;
  if (other._allowEntityFetch != null) {
    if (allowEntityFetch == null) {
      allowEntityFetch = other._allowEntityFetch;
    } else if (allowEntityFetch != other._allowEntityFetch) {
      if (bothMergeTolerant) {
        allowEntityFetch = null;
      } else if (mergeTolerant) {
        allowEntityFetch = other._allowEntityFetch;
      } else if (!other.mergeTolerant) {
        throw MergeEntityRulesError(this, other, 'allowEntityFetch');
      }
    }
  }

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

  var allLazy = this.allLazy;
  if (other.allLazy != null) {
    if (allLazy == null) {
      allLazy = other.allLazy;
    } else if (allLazy != other.allLazy) {
      if (bothMergeTolerant) {
        allLazy = null;
      } else if (mergeTolerant) {
        allLazy = other.allLazy;
      } else if (!other.mergeTolerant) {
        throw MergeEntityRulesError(this, other, 'allLazy');
      }
    }
  }

  var allEager = this.allEager;
  if (other.allEager != null) {
    if (allEager == null) {
      allEager = other.allEager;
    } else if (allEager != other.allEager) {
      if (bothMergeTolerant) {
        allEager = null;
      } else if (mergeTolerant) {
        allEager = other.allEager;
      } else if (!other.mergeTolerant) {
        throw MergeEntityRulesError(this, other, 'allEager');
      }
    }
  }

  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 {
        List<Type>? lazyEntityTypes;
        List<Type>? eagerEntityTypes;

        if (mergeTolerant) {
          lazyEntityTypes = this
              .lazyEntityTypes
              .without(conflict)
              .merge(other.lazyEntityTypes);

          eagerEntityTypes = this
              .eagerEntityTypes
              .without(conflict)
              .merge(other.eagerEntityTypes);
        } else if (other.mergeTolerant) {
          lazyEntityTypes = other.lazyEntityTypes
              .without(conflict)
              .merge(this.lazyEntityTypes);

          eagerEntityTypes = other.eagerEntityTypes
              .without(conflict)
              .merge(this.eagerEntityTypes);
        }

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

  merge.validate();

  return merge;
}