Пример #1
0
        private ISettingsNode DeepMerge(ObjectNode other, SettingsMergeOptions options)
        {
            var newChildren = new Dictionary <string, ISettingsNode>(children, Comparers.NodeName);

            foreach (var pair in other.children)
            {
                newChildren[pair.Key] = SettingsNodeMerger.Merge(this[pair.Key], pair.Value, options);
            }

            return(new ObjectNode(other.Name, newChildren));
        }
        /// <inheritdoc />
        public ISettingsNode Merge(ISettingsNode other, SettingsMergeOptions options = null)
        {
            if (other == null)
            {
                return(this);
            }

            if (!(other is ArrayNode otherArrayNode))
            {
                return(other);
            }

            options = options ?? SettingsMergeOptions.Default;

            switch (options.ArrayMergeStyle)
            {
            case ArrayMergeStyle.Replace:
                return(other);

            case ArrayMergeStyle.Concat:
                return(new ArrayNode(other.Name, children.Concat(other.Children).ToArray()));

            case ArrayMergeStyle.Union:
                return(new ArrayNode(other.Name, children.Union(other.Children).ToArray()));

            case ArrayMergeStyle.PerElement:
                var newChildren = children.Zip(other.Children, (c1, c2) => SettingsNodeMerger.Merge(c1, c2, options));

                if (ChildrenCount > otherArrayNode.ChildrenCount)
                {
                    newChildren = newChildren.Concat(Children.Skip(otherArrayNode.ChildrenCount));
                }

                if (ChildrenCount < otherArrayNode.ChildrenCount)
                {
                    newChildren = newChildren.Concat(other.Children.Skip(ChildrenCount));
                }

                return(new ArrayNode(other.Name, newChildren.ToArray()));

            default:
                return(null);
            }
        }
Пример #3
0
        private ISettingsNode ShallowMerge(ObjectNode other, SettingsMergeOptions options)
        {
            if (other.children.Count != children.Count)
            {
                return(other);
            }

            var newChildren = new Dictionary <string, ISettingsNode>(children.Count, Comparers.NodeName);

            foreach (var pair in children)
            {
                if (!other.children.TryGetValue(pair.Key, out var otherValue))
                {
                    return(other);
                }

                newChildren[pair.Key] = SettingsNodeMerger.Merge(pair.Value, otherValue, options);
            }

            return(new ObjectNode(other.Name, newChildren));
        }