Ejemplo n.º 1
0
        private KeyValuePair <string, object> TraverseSingleObject(
            object responseNode, FieldSelectorTreeNode selectorNode, string parentSelectorPath)
        {
            string currentPathSelector = FormatSelectorPath(parentSelectorPath, selectorNode);

            IPropertyValueGetter currentProperty =
                _propertyInfoCache.GetOrAdd(
                    currentPathSelector,
                    key => _propertyGetterFactory.CreatePropertyValueGetter(responseNode, selectorNode.MemberName)
                    );

            if (currentProperty == null)
            {
                return(new KeyValuePair <string, object>());
            }

            object value = currentProperty.GetPropertyValue(responseNode);

            if (selectorNode.Children.IsEmpty())
            {
                return(new KeyValuePair <string, object>(currentProperty.PropertyName, value));
            }

            object keyValue = TraverseSelectorNodes(value, selectorNode.Children, currentPathSelector);

            return(new KeyValuePair <string, object>(currentProperty.PropertyName, keyValue));
        }
        public FieldSelectorTreeNode GetOrAddChildNode(string memberName)
        {
            FieldSelectorTreeNode childNode = Children.Find(node => node.MemberName == memberName);

            if (childNode == null)
            {
                childNode = new FieldSelectorTreeNode(memberName);
                Children.Add(childNode);
            }

            return(childNode);
        }
        public FieldSelectorTreeNode GetOrAddChildNode(string memberName)
        {
            FieldSelectorTreeNode childNode = Children.Find(node => node.MemberName == memberName);

            if (childNode == null)
            {
                childNode = new FieldSelectorTreeNode(memberName);
                Children.Add(childNode);
            }

            return childNode;
        }
        public static List<FieldSelectorTreeNode> Expand(string partialResponseFields)
        {
            if (string.IsNullOrWhiteSpace(partialResponseFields))
            {
                return new List<FieldSelectorTreeNode>();
            }

            if (FieldSelectorConstants.StartsWithReservedToken(partialResponseFields))
            {
                throw new ArgumentException(
                    "A reserved token can not be the first character of the fields selector.", "partialResponseFields");
            }

            var subSelectStack = new Stack<FieldSelectorTreeNode>();
            var nestedStack = new Stack<FieldSelectorTreeNode>();
            var currentMemberName = new StringBuilder();

            var parent = new FieldSelectorTreeNode(string.Empty);
            foreach (char c in partialResponseFields)
            {
                FieldSelectorTreeNode childNode;
                switch (c)
                {
                    case FieldSelectorConstants.NestedFieldSelector:
                        if (currentMemberName.Length == 0)
                        {
                            throw new ArgumentException(
                                string.Format(
                                    "Nested Field token '{0}' can not be preceeded by another reserved token.",
                                    FieldSelectorConstants.NestedFieldSelector),
                                "partialResponseFields");
                        }

                        childNode = parent.GetOrAddChildNode(currentMemberName.ToString());
                        currentMemberName = new StringBuilder();

                        nestedStack.Push(parent);
                        parent = childNode;
                        break;
                    case FieldSelectorConstants.MultipleFieldSeparator:
                        if (currentMemberName.Length != 0)
                        {
                            parent.GetOrAddChildNode(currentMemberName.ToString());
                            currentMemberName = new StringBuilder();
                        }
                        while (nestedStack.Count > 0)
                        {
                            parent = nestedStack.Pop();
                        }
                        break;
                    case FieldSelectorConstants.BeginSubSelectExpression:
                        if (currentMemberName.Length == 0)
                        {
                            throw new ArgumentException(
                                string.Format(
                                    "Begin Subselection token '{0}' can not be preceeded by another reserved token.",
                                    FieldSelectorConstants.BeginSubSelectExpression),
                                "partialResponseFields");
                        }

                        childNode = parent.GetOrAddChildNode(currentMemberName.ToString());
                        currentMemberName = new StringBuilder();

                        subSelectStack.Push(parent);
                        parent = childNode;

                        nestedStack = new Stack<FieldSelectorTreeNode>();
                        break;
                    case FieldSelectorConstants.EndSubSelectExpression:
                        if (currentMemberName.Length != 0)
                        {
                            parent.GetOrAddChildNode(currentMemberName.ToString());
                            currentMemberName = new StringBuilder();
                        }
                        parent = subSelectStack.Pop();
                        nestedStack = new Stack<FieldSelectorTreeNode>();
                        break;
                    default:
                        currentMemberName.Append(c);
                        break;
                }
            }

            if (currentMemberName.Length > 0)
            {
                parent.GetOrAddChildNode(currentMemberName.ToString());

                while (nestedStack.Count > 0)
                {
                    parent = nestedStack.Pop();
                }
            }

            return parent.Children;
        }
