Example #1
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string text)
            {
                if (string.IsNullOrEmpty(text))
                {
                    return(Paging.Page(1));
                }

                if (text == "*" || text == "0")
                {
                    return(Paging.Disabled);
                }

                var match = _regex_.Match(text);

                if (match.Success && match.Groups["index"].Success)
                {
                    if (match.Groups["count"].Success)
                    {
                        var total = match.Groups["total"].Success ? long.Parse(match.Groups["total"].Value) : 0;

                        if (total > 0)
                        {
                            return new Paging(int.Parse(match.Groups["index"].Value), (int)total / int.Parse(match.Groups["count"].Value))
                                   {
                                       TotalCount = total
                                   }
                        }
                        ;
                        else
                        {
                            return(new Paging(int.Parse(match.Groups["index"].Value)));
                        }
                    }

                    return(match.Groups["size"].Success ?
                           Paging.Page(int.Parse(match.Groups["index"].Value), int.Parse(match.Groups["size"].Value)) :
                           Paging.Page(int.Parse(match.Groups["index"].Value)));
                }
            }

            return(base.ConvertFrom(context, culture, value));
        }
            public bool Complete(out Collections.INamedCollection <TMember> members)
            {
                members = null;

                if (_stack != null && _stack.Count > 0)
                {
                    _onError?.Invoke("SyntaxError: The data schema is empty.");
                    return(false);
                }

                switch (State)
                {
                case State.None:
                    if (_members != null && _members.Count > 0)
                    {
                        members = _members;
                    }

                    return(true);

                case State.Asterisk:
                    this.Include("*");
                    break;

                case State.Exclude:
                    this.Exclude();
                    break;

                case State.Include:
                    this.Include();
                    break;

                case State.PagingCount:
                    if (_bufferIndex == 0)
                    {
                        _onError?.Invoke("SyntaxError: Expected pagination number in the data schema, but missing.");
                        return(false);
                    }

                    _current.Paging = Paging.Page(1, int.Parse(new string(_buffer, 0, _bufferIndex)));
                    break;

                case State.PagingSize:
                    if (_bufferIndex == 0)
                    {
                        _onError?.Invoke("SyntaxError: Expected pagination size in the data schema, but missing.");
                        return(false);
                    }

                    var buffer = new string(_buffer, 0, _bufferIndex);

                    if (buffer != "?")
                    {
                        _current.Paging.PageSize = int.Parse(buffer);
                    }

                    break;

                default:
                    _onError?.Invoke($"SyntaxError: The data schema expression is incorrect({State}).");
                    return(false);
                }

                members = _members;
                return(true);
            }
        private static bool DoPagingCount(ref StateContext context)
        {
            string buffer;

            switch (context.Character)
            {
            case '?':
                if (context.HasBuffer())
                {
                    context.OnError("SyntaxError: The pagination number format of the data schema is incorrect.");
                    return(false);
                }

                context.Current.Paging = null;
                context.State          = State.Include;
                return(true);

            case '*':
                if (context.HasBuffer())
                {
                    context.OnError("SyntaxError: The pagination number format of the data schema is incorrect.");
                    return(false);
                }

                context.Current.Paging = Paging.Disabled;
                context.State          = State.Include;
                return(true);

            case '/':
                if (!context.TryGetBuffer(out buffer))
                {
                    context.OnError("SyntaxError: Expected pagination number in the data schema, but missing.");
                    return(false);
                }

                context.Current.Paging = Paging.Page(int.Parse(buffer));
                context.State          = State.PagingSize;

                return(true);

            case '(':
                if (!context.TryGetBuffer(out buffer))
                {
                    context.OnError("SyntaxError: Expected pagination number in the data schema, but missing.");
                    return(false);
                }

                context.Current.Paging = Paging.Page(1, int.Parse(buffer));
                context.State          = State.SortingField;
                return(true);

            case '{':
                if (!context.TryGetBuffer(out buffer))
                {
                    context.OnError("SyntaxError: Expected pagination number in the data schema, but missing.");
                    return(false);
                }

                context.Current.Paging = Paging.Page(1, int.Parse(buffer));
                context.Push();
                context.State = State.None;
                return(true);

            default:
                if (char.IsDigit(context.Character))
                {
                    context.Accept();
                    return(true);
                }

                context.OnError($"SyntaxError: The pagination number of the data schema contains '{context.Character}' illegal character.");
                return(false);
            }
        }
            public bool Complete(out Collections.INamedCollection <TMember> members)
            {
                members = null;

                if (_stack != null && _stack.Count > 0)
                {
                    _onError?.Invoke("");
                    return(false);
                }

                switch (State)
                {
                case State.None:
                    if (_members != null && _members.Count > 0)
                    {
                        members = _members;
                    }

                    return(true);

                case State.Asterisk:
                    this.Include("*");
                    break;

                case State.Exclude:
                    this.Exclude();
                    break;

                case State.Include:
                    this.Include();
                    break;

                case State.PagingCount:
                    if (_bufferIndex == 0)
                    {
                        _onError?.Invoke("");
                        return(false);
                    }

                    _current.Paging = Paging.Page(1, int.Parse(new string(_buffer, 0, _bufferIndex)));
                    break;

                case State.PagingSize:
                    if (_bufferIndex == 0)
                    {
                        _onError?.Invoke("");
                        return(false);
                    }

                    var buffer = new string(_buffer, 0, _bufferIndex);

                    if (buffer != "?")
                    {
                        _current.Paging.PageSize = int.Parse(buffer);
                    }

                    break;

                default:
                    _onError?.Invoke("");
                    return(false);
                }

                members = _members;
                return(true);
            }
        private static bool DoPagingCount(ref StateContext context)
        {
            string buffer;

            switch (context.Character)
            {
            case '?':
                if (context.HasBuffer())
                {
                    context.OnError("");
                    return(false);
                }

                context.Current.Paging = null;
                context.State          = State.Include;
                return(true);

            case '*':
                if (context.HasBuffer())
                {
                    context.OnError("");
                    return(false);
                }

                context.Current.Paging = Paging.Disable;
                context.State          = State.Include;
                return(true);

            case '/':
                if (!context.TryGetBuffer(out buffer))
                {
                    context.OnError("");
                    return(false);
                }

                context.Current.Paging = Paging.Page(int.Parse(buffer));
                context.State          = State.PagingSize;

                return(true);

            case '(':
                if (!context.TryGetBuffer(out buffer))
                {
                    context.OnError("");
                    return(false);
                }

                context.Current.Paging = Paging.Page(1, int.Parse(buffer));
                context.State          = State.SortingField;
                return(true);

            case '{':
                if (!context.TryGetBuffer(out buffer))
                {
                    context.OnError("");
                    return(false);
                }

                context.Current.Paging = Paging.Page(1, int.Parse(buffer));
                context.Push();
                context.State = State.None;
                return(true);

            default:
                if (char.IsDigit(context.Character))
                {
                    context.Accept();
                    return(true);
                }

                context.OnError("");
                return(false);
            }
        }