Example #1
0
        public static Dictionary <string, AttributeDefinition> Diff(AttributeDefinition left, AttributeDefinition right)
        {
            var leftIfNrMap  = left.DefinitionCollection.AttributeDefinition.ToDictionary(a => a.Ifnr);
            var rightIfNrMap = right.DefinitionCollection.AttributeDefinition.ToDictionary(a => a.Ifnr);

            if (!EqualityComparer <string> .Default.Equals(left.DefinitionCollection.Region, right.DefinitionCollection.Region))
            {
                throw new Exception($"Regions don't match ({left.DefinitionCollection.Region} != {right.DefinitionCollection.Region})");
            }

            List <Data.AttributeDefinition> leftConflicts  = new List <Data.AttributeDefinition>();
            List <Data.AttributeDefinition> rightConflicts = new List <Data.AttributeDefinition>();
            List <Data.AttributeDefinition> equals         = new List <Data.AttributeDefinition>();
            List <Data.AttributeDefinition> onlyLeft       = new List <Data.AttributeDefinition>();

            foreach (var leftIfNr in leftIfNrMap.Keys)
            {
                if (!rightIfNrMap.ContainsKey(leftIfNr))
                {
                    onlyLeft.Add(leftIfNrMap[leftIfNr]);
                }
                else if (!leftIfNrMap[leftIfNr].Equals(rightIfNrMap[leftIfNr]))
                {
                    leftConflicts.Add(leftIfNrMap[leftIfNr]);
                    rightConflicts.Add(rightIfNrMap[leftIfNr]);
                }
                else
                {
                    equals.Add(leftIfNrMap[leftIfNr]);
                }
            }

            return(new Dictionary <string, AttributeDefinition>()
            {
                { "equal", left.NewWithSameRegion(equals) },
                { "leftConflicts", left.NewWithSameRegion(leftConflicts) },
                { "rightConflicts", right.NewWithSameRegion(rightConflicts) },
                { "onlyLeft", left.NewWithSameRegion(onlyLeft) },
                { "onlyRight", right.NewWithSameRegion(rightIfNrMap.Where(r => !leftIfNrMap.ContainsKey(r.Key)).Select(r => r.Value)) }
            });
        }
Example #2
0
        public static Dictionary <string, object> Merge(AttributeDefinition left, AttributeDefinition right, MergeStrategy mergeStrategy)
        {
            var leftIfNrMap  = left.DefinitionCollection.AttributeDefinition.ToDictionary(a => a.Ifnr);
            var rightIfNrMap = right.DefinitionCollection.AttributeDefinition.ToDictionary(a => a.Ifnr);

            var finalMerge = leftIfNrMap
                             .Where(a => !rightIfNrMap.ContainsKey(a.Key))
                             .Concat(rightIfNrMap.Where(a => !leftIfNrMap.ContainsKey(a.Key)))
                             .Select(a => a.Value)
                             .ToList();

            var report = new List <string[]>();

            foreach (var ifnr in leftIfNrMap.Keys.Where(k => rightIfNrMap.ContainsKey(k)))
            {
                var la = leftIfNrMap[ifnr];
                var ra = rightIfNrMap[ifnr];
                if (!la.Equals(ra))
                {
                    switch (mergeStrategy)
                    {
                    case MergeStrategy.ThrowExceptionOnFirst:
                        throw new Exception($"IfNr #{ifnr} causes conflicts (left name '{la.Text}' != '{ra.Text}'");

                    case MergeStrategy.TryCaseInvariantPreferRight:
                    case MergeStrategy.TryCaseInvariantPreferLeft:
                        if (string.Equals(la.Text, ra.Text, StringComparison.CurrentCultureIgnoreCase))
                        {
                            if (mergeStrategy == MergeStrategy.TryCaseInvariantPreferLeft)
                            {
                                finalMerge.Add(la);
                            }
                            else
                            {
                                finalMerge.Add(ra);
                            }

                            report.Add(new string[] { $"{la.Ifnr}", $"Case invariant match '{la.Text}' vs. '{ra.Text}'" });
                        }
                        else
                        {
                            throw new Exception($"IfNr #{ifnr} causes conflicts (left name '{la.Text}' != '{ra.Text}'");
                        }
                        break;

                    case MergeStrategy.UseLeftSideByDefault:
                        finalMerge.Add(la);
                        report.Add(new string[] { $"{la.Ifnr}", $"Using left: '{la.Text}' vs. '{ra.Text}'" });
                        break;

                    case MergeStrategy.UseRightSideByDefault:
                        finalMerge.Add(ra);
                        report.Add(new string[] { $"{la.Ifnr}", $"Using right: '{la.Text}' vs. '{ra.Text}'" });
                        break;
                    }
                }
            }

            return(new Dictionary <string, object>()
            {
                { "attributeDefintion", left.NewWithSameRegion(finalMerge) },
                { "report", report.ToArray() }
            });
        }