Ejemplo n.º 5
0
 private static string FormatSelectorPath(string rootPath, FieldSelectorTreeNode subNode)
 {
     return(string.Format("{0}{1}{2}", rootPath, FieldSelectorConstants.NestedFieldSelector, subNode.MemberName));
 }
        public static List <FieldSelectorTreeNode> Expand(string partialResponseFields)
        {
            if (string.IsNullOrWhiteSpace(partialResponseFields))
            {
                return(new List <FieldSelectorTreeNode>());
            }

            if (FieldSelectorConstants.StartsWithReservedToken(partialResponseFields))
            {
                throw new ArgumentException(
                          "A reserved token can not be the first character of the fields selector.", "partialResponseFields");
            }

            var subSelectStack    = new Stack <FieldSelectorTreeNode>();
            var nestedStack       = new Stack <FieldSelectorTreeNode>();
            var currentMemberName = new StringBuilder();

            var parent = new FieldSelectorTreeNode(string.Empty);

            foreach (char c in partialResponseFields)
            {
                FieldSelectorTreeNode childNode;
                switch (c)
                {
                case FieldSelectorConstants.NestedFieldSelector:
                    if (currentMemberName.Length == 0)
                    {
                        throw new ArgumentException(
                                  string.Format(
                                      "Nested Field token '{0}' can not be preceeded by another reserved token.",
                                      FieldSelectorConstants.NestedFieldSelector),
                                  "partialResponseFields");
                    }

                    childNode         = parent.GetOrAddChildNode(currentMemberName.ToString());
                    currentMemberName = new StringBuilder();

                    nestedStack.Push(parent);
                    parent = childNode;
                    break;

                case FieldSelectorConstants.MultipleFieldSeparator:
                    if (currentMemberName.Length != 0)
                    {
                        parent.GetOrAddChildNode(currentMemberName.ToString());
                        currentMemberName = new StringBuilder();
                    }
                    while (nestedStack.Count > 0)
                    {
                        parent = nestedStack.Pop();
                    }
                    break;

                case FieldSelectorConstants.BeginSubSelectExpression:
                    if (currentMemberName.Length == 0)
                    {
                        throw new ArgumentException(
                                  string.Format(
                                      "Begin Subselection token '{0}' can not be preceeded by another reserved token.",
                                      FieldSelectorConstants.BeginSubSelectExpression),
                                  "partialResponseFields");
                    }

                    childNode         = parent.GetOrAddChildNode(currentMemberName.ToString());
                    currentMemberName = new StringBuilder();

                    subSelectStack.Push(parent);
                    parent = childNode;

                    nestedStack = new Stack <FieldSelectorTreeNode>();
                    break;

                case FieldSelectorConstants.EndSubSelectExpression:
                    if (currentMemberName.Length != 0)
                    {
                        parent.GetOrAddChildNode(currentMemberName.ToString());
                        currentMemberName = new StringBuilder();
                    }
                    parent      = subSelectStack.Pop();
                    nestedStack = new Stack <FieldSelectorTreeNode>();
                    break;

                default:
                    currentMemberName.Append(c);
                    break;
                }
            }

            if (currentMemberName.Length > 0)
            {
                parent.GetOrAddChildNode(currentMemberName.ToString());

                while (nestedStack.Count > 0)
                {
                    parent = nestedStack.Pop();
                }
            }

            return(parent.Children);
        }
 private static string FormatSelectorPath(string rootPath, FieldSelectorTreeNode subNode)
 {
     return string.Format("{0}{1}{2}", rootPath, FieldSelectorConstants.NestedFieldSelector, subNode.MemberName);
 }
        private KeyValuePair<string, object> TraverseSingleObject(
            object responseNode, FieldSelectorTreeNode selectorNode, string parentSelectorPath)
        {
            string currentPathSelector = FormatSelectorPath(parentSelectorPath, selectorNode);

            IPropertyValueGetter currentProperty =
                _propertyInfoCache.GetOrAdd(
                    currentPathSelector,
                    key => _propertyGetterFactory.CreatePropertyValueGetter(responseNode, selectorNode.MemberName)
                    );

            if (currentProperty == null)
            {
                return new KeyValuePair<string, object>();
            }

            object value = currentProperty.GetPropertyValue(responseNode);

            if (selectorNode.Children.IsEmpty())
            {
                return new KeyValuePair<string, object>(currentProperty.PropertyName, value);
            }

            object keyValue = TraverseSelectorNodes(value, selectorNode.Children, currentPathSelector);

            return new KeyValuePair<string, object>(currentProperty.PropertyName, keyValue);
        }