internal static void HandleDuplicateProperties(Sentence firstSentence, DuplicatePropertyHandling duplicatePropertyHandling) { if (duplicatePropertyHandling == DuplicatePropertyHandling.Allow) { return; } // ReSharper disable once LoopCanBeConvertedToQuery foreach (var section in firstSentence.After.Sections()) { var duplicatePropertyGroups = section.Properties() .GroupBy(x => (string)x.PropertyToken(), (name, properties) => new { name, properties }) .Where(x => x.properties.Count() > 1) .ToList(); if (!duplicatePropertyGroups.Any()) { continue; } if (duplicatePropertyHandling == DuplicatePropertyHandling.Disallow) { throw new DuplicatePropertiesException(); } if (duplicatePropertyHandling == DuplicatePropertyHandling.Rename) { foreach (var duplicatePropertyGroup in duplicatePropertyGroups) { // append counter to each property var counter = 1; foreach (var property in duplicatePropertyGroup.properties) { var name = property.PropertyToken().Value; property.Tokens.PropertyToken().Value = $"{name}{counter++}"; } } } var removeProperties = new Action <IEnumerable <Sentence> >(propertiesToRemove => { foreach (var propertyToRemove in propertiesToRemove) { // also remove comments var comments = propertyToRemove.Comments(); foreach (var comment in comments) { comment.Remove(); } // finally the property propertyToRemove.Remove(); } }); if (duplicatePropertyHandling == DuplicatePropertyHandling.KeepFirst) { foreach (var duplicatePropertyGroup in duplicatePropertyGroups.ToList()) { // skip the first property that we want to keep var propertiesToRemove = duplicatePropertyGroup.properties.Skip(1); removeProperties(propertiesToRemove); } } if (duplicatePropertyHandling == DuplicatePropertyHandling.KeepLast) { foreach (var duplicatePropertyGroup in duplicatePropertyGroups.ToList()) { // skip the first property that we want to keep var lastProperty = duplicatePropertyGroup.properties.Last(); var propertiesToRemove = duplicatePropertyGroup.properties.Where(p => p != lastProperty); removeProperties(propertiesToRemove); } } } }
internal static void HandleDuplicateProperties(Sentence firstSentence, DuplicatePropertyHandling duplicatePropertyHandling) { if (duplicatePropertyHandling == DuplicatePropertyHandling.Allow) { return; } // ReSharper disable once LoopCanBeConvertedToQuery foreach (var section in firstSentence.After.Sections()) { var duplicatePropertyGroups = section.Properties() .GroupBy(x => (string)x.PropertyToken(), (name, properties) => new { name, properties }) .Where(x => x.properties.Count() > 1) .ToList(); if (!duplicatePropertyGroups.Any()) { continue; } if (duplicatePropertyHandling == DuplicatePropertyHandling.Disallow) { throw new DuplicatePropertiesException(); } if (duplicatePropertyHandling == DuplicatePropertyHandling.Rename) { foreach (var duplicatePropertyGroup in duplicatePropertyGroups) { // append counter to each property var counter = 1; foreach (var property in duplicatePropertyGroup.properties) { var name = property.PropertyToken().Value; property.Tokens.PropertyToken().Value = $"{name}{counter++}"; } } } var removeProperties = new Action<IEnumerable<Sentence>>(propertiesToRemove => { foreach (var propertyToRemove in propertiesToRemove) { // also remove comments var comments = propertyToRemove.Comments(); foreach (var comment in comments) { comment.Remove(); } // finally the property propertyToRemove.Remove(); } }); if (duplicatePropertyHandling == DuplicatePropertyHandling.KeepFirst) { foreach (var duplicatePropertyGroup in duplicatePropertyGroups.ToList()) { // skip the first property that we want to keep var propertiesToRemove = duplicatePropertyGroup.properties.Skip(1); removeProperties(propertiesToRemove); } } if (duplicatePropertyHandling == DuplicatePropertyHandling.KeepLast) { foreach (var duplicatePropertyGroup in duplicatePropertyGroups.ToList()) { // skip the first property that we want to keep var lastProperty = duplicatePropertyGroup.properties.Last(); var propertiesToRemove = duplicatePropertyGroup.properties.Where(p => p != lastProperty); removeProperties(propertiesToRemove); } } } }