Exemplo n.º 1
0
        public static YamlNode Merge(this YamlNode node, YamlNode otherNode)
        {
            var resultNode = node.Clone();

            if (otherNode == null || node.NodeType != otherNode.NodeType)
            {
                return(resultNode);
            }
            switch (node.NodeType)
            {
            case YamlNodeType.Mapping:
                foreach (var mappingChild in ((YamlMappingNode)otherNode).Children)
                {
                    if (((YamlMappingNode)resultNode).Children.ContainsKey(mappingChild.Key))
                    {
                        ((YamlMappingNode)resultNode).Children[mappingChild.Key.Clone()] =
                            ((YamlMappingNode)resultNode).Children[mappingChild.Key].Merge(mappingChild.Value);
                    }
                    else
                    {
                        ((YamlMappingNode)resultNode).Children[mappingChild.Key.Clone()] = mappingChild.Value.Clone();
                    }
                }
                break;

            case YamlNodeType.Scalar:
            case YamlNodeType.Sequence:
                resultNode = otherNode.Clone();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(resultNode);
        }
Exemplo n.º 2
0
        public static YamlNode Diff(this YamlNode node, YamlNode otherNode)
        {
            if (otherNode == null || node.NodeType != otherNode.NodeType || node.GetHashCode() == otherNode.GetHashCode())
            {
                return(null);
            }
            YamlNode result = null;

            switch (node.NodeType)
            {
            case YamlNodeType.Mapping:
                foreach (var keyValuePair in GetMappingChildrenDiff(((YamlMappingNode)node).Children,
                                                                    ((YamlMappingNode)otherNode).Children).Concat(
                             GetMappingChildrenDiff(((YamlMappingNode)otherNode).Children.
                                                    Where(kv => !((YamlMappingNode)node).Children.ContainsKey(kv.Key)),
                                                    ((YamlMappingNode)node).Children)))
                {
                    if (result == null)
                    {
                        result = new YamlMappingNode();
                    }
                    ((YamlMappingNode)result).Children.Add(keyValuePair);
                }
                break;

            case YamlNodeType.Scalar:
            case YamlNodeType.Sequence:
                result = otherNode.Clone();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(result);
        }
Exemplo n.º 3
0
        public static YamlNode GetChildBranch(this YamlNode node, YamlNode child)
        {
            YamlNode result = null;

            if (node == child)
            {
                result = node.Clone();
            }
            else if (node.NodeType == YamlNodeType.Mapping)
            {
                foreach (var mappingChild in ((YamlMappingNode)node).Children)
                {
                    var childBranch = mappingChild.Value.GetChildBranch(child);
                    if (childBranch == null)
                    {
                        continue;
                    }
                    result = new YamlMappingNode();
                    ((YamlMappingNode)result).Add(mappingChild.Key.Clone(), childBranch);
                    break;
                }
            }
            return(result);
        }