Exemplo n.º 1
0
        private static IEnumerable <TomlObject> ProcessIdentifier(TomlObject obj, ReadOnlyMemory <char> pathM)
        {
            if (pathM.IsEmpty)
            {
                return(Enumerable.Empty <TomlObject>());
            }

            var path = pathM.Span;

            switch (path[0])
            {
            case '*':
            {
                var rest = pathM.Slice(1);
                if (rest.IsEmpty)
                {
                    return(obj.GetAllSubItems());
                }

                if (IsArray(rest.Span))
                {
                    return(obj.GetAllSubItems().SelectMany(x => ProcessArray(x, rest)));
                }
                else if (IsDot(rest.Span))
                {
                    return(obj.GetAllSubItems().SelectMany(x => ProcessDot(x, rest)));
                }
                else
                {
                    throw new ArgumentException("Invalid expression after wildcard", nameof(pathM));
                }
            }

            case '[':
                throw new ArgumentException("Invalid array open bracket", nameof(pathM));

            case ']':
                throw new ArgumentException("Invalid array close bracket", nameof(pathM));

            case '.':
                throw new ArgumentException("Invalid dot", nameof(pathM));

            default:
            {
                var  subItemName = path;
                var  rest        = ReadOnlyMemory <char> .Empty;
                bool cont        = false;
                for (int i = 0; i < path.Length; i++)
                {
                    // todo allow in future
                    if (path[i] == '*')
                    {
                        throw new ArgumentException("Invalid wildcard position", nameof(pathM));
                    }

                    var currentSub = path.Slice(i);
                    if (!IsIdentifier(currentSub))                             // if (!IsName)
                    {
                        cont        = true;
                        subItemName = path.Slice(0, i);
                        rest        = pathM.Slice(i);
                        break;
                    }
                }
                var item = obj.GetSubItemByName(subItemName);
                if (item is null)
                {
                    return(Enumerable.Empty <TomlObject>());
                }

                if (cont)
                {
                    if (IsArray(rest.Span))
                    {
                        return(ProcessArray(item, rest));
                    }
                    else if (IsDot(rest.Span))
                    {
                        return(ProcessDot(item, rest));
                    }
                    else
                    {
                        throw new ArgumentException("Invalid expression name identifier", nameof(pathM));
                    }
                }
                return(new[] { item });
            }
            }
        }