Beispiel #1
0
        public ISettingsNode Transform([CanBeNull] ISettingsNode node)
        {
            ISettingsNode transformedNode;

            switch (node)
            {
            case ObjectNode objectNode:
                if (TryTransform(objectNode, out transformedNode))
                {
                    return(transformedNode);
                }

                var builder             = new ObjectNodeBuilder(objectNode.Name);
                var transformedAnything = false;

                foreach (var child in objectNode.Children)
                {
                    var transformedChild = Transform(child);
                    if (transformedChild != null)
                    {
                        builder.SetChild(transformedChild);
                    }

                    if (!ReferenceEquals(child, transformedChild))
                    {
                        transformedAnything = true;
                    }
                }

                return(transformedAnything ? builder.Build() : objectNode);

            case ArrayNode arrayNode:
                if (TryTransform(arrayNode, out transformedNode))
                {
                    return(transformedNode);
                }

                var transformedChildren = arrayNode.Children.Select(Transform).Where(t => t != null).ToArray();
                if (transformedChildren.SequenceEqual(arrayNode.Children, ByReferenceEqualityComparer <ISettingsNode> .Instance))
                {
                    return(arrayNode);
                }

                return(new ArrayNode(arrayNode.Name, transformedChildren));

            case ValueNode valueNode:
                return(TryTransform(valueNode, out transformedNode) ? transformedNode : valueNode);

            default:
                return(node);
            }
        }
        public void Should_not_mutate_original_node()
        {
            var initialNode = new ObjectNode("name", new ISettingsNode[]
            {
                new ValueNode("key1", "value1"),
                new ValueNode("key2", "value2")
            });

            builder = new ObjectNodeBuilder(initialNode)
            {
                Name = "name2"
            };

            builder.SetChild(new ValueNode("key1", "value0"));
            builder.SetChild(new ValueNode("key3", "value3"));

            builder.Build();

            initialNode.Name.Should().Be("name");
            initialNode.ChildrenCount.Should().Be(2);
            initialNode["key1"]?.Value.Should().Be("value1");
            initialNode["key2"]?.Value.Should().Be("value2");
        }
        public void Should_return_mutated_node()
        {
            var initialNode = new ObjectNode("name", new ISettingsNode[]
            {
                new ValueNode("key1", "value1"),
                new ValueNode("key2", "value2")
            });

            builder = new ObjectNodeBuilder(initialNode)
            {
                Name = "name2"
            };

            builder.SetChild(new ValueNode("key1", "value0"));
            builder.SetChild(new ValueNode("key3", "value3"));

            var builtNode = builder.Build();

            builtNode.Name.Should().Be("name2");
            builtNode["key1"]?.Value.Should().Be("value0");
            builtNode["key2"]?.Value.Should().Be("value2");
            builtNode["key3"]?.Value.Should().Be("value3");
        }
 public void Should_allow_to_build_multiple_times()
 {
     builder.Build();
     builder.Build();
     builder.Build();
 }
        private static ISettingsNode ParseSettings(string[] args, string defaultKey, string defaultValue)
        {
            var resultBuilder   = new ObjectNodeBuilder();
            var valueNodeIndex  = new Dictionary <string, List <ValueNode> >(StringComparer.OrdinalIgnoreCase);
            var objectNodeIndex = new Dictionary <string, ISettingsNode>(StringComparer.OrdinalIgnoreCase);

            foreach (var pair in CommandLineArgumentsParser.Parse(args ?? Array.Empty <string>()))
            {
                var key = pair.key ?? defaultKey;
                if (key == null)
                {
                    continue;
                }

                var value = pair.value ?? defaultValue;
                if (value == null)
                {
                    continue;
                }

                var node = TreeFactory.CreateTreeByMultiLevelKey(null, key.Split('.'), value).Children.Single();
                var name = node.Name ?? string.Empty;

                if (node is ObjectNode objectNode)
                {
                    var currentNode = objectNodeIndex.TryGetValue(name, out var current) ? current : null;

                    objectNodeIndex[name] = SettingsNodeMerger.Merge(currentNode, objectNode, null);
                }
                else if (node is ValueNode valueNode)
                {
                    if (!valueNodeIndex.TryGetValue(name, out var nodes))
                    {
                        valueNodeIndex[name] = nodes = new List <ValueNode>();
                    }

                    nodes.Add(valueNode);
                }
            }

            foreach (var pair in valueNodeIndex)
            {
                if (pair.Value.Count == 1)
                {
                    resultBuilder.SetChild(pair.Value.Single());
                }
                else
                {
                    resultBuilder.SetChild(new ArrayNode(pair.Key, pair.Value.Select((node, index) => new ValueNode(index.ToString(), node.Value)).ToArray()));
                }
            }

            foreach (var pair in objectNodeIndex)
            {
                resultBuilder.SetChild(pair.Value);
            }

            var result = resultBuilder.Build();

            if (result.ChildrenCount > 0)
            {
                return(result);
            }

            return(null);
        }