예제 #1
0
        /// <inheritdoc />
        public ISettingsNode Merge(ISettingsNode other, SettingsMergeOptions options = null)
        {
            if (other == null)
            {
                return(this);
            }

            if (!(other is ArrayNode))
            {
                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()));

            default:
                return(null);
            }
        }
        public void Should_merge_arrays_of_object_nodes_with_per_element_merge_style()
        {
            var array1 = Array("array1",
                               Object(null, ("A", "1")),
                               Object(null, ("A", "2")),
                               Object(null, ("A", "3")));

            var array2 = Array("array2",
                               Object(null, ("B", "2")),
                               Object(null, ("B", "1")));

            var options = new SettingsMergeOptions
            {
                ArrayMergeStyle  = ArrayMergeStyle.PerElement,
                ObjectMergeStyle = ObjectMergeStyle.Deep
            };

            array1.Merge(array2, options).Should().Be(Array("array2",
                                                            Object(null, ("A", "1"), ("B", "2")),
                                                            Object(null, ("A", "2"), ("B", "1")),
                                                            Object(null, ("A", "3")))
                                                      );

            array2.Merge(array1, options).Should().Be(Array("array1",
                                                            Object(null, ("A", "1"), ("B", "2")),
                                                            Object(null, ("A", "2"), ("B", "1")),
                                                            Object(null, ("A", "3")))
                                                      );
        }
예제 #3
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));
        }
예제 #4
0
        public CombinedSource(
            [NotNull][ItemNotNull] IEnumerable <IConfigurationSource> sources,
            [CanBeNull] SettingsMergeOptions options)
        {
            if (sources == null || !sources.Any())
            {
                throw new ArgumentException($"{nameof(CombinedSource)}: {nameof(sources)} collection should not be empty");
            }

            this.sources = sources;
            this.options = options;
        }
예제 #5
0
        public IObservable <LogConfigurationRule[]> Build(BuildContext context)
        {
            var rulesSources = new List <IConfigurationSource>();

            rulesSources.AddRange(userSources);
            rulesSources.Add(new ObjectSource(userRules));

            var mergeOptions = new SettingsMergeOptions {
                ArrayMergeStyle = ArrayMergeStyle.Union
            };
            var mergedSource = new CombinedSource(rulesSources, mergeOptions);

            return(context.ConfigurationProvider.Observe <LogConfigurationRule[]>(mergedSource));
        }
        public static ISettingsNode Merge(
            [CanBeNull] ISettingsNode node1,
            [CanBeNull] ISettingsNode node2,
            [CanBeNull] SettingsMergeOptions options)
        {
            if (node1 == null)
            {
                return(node2);
            }

            if (node2 == null)
            {
                return(node1);
            }

            return(node1.Merge(node2, options));
        }
        /// <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);
            }
        }
예제 #8
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));
        }
예제 #9
0
        /// <inheritdoc />
        public ISettingsNode Merge(ISettingsNode other, SettingsMergeOptions options = null)
        {
            if (other == null)
            {
                return(this);
            }

            if (!(other is ObjectNode objectNodeOther))
            {
                return(other);
            }

            if (children.Count == 0)
            {
                return(other);
            }

            if (objectNodeOther.children.Count == 0)
            {
                return(this);
            }

            options = options ?? SettingsMergeOptions.Default;

            switch (options.ObjectMergeStyle)
            {
            case ObjectMergeStyle.Shallow:
                return(ShallowMerge(objectNodeOther, options));

            case ObjectMergeStyle.Deep:
                return(DeepMerge(objectNodeOther, options));

            default:
                return(null);
            }
        }
예제 #10
0
 /// <inheritdoc />
 public ISettingsNode Merge(ISettingsNode other, SettingsMergeOptions options = null) => other ?? this;
 /// <inheritdoc cref="CombineWith(IConfigurationSource,IConfigurationSource,SettingsMergeOptions)"/>
 public static IConfigurationSource CombineWith(this IConfigurationSource source, IEnumerable <IConfigurationSource> others, SettingsMergeOptions options = null) =>
 new CombinedSource(source.ToEnumerable().Concat(others), options);
 /// <inheritdoc cref="CombineWith(IConfigurationSource,IConfigurationSource,SettingsMergeOptions)"/>
 public static IConfigurationSource CombineWith(this IConfigurationSource source, SettingsMergeOptions options, params IConfigurationSource[] others) =>
 new CombinedSource(source.ToEnumerable().Concat(others), options);
예제 #13
0
        public static ISettingsNode Extract([NotNull] ClusterConfigClientState state, ClusterConfigPath path, [CanBeNull] SettingsMergeOptions mergeOptions)
        {
            mergeOptions = mergeOptions ?? DefaultMergeOptions;

            return(state.Cache.Obtain(
                       path,
                       p =>
            {
                foreach (var prefix in EnumeratePrefixes(p))
                {
                    if (state.Cache.TryGetValue(prefix, out var tree))
                    {
                        return tree.ScopeTo(path.Segments.Skip(prefix.Segments.Count()));
                    }
                }

                var remoteSettings = state.RemoteTree?.GetSettings(p);
                var localSettings = state.LocalTree?.ScopeTo(p.Segments);

                return SettingsNodeMerger.Merge(remoteSettings, localSettings, mergeOptions);
            }));
        }